/
model.h
219 lines (184 loc) · 4.37 KB
/
model.h
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* \file
* \brief Declaration of the Model class
*/
#ifndef MODEL_H
#define MODEL_H
#include <string>
#include <vector>
#include <map>
#include <GL/glfw.h>
#include "mar_tp1/resource_manager.h"
#include "mar_tp1/vector.h"
namespace mar
{
namespace
{
// --- Some forwards
struct OBJObject;
struct OBJFace;
struct OBJVertex;
struct OBJNormal;
struct OBJTexture;
struct OBJFaceItem;
struct OBJMaterial;
struct OBJFace
{
std::vector<OBJFaceItem> items;
std::string material;
};
struct OBJFaceItem
{
OBJFaceItem()
{
vertexIndex = 0;
normalIndex = 0;
textureIndex = 0;
}
unsigned int vertexIndex;
unsigned int normalIndex;
unsigned int textureIndex;
};
struct OBJVertex
{
OBJVertex()
{
coords[0] = 0;
coords[1] = 0;
coords[2] = 0;
}
float coords[3];
};
struct OBJNormal
{
OBJNormal()
{
coords[0] = 0;
coords[1] = 0;
coords[2] = 0;
}
float coords[3];
};
struct OBJTexture
{
OBJTexture()
{
coords[0] = 0;
coords[1] = 0;
coords[2] = 0;
}
float coords[3];
};
struct OBJMaterial
{
float Ka[4];
float Kd[4];
float Ks[4];
GLuint texture;
float texScaleU;
float texScaleV;
};
struct OBJObject
{
OBJObject()
{
vertices.push_back( OBJVertex() );
normals.push_back ( OBJNormal() );
textures.push_back( OBJTexture() );
computedNormals.push_back( OBJNormal() );
}
std::vector<OBJVertex> vertices;
std::vector<OBJNormal> computedNormals;
std::vector<OBJNormal> normals;
std::vector<OBJTexture> textures;
std::vector<OBJFace> faces;
std::map<std::string,OBJMaterial> materials;
};
}
/**
* \brief This class handles a 3D model. Only the OBJ file format is supported.
*/
class Model
{
public:
/**
* \brief Create a new, empty model.
*/
Model();
/**
* \brief Destructs a model.
*/
~Model();
public:
/**
* \brief Load an OBJ model.
* \param data_path Path from where to look for textures (when processing MTL)
* \param mesh_filename Path to the OBJ file
* \param material_filename Path to the MTL file
* \return true if the model was loaded successfully, false otherwise
*/
bool load( const std::string& data_path, const std::string& mesh_filename, const std::string& material_filename="" );
/**
* \brief Render the model
* \param withBoundingBox true if the bounding box should be rendered as well
*/
void render( bool withBoundingBox=false ) const;
public:
/**
* \brief Retrieve minimum vertex of the bounding box.
*/
const Vector<3>& minimumVertex() const;
/**
* \brief Retrieve maximum vertex of the bounding box.
*/
const Vector<3>& maximumVertex() const;
protected:
/**
* \brief Process a single line of an OBJ file.
*/
void processMeshLine( const std::string& line );
/**
* \brief Process a single line of a MTL file.
*/
void processMaterialLine( const std::string& line );
/**
* \brief Build the display list for the model.
*/
void build();
/**
* \brief Render the bounding box of the model
* \see render
*/
void renderBoundingBox() const;
private:
/**
* \brief OBJ structure containing the model.
*/
OBJObject object_;
/**
* \brief ID of the model display list.
*/
GLuint listId_;
/**
* \brief Name of the current material during parsing.
*/
std::string currentMaterial_;
/**
* \brief Resource manager containing material textures.
*/
ResourceManager resourceManager_;
/**
* \brief Path to the texture files.
*/
std::string dataPath_;
/**
* \brief Minimum vertex of the bounding box.
*/
Vector<3> minimumVertex_;
/**
* \brief Maximum vertex of the bounding box.
*/
Vector<3> maximumVertex_;
};
}
#endif // MODEL_H