-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjElement.hpp
162 lines (125 loc) · 3.32 KB
/
ObjElement.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef __OBJ_ELEMENT_HPP__
#define __OBJ_ELEMENT_HPP__
#include "Util.hpp"
#include <string>
#include <GL/gl.h>
#include <vector>
#include <unordered_map>
#include "VertexNormal.hpp"
class Face;
class Vertex
{
public:
typedef uint64_t VertId;
typedef std::unordered_map<VertId, Vertex*> VertMap;
typedef VertMap::iterator VertMapIter;
typedef VertMap::const_iterator VertMapCIter;
static bool construct_from_line(VertMap& vmap,std::string line);
VertId vid() const
{
return _vid;
}
const Point3& pt() const
{
return _pt;
}
void add_face(Face* f)
{
_face_vec.push_back(f);
}
VertexNormal* avg_normal (VertexNormal::VertNormalMap& vnmap);
private:
Vertex(VertMap& vmap,float x, float y, float z);
~Vertex();
VertId _vid;
Point3 _pt;
std::vector<Face*> _face_vec;
VertexNormal* _avg_normal;
static VertId _next_vid()
{
static VertId vid = 0;
return ++vid;
}
};
class TextureCoordinate
{
public:
typedef uint64_t TextCoordId;
typedef std::unordered_map<TextCoordId,TextureCoordinate*> TextCoordinateMap;
typedef TextCoordinateMap::iterator TextCoordinateMapIter;
typedef TextCoordinateMap::const_iterator TextCoordinateMapCIter;
~TextureCoordinate();
static bool construct_from_line(TextCoordinateMap& tc_map,std::string line);
TextCoordId tcid() const
{
return _tcid;
}
const Point3& pt() const
{
return _pt;
}
private:
TextureCoordinate(TextCoordinateMap& tc_map,GLfloat u, GLfloat v, GLfloat w=0.0);
TextCoordId _tcid;
Point3 _pt;
static TextCoordId _next_tcid()
{
static TextCoordId tcid = 0;
return ++tcid;
}
};
struct FaceVertexData
{
FaceVertexData(Vertex* _vert, VertexNormal* _vert_normal,
TextureCoordinate* _tc_coord)
: vert(_vert), vert_normal(_vert_normal),tc_coord(_tc_coord)
{}
Vertex* vert;
// may be NULL
VertexNormal* vert_normal;
TextureCoordinate* tc_coord;
};
typedef std::vector<FaceVertexData*> FaceVertDataVec;
typedef FaceVertDataVec::iterator FaceVertDataVecIter;
typedef FaceVertDataVec::const_iterator FaceVertDataVecCIter;
class Face
{
public:
typedef uint64_t FaceId;
typedef std::unordered_map<FaceId,Face*> FaceMap;
typedef FaceMap::iterator FaceMapIter;
typedef FaceMap::const_iterator FaceMapCIter;
static bool construct_from_line(
Vertex::VertMap& vmap,TextureCoordinate::TextCoordinateMap& tcmap,
VertexNormal::VertNormalMap& vnmap,Face::FaceMap& fmap, std::string line);
~Face();
void calc_normals(VertexNormal::VertNormalMap& vnmap);
FaceId fid() const
{
return _fid;
}
FaceVertDataVecIter vert_iter_begin()
{
return _vert_data_vec.begin();
}
FaceVertDataVecIter vert_iter_end()
{
return _vert_data_vec.end();
}
VertexNormal* face_normal() const
{
return _face_normal;
}
private:
Face(FaceMap& fmap, VertexNormal::VertNormalMap& vnmap,
const FaceVertDataVec& fvdv);
FaceId _fid;
FaceVertDataVec _vert_data_vec;
VertexNormal* _face_normal;
static FaceId _next_fid()
{
static FaceId fid = 0;
return ++fid;
}
};
#endif