This repository has been archived by the owner on Aug 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjModel.cpp
445 lines (373 loc) · 12.6 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include <gl/glew.h>
#include "ObjModel.h"
#include "Texture.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <glm.hpp>
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
std::string replace(std::string str, std::string toReplace, std::string replacement)
{
size_t index = 0;
while (true)
{
index = str.find(toReplace, index);
if (index == std::string::npos)
break;
str.replace(index, toReplace.length(), replacement);
++index;
}
return str;
}
std::vector<std::string> split(std::string str, std::string sep)
{
std::vector<std::string> ret;
size_t index;
while(true)
{
index = str.find(sep);
if(index == std::string::npos)
break;
ret.push_back(str.substr(0, index));
str = str.substr(index+1);
}
ret.push_back(str);
return ret;
}
inline std::string toLower(std::string data)
{
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
return data;
}
glm::vec4 calcTangentVector(
glm::vec3 pos1, glm::vec3 pos2, glm::vec3 pos3,
glm::vec2 texCoord1, glm::vec2 texCoord2, glm::vec2 texCoord3, glm::vec3 normal)
{
// Given the 3 vertices (position and texture coordinates) of a triangle
// calculate and return the triangle's tangent vector.
// Create 2 vectors in object space.
//
// edge1 is the vector from vertex positions pos1 to pos2.
// edge2 is the vector from vertex positions pos1 to pos3.
glm::vec3 edge1(pos2-pos1);//Vector3 edge1(pos2[0] - pos1[0], pos2[1] - pos1[1], pos2[2] - pos1[2]);
glm::vec3 edge2(pos3-pos1);//Vector3 edge2(pos3[0] - pos1[0], pos3[1] - pos1[1], pos3[2] - pos1[2]);
edge1 = glm::normalize(edge1);
edge2 = glm::normalize(edge2);
// Create 2 vectors in tangent (texture) space that point in the same
// direction as edge1 and edge2 (in object space).
//
// texEdge1 is the vector from texture coordinates texCoord1 to texCoord2.
// texEdge2 is the vector from texture coordinates texCoord1 to texCoord3.
glm::vec2 texEdge1(texCoord2 - texCoord1);
glm::vec2 texEdge2(texCoord3 - texCoord1);
texEdge1 = glm::normalize(texEdge1);
texEdge2 = glm::normalize(texEdge2);
// These 2 sets of vectors form the following system of equations:
//
// edge1 = (texEdge1.x * tangent) + (texEdge1.y * bitangent)
// edge2 = (texEdge2.x * tangent) + (texEdge2.y * bitangent)
//
// Using matrix notation this system looks like:
//
// [ edge1 ] [ texEdge1.x texEdge1.y ] [ tangent ]
// [ ] = [ ] [ ]
// [ edge2 ] [ texEdge2.x texEdge2.y ] [ bitangent ]
//
// The solution is:
//
// [ tangent ] 1 [ texEdge2.y -texEdge1.y ] [ edge1 ]
// [ ] = ------- [ ] [ ]
// [ bitangent ] det A [-texEdge2.x texEdge1.x ] [ edge2 ]
//
// where:
// [ texEdge1.x texEdge1.y ]
// A = [ ]
// [ texEdge2.x texEdge2.y ]
//
// det A = (texEdge1.x * texEdge2.y) - (texEdge1.y * texEdge2.x)
//
// From this solution the tangent space basis vectors are:
//
// tangent = (1 / det A) * ( texEdge2.y * edge1 - texEdge1.y * edge2)
// bitangent = (1 / det A) * (-texEdge2.x * edge1 + texEdge1.x * edge2)
// normal = cross(tangent, bitangent)
glm::vec3 t;
glm::vec3 b;
float det = (texEdge1[0] * texEdge2[1]) - (texEdge1[1] * texEdge2[0]);
if (det == 0)
{
t = glm::vec3(1.0f, 0.0f, 0.0f);
b = glm::vec3(0.0f, 1.0f, 0.0f);
}
else
{
det = 1.0f / det;
t[0] = (texEdge2[1] * edge1[0] - texEdge1[1] * edge2[0]) * det;
t[1] = (texEdge2[1] * edge1[1] - texEdge1[1] * edge2[0]) * det;
t[2] = (texEdge2[1] * edge1[2] - texEdge1[1] * edge2[0]) * det;
b[0] = (-texEdge2[0] * edge1[0] + texEdge1[0] * edge2[0]) * det;
b[1] = (-texEdge2[0] * edge1[1] + texEdge1[0] * edge2[1]) * det;
b[2] = (-texEdge2[0] * edge1[2] + texEdge1[0] * edge2[2]) * det;
t = normalize(t);
b = normalize(b);
}
// Calculate the handedness of the local tangent space.
// The bitangent vector is the cross product between the triangle face
// normal vector and the calculated tangent vector. The resulting bitangent
// vector should be the same as the bitangent vector calculated from the
// set of linear equations above. If they point in different directions
// then we need to invert the cross product calculated bitangent vector. We
// store this scalar multiplier in the tangent vector's 'w' component so
// that the correct bitangent vector can be generated in the normal mapping
// shader's vertex shader.
glm::vec3 bitangent = glm::cross(normal, t);
float handedness = (glm::dot(bitangent, b) < 0.0f) ? -1.0f : 1.0f;
return glm::vec4(t[0], t[1], t[2], handedness);
}
ObjModel::ObjModel(std::string fileName)
{
std::string dirName = fileName;
if(dirName.rfind("/") != std::string::npos)
dirName = dirName.substr(0, dirName.rfind("/"));
if(dirName.rfind("\\") != std::string::npos)
dirName = dirName.substr(0, dirName.rfind("\\"));
if(fileName == dirName)
dirName = "";
std::ifstream pFile(fileName.c_str());
if (!pFile.is_open())
{
std::cout << "Error opening " << fileName << std::endl;
return;
}
std::vector<float> vertices;
std::vector<float> normals;
std::vector<float> texcoords;
std::vector<float> finalVertices;
ObjGroup* currentGroup = new ObjGroup();
currentGroup->end = -1;
currentGroup->start = 0;
currentGroup->materialIndex = -1;
while(!pFile.eof())
{
std::string line;
std::getline(pFile, line);
line = replace(line, "\t", " ");
while(line.find(" ") != std::string::npos)
line = replace(line, " ", " ");
if(line == "")
continue;
if(line[0] == ' ')
line = line.substr(1);
if(line == "")
continue;
if(line[line.length()-1] == ' ')
line = line.substr(0, line.length()-1);
if(line == "")
continue;
if(line[0] == '#')
continue;
std::vector<std::string> params = split(line, " ");
params[0] = toLower(params[0]);
if(params[0] == "v")
{
vertices.push_back((float)atof(params[1].c_str()));
vertices.push_back((float)atof(params[2].c_str()));
vertices.push_back((float)atof(params[3].c_str()));
}
else if(params[0] == "vn")
{
normals.push_back((float)atof(params[1].c_str()));
normals.push_back((float)atof(params[2].c_str()));
normals.push_back((float)atof(params[3].c_str()));
}
else if(params[0] == "vt")
{
texcoords.push_back((float)atof(params[1].c_str()));
texcoords.push_back((float)atof(params[2].c_str()));
}
else if(params[0] == "f")
{
std::vector<std::string> indices1 = split(params[1], "/");
std::vector<std::string> indices2 = split(params[2], "/");
std::vector<std::string> indices3 = split(params[3], "/");
glm::vec3 p1(vertices[(atoi(indices1[0].c_str())-1)*3+0],vertices[(atoi(indices1[0].c_str())-1)*3+1],vertices[(atoi(indices1[0].c_str())-1)*3+2]);
glm::vec3 p2(vertices[(atoi(indices2[0].c_str())-1)*3+0],vertices[(atoi(indices2[0].c_str())-1)*3+1],vertices[(atoi(indices2[0].c_str())-1)*3+2]);
glm::vec3 p3(vertices[(atoi(indices3[0].c_str())-1)*3+0],vertices[(atoi(indices3[0].c_str())-1)*3+1],vertices[(atoi(indices3[0].c_str())-1)*3+2]);
glm::vec2 t1(texcoords[(atoi(indices1[1].c_str())-1)*2+0],texcoords[(atoi(indices1[1].c_str())-1)*2+1]);
glm::vec2 t2(texcoords[(atoi(indices2[1].c_str())-1)*2+0],texcoords[(atoi(indices2[1].c_str())-1)*2+1]);
glm::vec2 t3(texcoords[(atoi(indices3[1].c_str())-1)*2+0],texcoords[(atoi(indices3[1].c_str())-1)*2+1]);
glm::vec3 n(normals[(atoi(indices1[2].c_str())-1)*3+0],normals[(atoi(indices1[2].c_str())-1)*3+1],normals[(atoi(indices1[2].c_str())-1)*3+2]);
glm::vec4 tangent(calcTangentVector(p1,p2,p3,t1,t2,t3,n));
for(size_t ii = 4; ii <= params.size(); ii++)
{
for(size_t i = ii-3; i < ii; i++)
{
std::vector<std::string> indices = split(params[i == (ii-3) ? 1 : i], "/");
glm::vec3 p;
glm::vec2 t;
glm::vec3 n;
float px = 0, py = 0, pz = 0, tx = 0, ty = 0, nx = 0, ny = 0, nz = 0;
if(indices.size() >= 1)
{
p[0] = vertices[(atoi(indices[0].c_str())-1) * 3+0];
p[1] = vertices[(atoi(indices[0].c_str())-1) * 3+1];
p[2] = vertices[(atoi(indices[0].c_str())-1) * 3+2];
}
if(indices.size() == 2) //texture
{
t[0] = texcoords[(atoi(indices[1].c_str())-1) * 2+0];
t[1] = texcoords[(atoi(indices[1].c_str())-1) * 2+1];
}
if(indices.size() == 3)
{
if( indices[1] != "")
{
t[0] = texcoords[(atoi(indices[1].c_str())-1) * 2+0];
t[1] = texcoords[(atoi(indices[1].c_str())-1) * 2+1];
}
n[0] = normals[(atoi(indices[2].c_str())-1) * 3+0];
n[1] = normals[(atoi(indices[2].c_str())-1) * 3+1];
n[2] = normals[(atoi(indices[2].c_str())-1) * 3+2];
}
finalVertices.push_back(p[0]);
finalVertices.push_back(p[1]);
finalVertices.push_back(p[2]);
finalVertices.push_back(n[0]);
finalVertices.push_back(n[1]);
finalVertices.push_back(n[2]);
finalVertices.push_back(t[0]);
finalVertices.push_back(t[1]);
finalVertices.push_back(tangent[0]);
finalVertices.push_back(tangent[1]);
finalVertices.push_back(tangent[2]);
finalVertices.push_back(tangent[3]);
currentGroup->end = finalVertices.size() / 12;
}
}
}
else if(params[0] == "s")
{
}
else if(params[0] == "mtllib")
{
loadMaterialFile(dirName + "/" + params[1], dirName);
}
else if(params[0] == "usemtl")
{
if(currentGroup->end != -1)
groups.push_back(currentGroup);
currentGroup = new ObjGroup();
currentGroup->start = finalVertices.size()/12;
currentGroup->materialIndex = -1;
for(size_t i = 0; i < materials.size(); i++)
{
MaterialInfo* info = materials[i];
if(info->name == params[1])
{
currentGroup->materialIndex = i;
break;
}
}
if(currentGroup->materialIndex == -1)
std::cout<<"Could not find material name "<<params[1]<<std::endl;
}
//else
// std::cout<<line<<std::endl;
}
groups.push_back(currentGroup);
glGenVertexArrays(1, &_vertexArray);
glBindVertexArray(_vertexArray);
GLuint _vertexBuffer;
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, finalVertices.size()*sizeof(GLfloat), &finalVertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 48, BUFFER_OFFSET(0));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 48, BUFFER_OFFSET(12));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 48, BUFFER_OFFSET(24));
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 48, BUFFER_OFFSET(32));
glBindVertexArray(0);
}
ObjModel::~ObjModel(void)
{
}
void ObjModel::draw()
{
glBindVertexArray(_vertexArray);
for(size_t i = 0; i < groups.size(); i++)
{
ObjGroup* group = groups[i];
MaterialInfo* material = materials[group->materialIndex];
if(material->hasTexture)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, material->texture->textureId);
}
if(material->bumpMap != NULL)
{
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, material->bumpMap->textureId);
}
glDrawArrays(GL_TRIANGLES, group->start, group->end - group->start);
}
glBindVertexArray(0);
}
void ObjModel::loadMaterialFile( std::string fileName, std::string dirName )
{
std::ifstream pFile(fileName.c_str());
MaterialInfo* currentMaterial = NULL;
while(!pFile.eof())
{
std::string line;
std::getline(pFile, line);
line = replace(line, "\t", " ");
while(line.find(" ") != std::string::npos)
line = replace(line, " ", " ");
if(line == "")
continue;
if(line[0] == ' ')
line = line.substr(1);
if(line == "")
continue;
if(line[line.length()-1] == ' ')
line = line.substr(0, line.length()-1);
if(line == "")
continue;
if(line[0] == '#')
continue;
std::vector<std::string> params = split(line, " ");
params[0] = toLower(params[0]);
if(params[0] == "newmtl")
{
if(currentMaterial != NULL)
{
materials.push_back(currentMaterial);
}
currentMaterial = new MaterialInfo();
currentMaterial->name = params[1];
}
else if(params[0] == "map_kd")
{
currentMaterial->hasTexture = true;
currentMaterial->texture = new Texture(dirName + "/" + params[1]);
}
else if(params[0] == "map_bump")
{
currentMaterial->bumpMap = new Texture(dirName + "/" + params[1]);
}
else
std::cout<<"Didn't parse "<<params[0]<<" in material file"<<std::endl;
}
if(currentMaterial != NULL)
materials.push_back(currentMaterial);
}
ObjModel::MaterialInfo::MaterialInfo()
{
texture = NULL;
bumpMap = NULL;
hasTexture = false;
}