-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriangleMeshLoad.cpp
executable file
·217 lines (186 loc) · 4.37 KB
/
TriangleMeshLoad.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
#include "TriangleMesh.h"
#include "Console.h"
#include <algorithm>
#ifdef _WIN32
// disable useless warnings
#pragma warning(disable:4996)
#endif
void
TriangleMesh::createSingleTriangle()
{
m_normals = new Vector3[3];
m_vertices = new Vector3[3];
m_texCoords = new VectorR2[3];
m_texCoords[0].x = 0.0f;
m_texCoords[0].y = 0.0f;
m_texCoords[1].x = 1.0f;
m_texCoords[1].y = 0.0f;
m_texCoords[2].x = 0.0f;
m_texCoords[2].y = 1.0f;
m_normalIndices = new TupleI3[1];
m_vertexIndices = new TupleI3[1];
m_texCoordIndices = new TupleI3[1];
m_vertexIndices[0].x = 0;
m_vertexIndices[0].y = 1;
m_vertexIndices[0].z = 2;
m_normalIndices[0].x = 0;
m_normalIndices[0].y = 1;
m_normalIndices[0].z = 2;
m_texCoordIndices[0].x = 0;
m_texCoordIndices[0].y = 1;
m_texCoordIndices[0].z = 2;
m_numTris = 1;
}
//************************************************************************
// You probably don't want to modify the following functions
// They are for loading .obj files
//************************************************************************
bool
TriangleMesh::load(char* file, const Matrix4x4& ctm)
{
FILE *fp = fopen(file, "rb");
if (!fp)
{
error("Cannot open \"%s\" for reading\n",file);
return false;
}
debug("Loading \"%s\"...\n", file);
loadObj(fp, ctm);
debug("Loaded \"%s\" with %d triangles\n",file,m_numTris);
fclose(fp);
return true;
}
void
getIndices(char *word, int *vindex, int *tindex, int *nindex)
{
char *null = " ";
char *ptr;
char *tp;
char *np;
// by default, the texture and normal pointers are set to the null string
tp = null;
np = null;
// replace slashes with null characters and cause tp and np to point
// to character immediately following the first or second slash
for (ptr = word; *ptr != '\0'; ptr++)
{
if (*ptr == '/')
{
if (tp == null)
tp = ptr + 1;
else
np = ptr + 1;
*ptr = '\0';
}
}
*vindex = atoi (word);
*tindex = atoi (tp);
*nindex = atoi (np);
}
void
TriangleMesh::loadObj(FILE* fp, const Matrix4x4& ctm)
{
int nv=0, nt=0, nn=0, nf=0;
char line[81];
while (fgets(line, 80, fp) != 0)
{
if (line[0] == 'v')
{
if (line[1] == 'n')
nn++;
else if (line[1] == 't')
nt++;
else
nv++;
}
else if (line[0] == 'f')
{
nf++;
}
}
fseek(fp, 0, 0);
m_normals = new Vector3[std::max(nv,nf)];
m_vertices = new Vector3[nv];
if (nt)
{ // got texture coordinates
m_texCoords = new VectorR2[nt];
m_texCoordIndices = new TupleI3[nf];
}
m_normalIndices = new TupleI3[nf]; // always make normals
m_vertexIndices = new TupleI3[nf]; // always have vertices
m_numTris = 0;
int nvertices = 0;
int nnormals = 0;
int ntextures = 0;
Matrix4x4 nctm = ctm;
nctm.invert();
nctm.transpose();
while (fgets(line, 80, fp) != 0)
{
if (line[0] == 'v')
{
if (line[1] == 'n')
{
float x, y, z;
sscanf(&line[2], "%f %f %f\n", &x, &y, &z);
Vector3 n(x, y, z);
m_normals[nnormals] = nctm*n;
m_normals[nnormals].normalize();
nnormals++;
}
else if (line[1] == 't')
{
float x, y;
sscanf(&line[2], "%f %f\n", &x, &y);
m_texCoords[ntextures].x = x;
m_texCoords[ntextures].y = y;
ntextures++;
}
else
{
float x, y, z;
sscanf(&line[1], "%f %f %f\n", &x, &y, &z);
Vector3 v(x, y, z);
m_vertices[nvertices] = ctm*v;
nvertices++;
}
}
else if (line[0] == 'f')
{
char s1[32], s2[32], s3[32];
int v, t, n;
sscanf(&line[1], "%s %s %s\n", s1, s2, s3);
getIndices(s1, &v, &t, &n);
m_vertexIndices[m_numTris].x = v-1;
if (n)
m_normalIndices[m_numTris].x = n-1;
if (t)
m_texCoordIndices[m_numTris].x = t-1;
getIndices(s2, &v, &t, &n);
m_vertexIndices[m_numTris].y = v-1;
if (n)
m_normalIndices[m_numTris].y = n-1;
if (t)
m_texCoordIndices[m_numTris].y = t-1;
getIndices(s3, &v, &t, &n);
m_vertexIndices[m_numTris].z = v-1;
if (n)
m_normalIndices[m_numTris].z = n-1;
if (t)
m_texCoordIndices[m_numTris].z = t-1;
if (!n)
{ // if no normal was supplied
Vector3 e1 = m_vertices[m_vertexIndices[m_numTris].y] -
m_vertices[m_vertexIndices[m_numTris].x];
Vector3 e2 = m_vertices[m_vertexIndices[m_numTris].z] -
m_vertices[m_vertexIndices[m_numTris].x];
m_normals[nn] = cross(e1, e2);
m_normalIndices[nn].x = nn;
m_normalIndices[nn].y = nn;
m_normalIndices[nn].z = nn;
nn++;
}
m_numTris++;
} // else ignore line
}
}