/
OBJLoader.cpp
235 lines (193 loc) · 6.57 KB
/
OBJLoader.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
#include "OBJLoader.h"
OBJLoader::OBJLoader(const char* filename) {
Assimp::Importer importer;
//Initialize DevIL
devilStartUp();
const aiScene* scene=importer.ReadFile(filename, aiProcess_GenSmoothNormals|aiProcess_Triangulate|aiProcess_CalcTangentSpace|aiProcess_FlipUVs);
if(scene->mFlags==AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
fprintf( stderr, "Couldn't load model, Error Importing Asset" );
return;
}
recursiveProcess(scene->mRootNode, scene);
}
OBJLoader::~OBJLoader()
{
for(std::size_t i=0; i<meshes.size(); i++)
delete meshes[i];
}
/* Initializes the third party softwares
* @param void
* @return void
*/
void OBJLoader::devilStartUp() {
ilutRenderer(ILUT_OPENGL);
ilInit();
iluInit();
ilutInit();
ilutRenderer(ILUT_OPENGL);
}
/* Recursively processes the meshes of the model
*/
void OBJLoader::recursiveProcess(aiNode* node, const aiScene* scene) {
//process
for(std::size_t i=0; i<node->mNumMeshes;i++)
{
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
processMesh(mesh, scene);
}
//recursion
for(std::size_t i=0; i<node->mNumChildren; i++)
{
recursiveProcess(node->mChildren[i], scene);
}
}
/* Processes the mesh information
*/
void OBJLoader::processMesh(aiMesh* cmesh, const aiScene* scene) {
vector<vertexData> data;
vector<unsigned int> indices;
vector<textureData> textures;
aiColor4D col;
aiMaterial* mat = scene->mMaterials[cmesh->mMaterialIndex];
aiGetMaterialColor(mat, AI_MATKEY_COLOR_DIFFUSE, &col);
glm::vec3 defaultColor(col.r, col.g, col.b);
for(std::size_t i=0; i<cmesh->mNumVertices; i++)
{
vertexData tmp;
//position
tmp.position = glm::vec3(cmesh->mVertices[i].x, cmesh->mVertices[i].y, cmesh->mVertices[i].z);
//normals
tmp.normal = glm::vec3(cmesh->mNormals[i].x, cmesh->mNormals[i].y, cmesh->mNormals[i].z);
//tangents
if(cmesh->mTangents)
tmp.tangent = glm::vec3(cmesh->mTangents[i].x, cmesh->mTangents[i].y, cmesh->mTangents[i].z);
else
tmp.tangent = glm::vec3(1,0,0);
//colors
if(cmesh->mColors[0])
tmp.color = glm::vec3(cmesh->mColors[0][i].r, cmesh->mColors[0][i].g, cmesh->mColors[0][i].b);
else
tmp.color = defaultColor;
//UVs
if(cmesh->mTextureCoords[0])
tmp.UV = glm::vec2(cmesh->mTextureCoords[0][i].x, cmesh->mTextureCoords[0][i].y);
else
tmp.UV = glm::vec2(0,0);
data.push_back(tmp);
}
for(std::size_t i=0; i<cmesh->mNumFaces; i++)
{
aiFace face = cmesh->mFaces[i];
for(std::size_t j=0; j<face.mNumIndices;j++) //0...2
{
indices.push_back(face.mIndices[j]);
}
}
//handle textures
for(std::size_t i=0; i<mat->GetTextureCount(aiTextureType_DIFFUSE); i++)
{
aiString str;
mat->GetTexture(aiTextureType_DIFFUSE, i, &str);
textureData tmp;
tmp.id = loadTexture(str.C_Str());
tmp.type = 0; //Index for Diffuse Map
textures.push_back(tmp);
}
meshes.push_back(new mesh(&data, &indices, &textures));
}
/* This function is used by loadTexture(const char* filename) to get the filepath
* @param The name of the file
* @return the path to the file
*/
string filepath(const char* filename) {
string file = filename;
string path;
if (file.compare("wolf") == 0) {
path = "Models/wolf/" + file;
}
return path;
}
/* This function loads the texture information from the model file
* @param the name of the model file
* @return the textureID
*/
unsigned int OBJLoader::loadTexture(const char* filename) {
ILuint imageID; // Create an image ID as a ULuint
GLuint textureID; // Create a texture ID as a GLuint
ILboolean success; // Create a flag to keep track of success/failure
ILenum error; // Create a flag to keep track of the IL error state
ilGenImages(1, &imageID); // Generate the image ID
ilBindImage(imageID); // Bind the image
std::stringstream sstm;
// Load image from file into filename
sstm << "Models/wolf/" << filename;
success = ilLoadImage((const ILstring) &sstm.str()[0]); // Load the image file
// If we managed to load the image, then we can start to do things with it...
if (success)
{
// If the image is flipped (i.e. upside-down and mirrored, flip it the right way up!)
/* ILinfo ImageInfo;
iluGetImageInfo(&ImageInfo);
if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT)
{
iluFlipImage();
}*/
// Convert the image into a suitable format to work with
// NOTE: If your image contains alpha channel you can replace IL_RGB with IL_RGBA
success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
// Quit out if we failed the conversion
if (!success)
{
error = ilGetError();
std::cout << "Image conversion failed - IL reports error: " << error << " - " << iluErrorString(error) << std::endl;
exit(-1);
}
// Generate a new texture
glGenTextures(1, &textureID);
// Bind the texture to a name
glBindTexture(GL_TEXTURE_2D, textureID);
// Set texture clamping method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// Set texture interpolation method to use linear interpolation (no MIPMAPS)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Specify the texture specification
glTexImage2D(GL_TEXTURE_2D, // Type of texture
0, // Pyramid level (for mip-mapping) - 0 is the top level
ilGetInteger(IL_IMAGE_FORMAT), // Internal pixel format to use. Can be a generic type like GL_RGB or GL_RGBA, or a sized type
ilGetInteger(IL_IMAGE_WIDTH), // Image width
ilGetInteger(IL_IMAGE_HEIGHT), // Image height
0, // Border width in pixels (can either be 1 or 0)
ilGetInteger(IL_IMAGE_FORMAT), // Format of image pixel data
GL_UNSIGNED_BYTE, // Image data type
ilGetData()); // The actual image data itself
}
else // If we failed to open the image file in the first place...
{
error = ilGetError();
std::cout << "Image load failed - IL reports error: " << error << " - " << iluErrorString(error) << std::endl;
exit(-1);
}
ilDeleteImages(1, &imageID); // Because we have already copied image data into texture data we can release memory used by image.
std::cout << "Texture creation successful." << std::endl;
return textureID; // Return the GLuint to the texture so you can use it!
}
/* Draws the model
* @param the shader's ProgramID
* @return void
*/
void OBJLoader::draw(unsigned int programId)
{
for(std::size_t i=0; i<meshes.size();i++)
meshes[i]->draw(programId);
}
/* Simple getter function for the mesh vector
* @param void
* @return a reference to the vector of meshes
*/
vector<mesh*>& OBJLoader::getMeshes()
{
return meshes;
}