-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMesh.cpp
299 lines (244 loc) · 7.68 KB
/
Mesh.cpp
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
294
295
296
297
298
#include "Mesh.h"
#include "Util.h"
#include "Texture.h"
#include <assimp/assimp.hpp>
#include <assimp/aiScene.h>
#include <assimp/aiPostProcess.h>
#include "MeshTypeParser.h"
#include "Util.h"
#include <iostream>
#include <memory>
#include <list>
using namespace std;
//#define MESH_SCALE 0.001f
//#define MESH_SCALE 1.0f
Mesh :: Mesh(std::string n, float minlod)
{
name = n;
minimum_lod = minlod;
flags = 0;
//bMaterialAlloc = false;
}
Mesh :: Mesh(std::string fn, ResourceCache<Texture>& tex_map, std::string override_name, float minlod)
{
minimum_lod = minlod;
flags = 0;
//bMaterialAlloc = false;
load(fn, tex_map);
if(override_name != "")
name = override_name; // override loaded name
}
Mesh :: ~Mesh()
{
//if(bMaterialAlloc)
//{
//for(auto itr = materials.begin();
// itr != materials.end();
// ++itr)
//{
// delete *itr;
//}
//}
}
bool Mesh :: load(std::string fn, ResourceCache<Texture>& rmap)
{
//bMaterialAlloc = true;
std::string path = "";
size_t split_point;
if((split_point = fn.find_last_of("\\/")) != string::npos)
{
path = fn.substr(0,split_point+1);
fn = fn.substr(split_point+1);
}
auto_ptr<Assimp::Importer> importer(new Assimp::Importer());
unsigned int flags = aiProcess_Triangulate|
aiProcess_CalcTangentSpace|
aiProcess_GenSmoothNormals;//GenNormals
//aiProcess_GenSmoothNormals;
const aiScene* aiscene = importer->ReadFile(path + fn, flags);
std::string err = importer->GetErrorString();
if(err != "" ||
aiscene->mFlags & AI_SCENE_FLAGS_INCOMPLETE ||
aiscene->mFlags & AI_SCENE_FLAGS_VALIDATED)
{
return false;
}
ASSERT(aiscene->mNumMeshes <= 1);
if(aiscene->mNumMeshes != 1)
return false;
for(unsigned int i=0; i<aiscene->mNumMaterials; i++)
{
aiTextureType type = aiTextureType_DIFFUSE;
unsigned int ntex = aiscene->mMaterials[i]->GetTextureCount(type);
if(ntex)
{
// Grab individual texture
aiString texpath;
aiTextureMapping mapping;
unsigned int uvindex;
float blend;
aiTextureOp op;
aiTextureMapMode mapmode[3];
aiscene->mMaterials[i]->GetTexture(
type,
0,
&texpath,
&mapping,
&uvindex,
&blend,
&op,
mapmode);
ASSERT(texpath.length);
shared_ptr<Material> material = Material::allocate(path + string(texpath.data), rmap);
if(!material ||
!material->good())
return false;
materials.push_back(material);
break;
}
}
for(unsigned int i = 0; i < aiscene->mNumMeshes; i++)
loadModel(aiscene->mMeshes[i]);
return true;
}
void Mesh :: logic(unsigned int advance)
{
}
void Mesh :: render(unsigned int flags, float quality) const
{
// Level of Detail approximations
if(flags & RENDER_LOD)
{
if(quality <= minimum_lod)
return;
Mesh* lod_mesh = getLOD(quality);
if(lod_mesh)
{
lod_mesh->render(flags & (!RENDER_LOD));
return;
}
}
// Material failsafe
unsigned int nMaterials = materials.size();
if(nMaterials == 0)
return;
unsigned int i = 0;
//for(unsigned int i = 0, layer = 0; i < nMaterials; ++i)
//{
if(!materials[i] ||
!materials[i]->good())
{
return;
//break;
}
materials[i]->bind(i);
//}
// OpenGL render code
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &vertices[0].x);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, &normals[0].x);
for(unsigned int i = 0; i < nMaterials; ++i)
{
glClientActiveTexture(GL_TEXTURE0 + i);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(3, GL_FLOAT, 0, &UVs[i][0].x);
}
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, true, 0, &tangents[0].x);
glEnableVertexAttribArray(6);
glVertexAttribPointer(6, 3, GL_FLOAT, true, 0, &bitangents[0].x);
glDrawElements(GL_TRIANGLES, faces.size()*3, GL_UNSIGNED_INT, &faces[0].indices);
for(unsigned int i = 0; i < nMaterials; ++i)
{
glClientActiveTexture(GL_TEXTURE0 + i);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(6);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
Texture::unbind();
// just for reference, here's some similar immediate-mode code (old)
/*
for(unsigned int t = 0; t < faces.size(); t++)
{
glBegin(GL_TRIANGLES);
for(unsigned int i = 0; i < 3; i++) {
unsigned int index = faces[t].indices[i];
if(normals.size())
glNormal3fv(&normals[index].x);
// TODO: Add multitexture for lightmap here
if(UVs.size() && UVs[0].size())
{
glMultiTexCoord2fv(GL_TEXTURE0, &UVs[0][index].x);
glMultiTexCoord2fv(GL_TEXTURE1, &UVs[1][index].x);
}
glVertex3fv(&vertices[index].x);
}
glEnd();
}*/
}
bool Mesh :: loadModel(const aiMesh* aimesh)
{
name = aimesh->mName.data;
if(aimesh->mNumVertices)
{
if(aimesh->HasTangentsAndBitangents())
{
bitangents.resize(aimesh->mNumVertices);
for(unsigned int j=0; j<aimesh->mNumVertices; j++)
MeshTypeParser::parseVector(bitangents[j], aimesh->mBitangents[j]);
tangents.resize(aimesh->mNumVertices);
for(unsigned int j=0; j<aimesh->mNumVertices; j++)
MeshTypeParser::parseVector(tangents[j], aimesh->mTangents[j]);
}
if(aimesh->mNormals)
{
normals.resize(aimesh->mNumVertices);
for(unsigned int j=0; j<aimesh->mNumVertices; j++)
MeshTypeParser::parseVector(normals[j], aimesh->mNormals[j]);
}
if(aimesh->HasTextureCoords(0))
{
UVs.resize(1);
UVs[0].resize(aimesh->mNumVertices);
for(unsigned int j=0; j<aimesh->mNumVertices; j++)
{
MeshTypeParser::parseVector(UVs[0][j], aimesh->mTextureCoords[0][j]);
//MeshTypeParser::parseVector(UVs[1][j], aimesh_lmap->mTextureCoords[0][j]);
}
}
vertices.resize(aimesh->mNumVertices);
for(unsigned int j=0; j<aimesh->mNumVertices; j++){
MeshTypeParser::parseVector(vertices[j], aimesh->mVertices[j]);
//vertices[j] *= MESH_SCALE;
}
}
if(aimesh->mNumFaces)
{
faces.resize(aimesh->mNumFaces);
for(unsigned int j=0; j<aimesh->mNumFaces; j++)
{
ASSERT(aimesh->mFaces[j].mNumIndices == 3);
//faces[j].indices.resize(aimesh->mFaces[j].mNumIndices);
for(unsigned int k=0; k<3; k++)
faces[j].indices[k] = aimesh->mFaces[j].mIndices[k];
}
}
return true;
}
//bool Mesh :: generateShadow(const Mesh& shadow)
//{
// return false;
//}
Mesh* Mesh :: getLOD(float quality) const
{
return NULL;
/*unsigned int lod_idx = round_uint(LOD.size() * quality);
if(lod_idx < LOD.size() && lod_idx >= 0)
{
ASSERT(LOD[lod_idx]);
return LOD[lod_idx]->get();
}*/
}