-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathobjloader.cpp
115 lines (104 loc) · 3.4 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
/*
* mtllib cube.mtl
* o cube
*
* v -1.000000 -1.000000 1.000000
* v 1.000000 -1.000000 1.000000
* v -1.000000 1.000000 1.000000
* v 1.000000 1.000000 1.000000
* v -1.000000 1.000000 -1.000000
* v 1.000000 1.000000 -1.000000
* v -1.000000 -1.000000 -1.000000
* v 1.000000 -1.000000 -1.000000
*
* vt 1.000000 0.000000
* vt 0.000000 0.000000
* vt 1.000000 1.000000
* vt 0.000000 1.000000
*
* vn 0.000000 0.000000 1.000000
* vn 0.000000 1.000000 0.000000
* vn 0.000000 0.000000 -1.000000
* vn 0.000000 -1.000000 0.000000
* vn 1.000000 0.000000 0.000000
* vn -1.000000 0.000000 0.000000
*
* g cube
* usemtl cube
* s 1
* f 1/1/1 2/2/1 3/3/1
* f 3/3/1 2/2/1 4/4/1
* s 2
* f 3/1/2 4/2/2 5/3/2
* f 5/3/2 4/2/2 6/4/2
* s 3
* f 5/4/3 6/3/3 7/2/3
* f 7/2/3 6/3/3 8/1/3
* s 4
* f 7/1/4 8/2/4 1/3/4
* f 1/3/4 8/2/4 2/4/4
* s 5
* f 2/1/5 8/2/5 4/3/5
* f 4/3/5 8/2/5 6/4/5
* s 6
* f 7/1/6 1/2/6 5/3/6
* f 5/3/6 1/2/6 3/4/6
*/
#include "objloader.h"
#include "tex2.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
OBJLoader::OBJLoader(const std::string& filename)
{
FILE* file = fopen(filename.c_str(), "r");
if (!file)
{
std::cout << "!!! OBJLoader: unable to open file " << filename << std::endl;
std::cout << "Have you updated ASSETS_DIR in window.cpp?" << std::endl;
exit(-1);
}
// store the texture coordinates from the vt section
std::vector<Tex2> texCoords;
char line[1024];
while (fgets(line, 1024, file))
{
// vertex data
if (strncmp(line, "v ", 2) == 0)
{
Vec3d vertex;
sscanf(line, "v %f %f %f", &vertex.x, &vertex.y, &vertex.z); // v -1.000000 -1.000000 1.000000
_mesh.vertices.push_back(vertex);
}
// texture coordinates info
if (strncmp(line, "vt ", 3) == 0)
{
Tex2 texCoord;
sscanf(line, "vt %f %f", &texCoord.u, &texCoord.v);
texCoords.push_back(texCoord);
}
/* f 5/4/3 6/3/3 7/2/3
* - each one of these 3 sections describe the vertexIdx/texCoordIdx/NormalIdx for one vertex of the triangular face
* - number 5: the first number. Its the 5th vertex index (stored in the v section) for the first vertex of this triangular face
* - number 4: the second number. Its the 4th texture coord index (stored in the vt section) associated to this vertex
*/
if (strncmp(line, "f ", 2) == 0)
{
int vertexIdx[3];
int textureIdx[3];
int normalsIdx[3];
sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d", &vertexIdx[0], &textureIdx[0], &normalsIdx[0], // f 1/1/1 2/2/1 3/3/1
&vertexIdx[1], &textureIdx[1], &normalsIdx[1],
&vertexIdx[2], &textureIdx[2], &normalsIdx[2]);
// store the 3 vertices of the face and its associated texture coord info
Face face(vertexIdx[0]-1, vertexIdx[1]-1, vertexIdx[2]-1,
texCoords[textureIdx[0]-1], texCoords[textureIdx[1]-1], texCoords[textureIdx[2]-1],
0xFFFFFFFF);
_mesh.faces.push_back(face);
}
}
}
Mesh OBJLoader::mesh()
{
return _mesh;
}