-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleGeometryLoader.cpp
More file actions
231 lines (187 loc) · 7.25 KB
/
ModuleGeometryLoader.cpp
File metadata and controls
231 lines (187 loc) · 7.25 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
#include "Globals.h"
#include "Application.h"
#include "ModuleGeometryLoader.h"
#include "ComponentMesh.h"
#include "ComponentTransform.h"
#include "ComponentMaterial.h"
#include "Assimp/include/cimport.h"
#include "Assimp/include/scene.h"
#include "Assimp/include/postprocess.h"
#include "Assimp/include/cfileio.h"
#include "Glew/include/glew.h"
#include "Devil/Devil/include/il.h"
#include "Devil/Devil/include/ilut.h"
#include <string>
#pragma comment (lib, "Assimp/libx86/assimp.lib")
#pragma comment (lib, "Glew/libx86/glew32.lib")
#pragma comment (lib, "Devil/Devil/libx86/Devil.lib")
#pragma comment (lib, "Devil/Devil/libx86/ILU.lib")
#pragma comment (lib, "Devil/Devil/libx86/ILUT.lib")
//Constructor
ModuleGeometryLoader::ModuleGeometryLoader(Application* app, bool start_enabled) : Module(app, start_enabled)
{
name = "geometry_loader";
}
//Destructor
ModuleGeometryLoader::~ModuleGeometryLoader()
{
}
bool ModuleGeometryLoader::Init()
{
ilInit();
ilutInit();
ilutRenderer(ILUT_OPENGL);
// Stream log messages to Debug window
struct aiLogStream stream;
stream = aiGetPredefinedLogStream(aiDefaultLogStream_DEBUGGER, nullptr);
aiAttachLogStream(&stream);
return true;
}
bool ModuleGeometryLoader::CleanUp()
{
// detach log stream
aiDetachAllLogStreams();
ilShutDown();
return true;
}
GameObject* ModuleGeometryLoader::RecursiveLoadGeometryFromFile(const aiScene* scene, const aiNode* node, GameObject* parent)
{
aiVector3D translation;
aiVector3D scaling;
aiQuaternion rotation;
node->mTransformation.Decompose(scaling, rotation, translation);
float3 pos(translation.x, translation.y, translation.z);
float3 scale(scaling.x, scaling.y, scaling.z);
Quat rot(rotation.x, rotation.y, rotation.z, rotation.w);
static std::string name;
name = (node->mName.length > 0) ? node->mName.C_Str() : "Unnamed";
static const char* dummies[5] = {
"$AssimpFbx$_PreRotation", "$AssimpFbx$_Rotation", "$AssimpFbx$_PostRotation",
"$AssimpFbx$_Scaling", "$AssimpFbx$_Translation" };
for (int i = 0; i < 5; ++i)
{
if (name.find(dummies[i]) != std::string::npos && node->mNumChildren == 1)
{
node = node->mChildren[0];
node->mTransformation.Decompose(scaling, rotation, translation);
// accumulate transform
pos += float3(translation.x, translation.y, translation.z);
scale = float3(scale.x * scaling.x, scale.y * scaling.y, scale.z * scaling.z);
rot = rot * Quat(rotation.x, rotation.y, rotation.z, rotation.w);
name = node->mName.C_Str();
i = -1; // start over!
}
}
GameObject* game_object = App->scene->CreateNewGameObject(parent, node->mName.C_Str());
ComponentTransform* component_transform = new ComponentTransform(pos, scale, rot);
game_object->components.push_back(component_transform);
game_object->SetTRS(pos, rot, scale);
// Use scene->mNumMeshes to iterate on scene->mMeshes array
for (int x = node->mNumMeshes, i = 0; x > 0; x--, i++)
{
scene->mMeshes[node->mMeshes[i]];
ComponentMesh* mesh = new ComponentMesh();
// copy vertices
mesh->num_vertices = scene->mMeshes[node->mMeshes[i]]->mNumVertices;
mesh->vertices = new float[mesh->num_vertices * 3];
memcpy(mesh->vertices, scene->mMeshes[node->mMeshes[i]]->mVertices, sizeof(float) * mesh->num_vertices * 3);
LOG("New mesh with %d vertices", mesh->num_vertices);
glGenBuffers(1, (GLuint*)&(mesh->id_vertices));
glBindBuffer(GL_ARRAY_BUFFER, mesh->id_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * mesh->num_vertices, mesh->vertices, GL_STATIC_DRAW);
// copy faces
if (scene->mMeshes[node->mMeshes[i]]->HasFaces())
{
mesh->num_indices = scene->mMeshes[node->mMeshes[i]]->mNumFaces * 3;
mesh->indices = new uint[mesh->num_indices]; // assume each face is a triangle
for (uint y = 0; y < scene->mMeshes[node->mMeshes[i]]->mNumFaces; y++)
{
if (scene->mMeshes[node->mMeshes[i]]->mFaces[y].mNumIndices != 3)
{
LOG("WARNING, geometry face with != 3 indices!");
}
else
memcpy(&mesh->indices[y * 3], scene->mMeshes[node->mMeshes[i]]->mFaces[y].mIndices, 3 * sizeof(uint));
}
}
glGenBuffers(1, (GLuint*)&(mesh->id_indices));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->id_indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(float) * mesh->num_indices, mesh->indices, GL_STATIC_DRAW);
// copy normals
if (scene->mMeshes[node->mMeshes[i]]->HasNormals())
{
mesh->normals = new float[mesh->num_vertices * 3];
memcpy(mesh->normals, scene->mMeshes[node->mMeshes[i]]->mNormals, sizeof(float) * mesh->num_vertices * 3);
glGenBuffers(1, (GLuint*)&(mesh->id_normals));
glBindBuffer(GL_ARRAY_BUFFER, mesh->id_normals);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * mesh->num_vertices, mesh->normals, GL_STATIC_DRAW);
}
// copy colors
if (scene->mMeshes[node->mMeshes[i]]->HasVertexColors(0))
{
mesh->colors = new float[mesh->num_vertices * 3];
memcpy(mesh->colors, scene->mMeshes[node->mMeshes[i]]->mColors, sizeof(float) * mesh->num_vertices * 3);
glGenBuffers(1, (GLuint*)&(mesh->id_colors));
glBindBuffer(GL_ARRAY_BUFFER, mesh->id_colors);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * mesh->num_vertices, mesh->colors, GL_STATIC_DRAW);
}
// copy texture coordinates
if (scene->mMeshes[node->mMeshes[i]]->HasTextureCoords(0))
{
mesh->texture_coordinates = new float[mesh->num_vertices * 3];
memcpy(mesh->texture_coordinates, scene->mMeshes[node->mMeshes[i]]->mTextureCoords[0], sizeof(float) * mesh->num_vertices * 3);
glGenBuffers(1, (GLuint*)&(mesh->id_texture_coordinates));
glBindBuffer(GL_ARRAY_BUFFER, mesh->id_texture_coordinates);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * mesh->num_vertices, mesh->texture_coordinates, GL_STATIC_DRAW);
}
// Generate AABB
mesh->original_bbox.SetNegativeInfinity();
mesh->original_bbox.Enclose((float3*)mesh->vertices, mesh->num_vertices);
mesh->name = (scene->mMeshes[node->mMeshes[i]]->mName.length > 0) ? scene->mMeshes[node->mMeshes[i]]->mName.C_Str() : "Unnamed";
game_object->components.push_back(mesh);
aiMaterial* material = scene->mMaterials[scene->mMeshes[node->mMeshes[i]]->mMaterialIndex];
if (material)
{
//for(unsigned int x = 0; x < material->GetTextureCount(aiTextureType_DIFFUSE); x++)
aiString path;
material->GetTexture(aiTextureType_DIFFUSE, 0, &path);
if (path.length > 0)
{
std::string real_path = "Assets/Town/";
int size = real_path.size();
real_path += path.data;
real_path.erase(size, real_path.find_last_of("\\") - size + 1);
ILuint id;
ilGenImages(1, &id);
ilBindImage(id);
ilLoadImage(real_path.c_str());
ComponentMaterial* component_material = new ComponentMaterial(ilutGLBindTexImage());
game_object->components.push_back(component_material);
}
}
}
if (node->mNumChildren != 0)
{
for (int z = 0; z < node->mNumChildren; z++)
{
RecursiveLoadGeometryFromFile(scene, node->mChildren[z], game_object);
}
}
return game_object;
}
bool ModuleGeometryLoader::LoadGeometryFromFile(const char* path, GameObject* root)
{
bool ret = true;
const aiScene* scene = aiImportFile(path, aiProcessPreset_TargetRealtime_MaxQuality);
if (scene != nullptr && scene->HasMeshes())
{
RecursiveLoadGeometryFromFile(scene, scene->mRootNode, root);
aiReleaseImport(scene);
}
else
{
LOG("Error loading scene %s", path);
ret = false;
}
return ret;
}