-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
365 lines (305 loc) · 13.4 KB
/
Copy pathModel.cpp
File metadata and controls
365 lines (305 loc) · 13.4 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Add this implementation to your model.h or create a model.cpp file
#include "model.h"
void Model::loadModel(string const& path)
{
// Use better flags for GLB files - especially important for larger models
unsigned int flags = aiProcess_Triangulate |
aiProcess_GenSmoothNormals |
aiProcess_CalcTangentSpace |
//aiProcess_FlipUVs | // Important for GLB files
aiProcess_JoinIdenticalVertices |
aiProcess_ValidateDataStructure |
aiProcess_ImproveCacheLocality |
aiProcess_RemoveRedundantMaterials |
aiProcess_FixInfacingNormals |
aiProcess_OptimizeMeshes;
scene = importer.ReadFile(path, flags);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
cout << "ERROR::ASSIMP:: " << importer.GetErrorString() << endl;
return;
}
directory = path.substr(0, path.find_last_of('/'));
// Debug scene information
cout << "=== SCENE DEBUG INFO ===" << endl;
cout << "Root node children: " << scene->mRootNode->mNumChildren << endl;
cout << "Total meshes: " << scene->mNumMeshes << endl;
cout << "Materials: " << scene->mNumMaterials << endl;
cout << "Embedded textures: " << scene->mNumTextures << endl;
// Print root node transformation
aiMatrix4x4& rootTransform = scene->mRootNode->mTransformation;
cout << "Root transform: [" << rootTransform.a1 << "," << rootTransform.a2 << "," << rootTransform.a3 << "," << rootTransform.a4 << "]" << endl;
// Process with identity matrix initially
processNode(scene->mRootNode, scene, glm::mat4(1.0f));
}
void Model::processNode(aiNode* node, const aiScene* scene, glm::mat4 parentTransform)
{
// Convert assimp matrix to glm matrix
glm::mat4 nodeTransform = aiMatrix4x4ToGlm(node->mTransformation);
// Apply parent transformation
glm::mat4 globalTransform = parentTransform * nodeTransform;
// Debug node information
cout << "Processing node: " << node->mName.C_Str()
<< " with " << node->mNumMeshes << " meshes" << endl;
// Process each mesh in this node
for (unsigned int i = 0; i < node->mNumMeshes; i++)
{
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(processMesh(mesh, scene, globalTransform));
}
// Process child nodes recursively
for (unsigned int i = 0; i < node->mNumChildren; i++)
{
processNode(node->mChildren[i], scene, globalTransform);
}
}
Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene, glm::mat4 transform)
{
vector<Vertex> vertices;
vector<unsigned int> indices;
vector<Texture> textures;
// Debug mesh information
cout << "Processing mesh with " << mesh->mNumVertices << " vertices" << endl;
// Process vertices with transformation applied
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
{
Vertex vertex;
// Apply transformation to vertex position
glm::vec4 pos = glm::vec4(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z, 1.0f);
pos = transform * pos;
vertex.Position = glm::vec3(pos.x, pos.y, pos.z);
// Transform normals (use inverse transpose for correct normal transformation)
if (mesh->HasNormals())
{
glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(transform)));
glm::vec3 normal = glm::vec3(mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z);
vertex.Normal = glm::normalize(normalMatrix * normal);
}
else
{
vertex.Normal = glm::vec3(0.0f, 1.0f, 0.0f); // Default normal
}
// Texture coordinates
if (mesh->mTextureCoords[0])
{
glm::vec2 vec;
vec.x = mesh->mTextureCoords[0][i].x;
vec.y = mesh->mTextureCoords[0][i].y;
vertex.TexCoords = vec;
// Transform tangents if available
if (mesh->mTangents) {
glm::vec3 tangent = glm::vec3(mesh->mTangents[i].x, mesh->mTangents[i].y, mesh->mTangents[i].z);
vertex.Tangent = glm::normalize(glm::mat3(transform) * tangent);
}
if (mesh->mBitangents) {
glm::vec3 bitangent = glm::vec3(mesh->mBitangents[i].x, mesh->mBitangents[i].y, mesh->mBitangents[i].z);
vertex.Bitangent = glm::normalize(glm::mat3(transform) * bitangent);
}
}
else
vertex.TexCoords = glm::vec2(0.0f, 0.0f);
// Initialize bone data to zero
for (int j = 0; j < MAX_BONE_INFLUENCE; j++) {
vertex.m_BoneIDs[j] = 0;
vertex.m_Weights[j] = 0.0f;
}
vertices.push_back(vertex);
}
// Process indices (unchanged)
for (unsigned int i = 0; i < mesh->mNumFaces; i++)
{
aiFace face = mesh->mFaces[i];
for (unsigned int j = 0; j < face.mNumIndices; j++)
indices.push_back(face.mIndices[j]);
}
// Process materials (unchanged)
if (mesh->mMaterialIndex >= 0)
{
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
vector<Texture> diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse");
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
vector<Texture> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
vector<Texture> normalMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal");
textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
vector<Texture> heightMaps = loadMaterialTextures(material, aiTextureType_AMBIENT, "texture_height");
textures.insert(textures.end(), heightMaps.begin(), heightMaps.end());
}
return Mesh(vertices, indices, textures);
}
vector<Texture> Model::loadMaterialTextures(aiMaterial* mat, aiTextureType type, string typeName)
{
vector<Texture> textures;
for (unsigned int i = 0; i < mat->GetTextureCount(type); i++)
{
aiString str;
mat->GetTexture(type, i, &str);
// check if texture was loaded before
bool skip = false;
for (unsigned int j = 0; j < textures_loaded.size(); j++)
{
if (std::strcmp(textures_loaded[j].path.data(), str.C_Str()) == 0)
{
textures.push_back(textures_loaded[j]);
skip = true;
break;
}
}
if (!skip)
{
Texture texture;
// Pass 'this' to allow access to embedded textures
texture.id = TextureFromFile(str.C_Str(), this->directory, false, this);
texture.type = typeName;
texture.path = str.C_Str();
textures.push_back(texture);
textures_loaded.push_back(texture);
}
}
return textures;
}
unsigned int Model::loadEmbeddedTexture(const char* path)
{
if (!scene || path[0] != '*') {
return 0;
}
// Extract texture index from path (remove '*' prefix)
string pathStr(path);
int textureIndex;
try {
textureIndex = std::stoi(pathStr.substr(1));
}
catch (const std::exception& e) {
cout << "Invalid embedded texture index: " << path << endl;
return 0;
}
if (textureIndex >= 0 && textureIndex < static_cast<int>(scene->mNumTextures)) {
aiTexture* texture = scene->mTextures[textureIndex];
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
if (texture->mHeight == 0) {
// Compressed texture format (JPEG, PNG, etc.)
cout << "Loading compressed embedded texture " << textureIndex
<< " (size: " << texture->mWidth << " bytes)" << endl;
int width, height, nrComponents;
unsigned char* data = stbi_load_from_memory(
reinterpret_cast<unsigned char*>(texture->pcData),
texture->mWidth,
&width, &height, &nrComponents, 0
);
if (data) {
GLenum format;
if (nrComponents == 1) format = GL_RED;
else if (nrComponents == 3) format = GL_RGB;
else if (nrComponents == 4) format = GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
cout << "Successfully loaded embedded texture " << textureIndex
<< " (" << width << "x" << height << ", " << nrComponents << " components)" << endl;
stbi_image_free(data);
}
else {
cout << "Failed to decode embedded texture " << textureIndex << endl;
// Create a default colored texture as fallback
unsigned char defaultData[] = { 128, 128, 255, 255 }; // Light blue
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, defaultData);
}
}
else {
// Uncompressed texture data
cout << "Loading uncompressed embedded texture " << textureIndex
<< " (" << texture->mWidth << "x" << texture->mHeight << ")" << endl;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->mWidth, texture->mHeight,
0, GL_RGBA, GL_UNSIGNED_BYTE, texture->pcData);
glGenerateMipmap(GL_TEXTURE_2D);
}
// Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return textureID;
}
else {
cout << "Invalid embedded texture index: " << textureIndex
<< " (max: " << scene->mNumTextures - 1 << ")" << endl;
}
return 0;
}
// Updated TextureFromFile function
unsigned int TextureFromFile(const char* path, const string& directory, bool gamma, Model* model)
{
string filename = string(path);
// Handle embedded textures first
if (filename[0] == '*' && model) {
unsigned int embeddedTexture = model->loadEmbeddedTexture(path);
if (embeddedTexture != 0) {
return embeddedTexture;
}
// If embedded texture loading failed, create a default texture
cout << "Embedded texture loading failed for " << path << ", creating default" << endl;
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
// Create a distinctive color based on the texture index
int index = 0;
try {
index = std::stoi(filename.substr(1));
}
catch (...) {}
unsigned char data[4];
switch (index % 8) {
case 0: data[0] = 255; data[1] = 100; data[2] = 100; break; // Red
case 1: data[0] = 100; data[1] = 255; data[2] = 100; break; // Green
case 2: data[0] = 100; data[1] = 100; data[2] = 255; break; // Blue
case 3: data[0] = 255; data[1] = 255; data[2] = 100; break; // Yellow
case 4: data[0] = 255; data[1] = 100; data[2] = 255; break; // Magenta
case 5: data[0] = 100; data[1] = 255; data[2] = 255; break; // Cyan
case 6: data[0] = 255; data[1] = 150; data[2] = 100; break; // Orange
case 7: data[0] = 150; data[1] = 100; data[2] = 255; break; // Purple
}
data[3] = 255;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return textureID;
}
// Handle regular file textures
filename = directory + '/' + filename;
unsigned int textureID;
glGenTextures(1, &textureID);
int width, height, nrComponents;
unsigned char* data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0);
if (data)
{
GLenum format;
if (nrComponents == 1)
format = GL_RED;
else if (nrComponents == 3)
format = GL_RGB;
else if (nrComponents == 4)
format = GL_RGBA;
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_image_free(data);
cout << "Loaded file texture: " << filename << endl;
}
else
{
cout << "Texture failed to load at path: " << filename << endl;
stbi_image_free(data);
// Create default white texture
glBindTexture(GL_TEXTURE_2D, textureID);
unsigned char defaultData[] = { 255, 255, 255, 255 };
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, defaultData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
return textureID;
}