-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.h
293 lines (243 loc) · 8.64 KB
/
mesh.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#ifndef MESH_H_sdfjlasdfjfsdfjljfasdf
#define MESH_H_sdfjlasdfjfsdfjljfasdf
#include "Vertex.h"
#include <vector>
#include <map>
#include <string>
#pragma comment(lib, "corona.lib")
#include "corona.h"
#include "Vec3D.h"
// define classes
class Triangle;
class Mesh;
//Material class of the mesh
//while colors seem useful, also texture names are loaded
//texture coordinates are also supported,
//YOU DO NOT have to use textures, if you do not want to!
//The materials are loaded from the mesh's .mtl file.
//You can modify the materials yourself in a text editor
//try it for the dodge (car model) that is provided
class Material
{
public:
Material() { cleanup(); };
Material(const Material & m)
{
*this=m;
};
Material & operator=(const Material & m)
{
Kd_=m.Kd_;
Kd_is_set_=m.Kd_is_set_; // diffuse
Ka_=m.Ka_;
Ka_is_set_=m.Ka_is_set_; // ambient
Ks_=m.Ks_;
Ks_is_set_=m.Ks_is_set_; // specular
Ns_=m.Ns_;
Ns_is_set_=m.Ns_is_set_; // specular
Ni_=m.Ni_;
Ni_is_set_=m.Ni_is_set_; // specular
Tr_=m.Tr_;
Tr_is_set_=m.Tr_is_set_; // transparency (use this value to trade off reflection/refraction
illum_ = m.illum_;
name_=m.name_;
textureName_ = m.textureName_;
containsTexture = m.containsTexture;
texturePixels = m.texturePixels;
textureWidth = m.textureWidth;
textureHeight = m.textureHeight;
return (*this);
};
void cleanup()
{
Kd_is_set_ = false;
Ka_is_set_ = false;
Ks_is_set_ = false;
Ns_is_set_ = false;
Ni_is_set_ = false;
Tr_is_set_ = false;
illum_is_set_=false;
name_="empty";
}
bool is_valid(void) const
{ return Kd_is_set_ || Ka_is_set_ || Ks_is_set_ || Tr_is_set_; }
bool has_Kd(void) { return Kd_is_set_; }
bool has_Ka(void) { return Ka_is_set_; }
bool has_Ks(void) { return Ks_is_set_; }
bool has_Ns(void) { return Ns_is_set_; }
bool has_Ni(void) { return Ni_is_set_; }
bool has_illum(void) { return illum_is_set_; }
bool has_Tr(void) { return Tr_is_set_; }
void set_Kd( float r, float g, float b )
{ Kd_=Vec3Df(r,g,b); Kd_is_set_=true; }
void set_Ka( float r, float g, float b )
{ Ka_=Vec3Df(r,g,b); Ka_is_set_=true; }
void set_Ks( float r, float g, float b )
{ Ks_=Vec3Df(r,g,b); Ks_is_set_=true; }
void set_Ns( float r)
{ Ns_=r; Ns_is_set_=true; }
void set_Ni( float r)
{ Ni_=r; Ni_is_set_=true; }
void set_illum( int r)
{ illum_=r; illum_is_set_=true; }
void set_Tr( float t )
{ Tr_=t; Tr_is_set_=true; }
void set_textureName(const std::string & s)//name of the texture image file
{
textureName_=s;
}
void set_name(const std::string & s )
{
name_=s;
}
// load the textures
void loadTexture()
{
corona::Image* textureImage;
// const char* name = textureName_.c_str();
textureImage = corona::OpenImage(textureName_.c_str(), corona::PF_R8G8B8A8);
if (!textureImage)
{
std::cout << "failed to read texture: " << textureName_ << std::endl;
return; // couldn't read image
}
// std::cout << "read texture: " << textureName_ << std::endl;
containsTexture = true;
texturePixels = textureImage->getPixels();
textureWidth = textureImage->getWidth();
textureHeight = textureImage->getHeight();
}
Vec3Df getTexture(const Mesh &MyMesh, const Triangle &t, const Vec3Df &hitpoint);
bool hasTexture() { return containsTexture; }
const Vec3Df& Kd( void ) const { return Kd_; } //diffuse
const Vec3Df& Ka( void ) const { return Ka_; } //ambiant
const Vec3Df& Ks( void ) const { return Ks_; } //specular
float Ni( void ) const { return Ni_; } //unofficial refraction index
float Ns( void ) const { return Ns_; } //shininess
int illum( void ) const { return illum_; }
float Tr( void ) const { return Tr_; } //can be hijacked, e.g., for transparency
const std::string & textureName()//name of the texture image file
{
return textureName_;
}
const std::string & name()//name of the material
{
return name_;
}
private:
Vec3Df getTexture2(Vec3Df texCoords);
// pixels: first 8 bits: red, next 8 bits: green, ...
// (red, green, blue, alpha)
void* texturePixels;
int textureWidth; // texture width
int textureHeight; // texture height
bool containsTexture = false; // if the material contains a texture
Vec3Df Kd_; bool Kd_is_set_; // diffuse
Vec3Df Ka_; bool Ka_is_set_; // ambient
Vec3Df Ks_; bool Ks_is_set_; // specular
float Ns_; bool Ns_is_set_;
float Ni_; bool Ni_is_set_;
int illum_; bool illum_is_set_;//illumination model
float Tr_; bool Tr_is_set_; // transperency
std::string name_;
std::string textureName_;
};
/************************************************************
* Triangle Class
************************************************************/
//A triangle contains 3 indices to refer to vertex positions
//and 3 indices to refer to texture coordinates (optional)
class Triangle {
public:
inline Triangle () {
v[0] = v[1] = v[2] = 0;
}
inline Triangle (const Triangle & t2) {
v[0] = t2.v[0];
v[1] = t2.v[1];
v[2] = t2.v[2];
t[0] = t2.t[0];
t[1] = t2.t[1];
t[2] = t2.t[2];
n[0] = t2.n[0];
n[1] = t2.n[1];
n[2] = t2.n[2];
normal = t2.normal;
}
inline Triangle(unsigned int v0, unsigned int t0, unsigned int v1, unsigned int t1, unsigned int v2, unsigned int t2) {
v[0] = v0;
v[1] = v1;
v[2] = v2;
t[0] = t0;
t[1] = t1;
t[2] = t2;
}
inline Triangle(unsigned int v0, unsigned int t0, unsigned int n0, unsigned int v1, unsigned int t1, unsigned int n1, unsigned int v2, unsigned int t2, unsigned int n2) {
v[0] = v0;
v[1] = v1;
v[2] = v2;
t[0] = t0;
t[1] = t1;
t[2] = t2;
n[0] = n0;
n[1] = n1;
n[2] = n2;
}
inline virtual ~Triangle () {}
inline Triangle & operator= (const Triangle & t2) {
v[0] = t2.v[0];
v[1] = t2.v[1];
v[2] = t2.v[2];
t[0] = t2.t[0];
t[1] = t2.t[1];
t[2] = t2.t[2];
normal = t2.normal;
return (*this);
}
Vec3Df normal;
inline bool operator== (const Triangle& t2)
{
return v[0] == t2.v[0] && v[1] == t2.v[1] && v[2] == t2.v[2];
}
//vertex indices
unsigned int v[3];
//texture coordinate
unsigned int t[3];
//normal indices
unsigned int n[3];
};
/************************************************************
* Basic Mesh class
************************************************************/
class Mesh {
public:
Mesh(){}
inline Mesh (const std::vector<Vertex> & v, const std::vector<Triangle> & t) : vertices (v), triangles (t) {}
bool loadMesh(const char * filename, bool randomizeTriangulation);
bool loadMtl(const char * filename, std::map<std::string, unsigned int> & materialIndex);
void computeVertexNormals();
void draw();
void drawSmooth();
//Vertices are the vertex positions, and normals of the mesh.
std::vector<Vertex> vertices;
//texCoords are the texture coordinates, these are DIFFERENT indices in triangles.
//in the current version, if you use textures, then you have to use texture coords everywhere
//for convenience, Vec3Df is used, although only 2D tex coordinates are stored (x,y entry of the Vec3Df).
std::vector<Vec3Df> texcoords;
//normals are the normals of the faces that can be used
std::vector<Vec3Df> normals;
//Triangles are the indices of the vertices involved in a triangle.
//A triangle, thus, contains a triplet of values corresponding to the 3 vertices of a triangle.
std::vector<Triangle> triangles;
//These are the material properties
//each triangle (!), NOT (!) each vertex, has a material.
//Use the triangle index to receive a material INDEX
std::vector<unsigned int> triangleMaterials;
//using the material index, you can then recover the material from this vector
//the class material is defined just above
std::vector<Material> materials;
//As an example:
//triangle triangles[i] has material index triangleMaterials[i]
//and uses Material materials[triangleMaterials[i]].
};
#endif // MESH_H