/
OBJModel.cpp
380 lines (309 loc) · 10.1 KB
/
OBJModel.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "OBJModel.h"
using namespace std;
//Constructor
OBJModel::OBJModel(const char* fileName)
{
this->init();
this->load(fileName);
}
//Free memory
OBJModel::~OBJModel(void)
{
//Free all vertices and normals
delete [] this->vertices;
delete [] this->normals;
delete [] this->uvs;
delete [] this->index;
delete [] this->normalsIndex;
delete [] this->uvIndex;
delete [] this->vertexArray;
delete [] this->normalsArray;
delete [] this->uvArray;
delete [] this->indexArray;
}
//General class initialization
void OBJModel::init()
{
this->numVertices = 0;
this->numFaces = 0;
this->numUVs = 0;
this->numNormals = 0;
//Set all arrays to null
this->vertices = NULL;
this->uvs = NULL;
this->normals = NULL;
this->index = NULL;
this->uvIndex = NULL;
this->normalsIndex = NULL;
this->vertexArray = NULL;
this->normalsArray = NULL;
this->uvArray = NULL;
this->indexArray = NULL;
//VBO
this->vbo_vertices = 0;
this->vbo_normals = 0;
this->vbo_uvs = 0;
this->vbo_index = 0;
}
/**
* Load the OBJ Model into memory
**/
void OBJModel::load(const char* fileName)
{
char buffer[255];
//Open file for reading
ifstream file;
file.open(fileName);
if (!file.is_open())
{
//If file isn't open
cout << "Error loading file: " << fileName << endl;
return;
} else {
//First pass of reading the file counts the data
while ( !file.eof() ){
//Read the line
file.getline(buffer, 255);
//Read the line
switch(buffer[0]) {
//Vertex
case 'v':
switch(buffer[1]) {
case ' ':
this->numVertices++;
break;
case 'n':
this->numNormals++;
break;
case 't':
this->numUVs++;
break;
}
break;
//Face
case 'f':
this->numFaces++;
break;
default:
break;
}
} //end of file
//Echo the stats
/*
printf("Loading OBJ file\n");
printf("fileName: %s \n", fileName);
printf("Vertices: %d \n", this->numVertices);
printf("Vertex Normals: %d \n", this->numNormals);
printf("Faces: %d \n", this->numFaces);
printf("\n");
*/
//Allocate memory for data
//Init vertices
this->vertices = new Vector3*[this->numVertices];
this->index = new int*[this->numFaces];
//Init normals
this->normals = new Vector3*[this->numNormals];
this->normalsIndex = new int*[this->numFaces]; //Triangles
//Init UV's
this->uvs = new UV*[this->numUVs];
this->uvIndex = new int*[this->numFaces];
//Initialize each of the indices
for (int i=0; i<this->numFaces; i++){
this->index[i] = new int[3];
this->normalsIndex[i] = new int[3];
this->uvIndex[i] = new int[3];
for (int j=0; j<3; j++){
this->index[i][j] = 0;
this->normalsIndex[i][j] = 0;
this->uvIndex[i][j] = 0;
}
}
//Second pass of file to load data
file.clear(); //Clear eof status
file.seekg(0, ios::beg); //Move cursor to beginning
int currentVertex = 0;
int currentIndex = 0;
int currentNormal = 0;
int currentUV = 0;
int currentUVIndex = 0;
int currentNormalIndex = 0;
while (!file.eof()){
//Read line
file.getline(buffer, 255);
switch (buffer[0]){
//Vertex
case 'v':
switch(buffer[1]){
case ' ':
//Create new vertex
this->vertices[currentVertex] = new Vector3();
sscanf(buffer, "v %f %f %f", &this->vertices[currentVertex]->x, &this->vertices[currentVertex]->y, &this->vertices[currentVertex]->z);
currentVertex++;
break;
case 'n':
//Create new vertex normal
this->normals[currentNormal] = new Vector3();
sscanf(buffer, "vn %f %f %f", &this->normals[currentNormal]->x, &this->normals[currentNormal]->y, &this->normals[currentNormal]->z);
currentNormal++;
break;
case 't':
//Create new UV
this->uvs[currentUV] = new UV();
sscanf(buffer, "vt %f %f", &this->uvs[currentUV]->u, &this->uvs[currentUV]->v);
currentUV++;
break;
}
break;
case 'f':
sscanf(buffer, "f %d/%d/%d %d/%d/%d %d/%d/%d", &this->index[currentIndex][0], &this->uvIndex[currentUVIndex][0], &this->normalsIndex[currentNormalIndex][0], &this->index[currentIndex][1], &this->uvIndex[currentUVIndex][1], &this->normalsIndex[currentNormalIndex][1], &this->index[currentIndex][2], &this->uvIndex[currentUVIndex][2], &this->normalsIndex[currentNormalIndex][2]);
currentIndex++;
currentNormalIndex++;
currentUVIndex++;
break;
}
} //end of file
//Release file pointer
file.close();
}
//decrement all indices to cater for OpenGL (OBJ indices start at 1)
for(int i = 0; i < this->numFaces; i++)
{
for(int j=0; j<3; j++){
this->index[i][j]--;
this->normalsIndex[i][j]--;
this->uvIndex[i][j]--;
}
}
//Prepare the arrays for drawing
this->prepArrays();
}
/**
Takes the vertex, normals and texture coords and turns them into straight march arrays for OpenGL to render
**/
void OBJModel::prepArrays()
{
//Initialize the arrays
this->vertexArray = new GLfloat[this->numFaces *3 * 3]; //3 vertices to a face, 3 floats to a vertex
this->normalsArray = new GLfloat[this->numFaces *3 * 3];
this->indexArray = new GLuint[this->numFaces * 3];
this->uvArray = new GLfloat[this->numFaces * 3 * 2]; //3 vertices to a face, 2 floats to a UV
//Turn index into a straight march
int numElements = this->numFaces * 3;
for (int i=0; i<numElements; i++)
{
this->indexArray[i] = i;
}
//Figure out vertices, normals and UVs
for (int face = 0; face < this->numFaces; face++)
{
//Get vertex numbers
int v1, v2, v3;
v1 = this->index[face][0];
v2 = this->index[face][1];
v3 = this->index[face][2];
//Get Normal numbers
int n1, n2, n3;
n1 = this->normalsIndex[face][0];
n2 = this->normalsIndex[face][1];
n3 = this->normalsIndex[face][2];
//Get UVs
int uv1, uv2, uv3;
uv1 = this->uvIndex[face][0];
uv2 = this->uvIndex[face][1];
uv3 = this->uvIndex[face][2];
//Element 1
this->uvArray[6*face] = (GLfloat) this->uvs[uv1]->u;
this->uvArray[6*face + 1] = (GLfloat) this->uvs[uv1]->v;
this->normalsArray[9*face] = (GLfloat) this->normals[n1]->x;
this->normalsArray[9*face + 1] = (GLfloat) this->normals[n1]->y;
this->normalsArray[9*face + 2] = (GLfloat) this->normals[n1]->z;
this->vertexArray[9*face] = (GLfloat) this->vertices[v1]->x;
this->vertexArray[9*face + 1] = (GLfloat) this->vertices[v1]->y;
this->vertexArray[9*face + 2] = (GLfloat) this->vertices[v1]->z;
//Element 2
this->uvArray[6*face + 2] = (GLfloat) this->uvs[uv2]->u;
this->uvArray[6*face + 3] = (GLfloat) this->uvs[uv2]->v;
this->normalsArray[9*face + 3] = (GLfloat) this->normals[n2]->x;
this->normalsArray[9*face + 4] = (GLfloat) this->normals[n2]->y;
this->normalsArray[9*face + 5] = (GLfloat) this->normals[n2]->z;
this->vertexArray[9*face + 3] = (GLfloat) this->vertices[v2]->x;
this->vertexArray[9*face + 4] = (GLfloat) this->vertices[v2]->y;
this->vertexArray[9*face + 5] = (GLfloat) this->vertices[v2]->z;
//Element 3
this->uvArray[6*face + 4] = (GLfloat) this->uvs[uv3]->u;
this->uvArray[6*face + 5] = (GLfloat) this->uvs[uv3]->v;
this->normalsArray[9*face + 6] = (GLfloat)this->normals[n3]->x;
this->normalsArray[9*face + 7] = (GLfloat)this->normals[n3]->y;
this->normalsArray[9*face + 8] = (GLfloat)this->normals[n3]->z;
this->vertexArray[9*face + 6] = (GLfloat)this->vertices[v3]->x;
this->vertexArray[9*face + 7] = (GLfloat)this->vertices[v3]->y;
this->vertexArray[9*face + 8] = (GLfloat)this->vertices[v3]->z;
}
//Release all the object memory - we only need the arrays
delete [] this->vertices;
delete [] this->normals;
delete [] this->uvs;
delete [] this->index;
delete [] this->normalsIndex;
delete [] this->uvIndex;
//this->debugArrays();
//Check vertex buffer object extension
if (glewGetExtension("GL_ARB_vertex_buffer_object"))
{
//cout << "Vertex buffer objects supported!" << endl;
//Load vertices
glGenBuffers(1, &this->vbo_vertices); //Get name/ID
glBindBuffer(GL_ARRAY_BUFFER, this->vbo_vertices); //Bind the buffer to the name
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*this->numFaces*3*3, this->vertexArray, GL_STATIC_DRAW);
//Load normals
glGenBuffers(1, &this->vbo_normals); //Get name/ID
glBindBuffer(GL_ARRAY_BUFFER, this->vbo_normals); //Bind the buffer to the name
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*this->numFaces*3*3, this->normalsArray, GL_STATIC_DRAW);
//Load UVs
glGenBuffers(1, &this->vbo_uvs); //Get name/ID
glBindBuffer(GL_ARRAY_BUFFER, this->vbo_uvs); //Bind the buffer to the name
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*this->numFaces*3*2, this->uvArray, GL_STATIC_DRAW);
//Load index
glGenBuffers(1, &this->vbo_index); //Get name/ID
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->vbo_index); //Bind the buffer to the name
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*this->numFaces*3, this->indexArray, GL_STATIC_DRAW_ARB);
//Clear bind buffers
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
//cout << "Loaded model into GPU memory" << endl;
}
}
//Show all the array data
void OBJModel::debugArrays()
{
FILE *file = fopen("debug.txt", "w");
if (file == NULL)
{
printf("Couldn't open debug file.");
return;
}
fprintf(file, "Element Index: \n");
for (int face=0; face < this->numFaces; face++)
{
fprintf(file, "%d %d %d \n", this->indexArray[3*face], this->indexArray[3*face + 1], this->indexArray[3*face + 2]);
}
fprintf(file, "\nVertices: \n");
for (int face=0; face < this->numFaces; face++)
{
fprintf(file, "%f %f %f %f %f %f %f %f %f \n",
this->vertexArray[3*face], this->vertexArray[3*face + 1], this->vertexArray[3*face + 2],
this->vertexArray[3*face+3], this->vertexArray[3*face + 4], this->vertexArray[3*face + 5],
this->vertexArray[3*face+6], this->vertexArray[3*face + 7], this->vertexArray[3*face + 8]
);
}
fprintf(file, "\nNormals: \n");
for (int face=0; face < this->numFaces; face++)
{
fprintf(file, "%f %f %f %f %f %f %f %f %f \n",
this->normalsArray[3*face], this->normalsArray[3*face + 1], this->normalsArray[3*face + 2],
this->normalsArray[3*face+3], this->normalsArray[3*face + 4], this->normalsArray[3*face + 5],
this->normalsArray[3*face+6], this->normalsArray[3*face + 7], this->normalsArray[3*face + 8]
);
}
fclose(file);
}