-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjWriter.hpp
131 lines (127 loc) · 2.81 KB
/
ObjWriter.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#pragma once
#include <fstream>
#include <string>
namespace objwriter
{
template<class TVertex, class TIndex, class TString, class TStream>
class ObjWriterT
{
public:
explicit ObjWriterT(TStream& stream) :
m_file(stream)
{}
void comment(const TString& comment)
{
m_file << "# " << comment << std::endl;
}
void materialLib(const TString& materialLib)
{
m_file << "mtllib " << materialLib << std::endl;
}
void objectName(const TString& objName)
{
m_file << "o " << objName << std::endl;
}
void vertex(TVertex v1, TVertex v2, TVertex v3)
{
m_file << "v "
<< v1 << " "
<< v2 << " "
<< v3 << std::endl;
}
void vertex(TVertex v1, TVertex v2, TVertex v3, TVertex v4)
{
m_file << "v "
<< v1 << " "
<< v2 << " "
<< v3 << " "
<< v4 << std::endl;
}
void texcoord(TVertex t1, TVertex t2)
{
m_file << "vt "
<< t1 << " "
<< t2 << std::endl;
}
void texcoord(TVertex t1, TVertex t2, TVertex t3)
{
m_file << "vt "
<< t1 << " "
<< t2 << " "
<< t3 << std::endl;
}
void normal(TVertex n1, TVertex n2, TVertex n3)
{
m_file << "vn "
<< n1 << " "
<< n2 << " "
<< n3 << std::endl;
}
void group(const TString& groupName)
{
m_file << "g " << groupName << std::endl;
}
void material(const TString& materialName)
{
m_file << "usemtl " << materialName << std::endl;
}
/**
* \param level smoothing level between 1 and 32
*/
void smoothLevel(int level)
{
m_file << "s " << level << std::endl;
}
void smoothOff()
{
m_file << "s off" << std::endl;
}
void face(TIndex i1, TIndex i2, TIndex i3)
{
m_file << "f "
<< i1 << " "
<< i2 << " "
<< i3 << std::endl;
}
// vertex + normal indices
void face_vn(TIndex i1, TIndex n1, TIndex i2, TIndex n2, TIndex i3, TIndex n3)
{
m_file << "f "
<< i1 << "//" << n1 << " "
<< i2 << "//" << n2 << " "
<< i3 << "//" << n3 << std::endl;
}
// vertex + texture indices
void face_vt(TIndex i1, TIndex t1, TIndex i2, TIndex t2, TIndex i3, TIndex t3)
{
m_file << "f "
<< i1 << "/" << t1 << " "
<< i2 << "/" << t2 << " "
<< i3 << "/" << t3 << std::endl;
}
// vertex + texture + normal indices
void face_vtn(TIndex i1, TIndex t1, TIndex n1, TIndex i2, TIndex t2, TIndex n2, TIndex i3, TIndex t3, TIndex n3)
{
m_file << "f "
<< i1 << "/" << t1 << "/" << n1 << " "
<< i2 << "/" << t2 << "/" << n2 << " "
<< i3 << "/" << t3 << "/" << n3 << std::endl;
}
void parameterVertex(TVertex u, TVertex v)
{
m_file << "vp "
<< u << " "
<< v << std::endl;
}
void parameterVertex(TVertex u, TVertex v, TVertex w)
{
m_file << "vp "
<< u << " "
<< v << " "
<< w << std::endl;
}
private:
TStream& m_file;
};
using ObjWriter = ObjWriterT<float, int, std::string, std::fstream>;
}