Repository navigation
Expand file tree
/
Copy pathMeshSupport.cpp
More file actions
282 lines (221 loc) · 6.75 KB
/
Copy pathMeshSupport.cpp
File metadata and controls
282 lines (221 loc) · 6.75 KB
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
//------------------------------------------------------------------------------
// Purpose: Quick and dirty mesh support
// Author: Andrew Willmott
//------------------------------------------------------------------------------
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include "MeshSupport.hpp"
#include <assert.h>
#include <float.h>
#include <stdio.h>
#ifdef _MSC_VER
#pragma warning (disable: 4244)
#define strcasecmp _stricmp
#define strtok_r strtok_s
#endif
using namespace MSL;
namespace
{
void InPlaceTriangulate(int numVerts, std::vector<int>& indices)
{
// Assume polygon of numVerts indices at the end of 'indices', and triangulate it in place.
int baseVertexIndex = (int) indices.size() - numVerts;
int baseEltIndex = indices[baseVertexIndex];
for (int i = 1; i < numVerts - 2; i++)
{
indices.insert(indices.begin() + baseVertexIndex + 3 * i, indices[baseVertexIndex + 3 * i - 1]);
indices.insert(indices.begin() + baseVertexIndex + 3 * i, baseEltIndex);
}
}
bool FaceCommand(cMesh* mesh, int argc, const char* va[])
{
argc--;
va++;
char* end;
for (int i = 0; i < argc; i++)
{
long ip = strtol(va[i], &end, 10);
if (end != va[i])
mesh->mPositionIndices.push_back(int(ip) - 1);
if (end[0] == '/')
{
const char* next = end + 1;
long it = strtol(next, &end, 10);
if (end != next)
mesh->mUVIndices.push_back(int(it) - 1);
}
if (end[0] == '/')
{
const char* next = end + 1;
long in = strtol(end + 1, &end, 10);
if (end != next)
mesh->mNormalIndices.push_back(int(in) - 1);
}
}
// quick and dirty in-place triangulation.
if (argc > 3)
{
InPlaceTriangulate(argc, mesh->mPositionIndices);
if (!mesh->mNormalIndices.empty())
InPlaceTriangulate(argc, mesh->mNormalIndices);
if (!mesh->mUVIndices.empty())
InPlaceTriangulate(argc, mesh->mUVIndices);
}
return true;
}
bool PositionCommand(cMesh* mesh, int argc, const char* va[])
{
if (argc < 4)
return false;
Vec3f p;
p.x = atof(va[1]);
p.y = atof(va[2]);
p.z = atof(va[3]);
mesh->mPositions.push_back(p);
return true;
}
bool NormalCommand(cMesh* mesh, int argc, const char* va[])
{
if (argc < 4)
return false;
Vec3f p;
p.x = atof(va[1]);
p.y = atof(va[2]);
p.z = atof(va[3]);
mesh->mNormals.push_back(p);
return true;
}
bool TexCoordCommand(cMesh* mesh, int argc, const char* va[])
{
if (argc < 3)
return false;
Vec2f p;
p.x = atof(va[1]);
p.y = atof(va[2]);
mesh->mUVs.push_back(p);
return true;
}
bool ObjectCommand(cMesh*, int, const char**)
{
return true;
}
bool GroupCommand(cMesh*, int, const char**)
{
return true;
}
bool SmoothingGroupCommand(cMesh*, int, const char**)
{
return true;
}
bool MaterialCommand(cMesh*, int, const char**)
{
return true;
}
bool MaterialLibraryCommand(cMesh*, int, const char**)
{
return true;
}
bool ProcessObjCommand(cMesh* mesh, int argc, const char* argv[])
{
assert(argc > 0);
switch (argv[0][0])
{
case 'f':
return FaceCommand(mesh, argc, argv);
case 'v':
if (argv[0][1] == 'n')
return NormalCommand(mesh, argc, argv);
else if (argv[0][1] == 't')
return TexCoordCommand(mesh, argc, argv);
else if (argv[0][1] == 0)
return PositionCommand(mesh, argc, argv);
break;
case 'o':
return ObjectCommand(mesh, argc, argv);
case 'g':
return GroupCommand(mesh, argc, argv);
case 's':
return SmoothingGroupCommand(mesh, argc, argv);
case 'u':
if (strcasecmp(argv[0], "usemtl") == 0)
return MaterialCommand(mesh, argc, argv);
break;
case 'm':
if (strcasecmp(argv[0], "mtllib") == 0)
return MaterialLibraryCommand(mesh, argc, argv);
break;
case '#':
return true;
}
return false;
}
int Split(char* buffer, int maxArgs, const char** argv)
{
char* last = 0;
maxArgs--; // always reserve the last spot for 0 terminator
for (int argc = 0; argc < maxArgs; argc++)
{
argv[argc] = strtok_r(buffer, " \t\n\r", &last);
buffer = 0;
if (!argv[argc] || !argv[argc][0])
return argc;
}
fprintf(stderr, "Warning: ignored arguments past %d\n", maxArgs - 1);
argv[maxArgs] = 0;
return maxArgs;
}
}
bool MSL::ReadObjFile(FILE* file, cMesh* mesh)
{
*mesh = cMesh();
const int kMaxArgs = 256;
const char* argv[kMaxArgs];
char lineBuffer[1024];
while (fgets(lineBuffer, 1024, file))
{
int argc = Split(lineBuffer, kMaxArgs, argv);
if (argc > 0)
{
if (!ProcessObjCommand(mesh, argc, argv))
{
fprintf(stderr, "Can't parse command: '%s'\n", argv[0]);
return false;
}
}
}
printf("Read %zu positions, %zu normals, %zu uvs\n",
mesh->mPositions.size(),
mesh->mNormals.size(),
mesh->mUVs.size()
);
printf(" %zd position indices, %zd normal indices, %zd uv indices\n",
mesh->mPositionIndices.size(),
mesh->mNormalIndices.size(),
mesh->mUVIndices.size()
);
return true;
}
Bounds3 MSL::FindBounds(const cMesh& mesh)
{
Bounds3 bbox = { { +FLT_MAX, +FLT_MAX, +FLT_MAX }, { -FLT_MAX, -FLT_MAX, -FLT_MAX } };
for (int i = 0, n = (int) mesh.mPositions.size(); i < n; i++)
{
Vec3f p = mesh.mPositions[i];
if (bbox.mMin.x > p.x)
bbox.mMin.x = p.x;
else
if (bbox.mMax.x < p.x)
bbox.mMax.x = p.x;
if (bbox.mMin.y > p.y)
bbox.mMin.y = p.y;
else
if (bbox.mMax.y < p.y)
bbox.mMax.y = p.y;
if (bbox.mMin.z > p.z)
bbox.mMin.z = p.z;
else
if (bbox.mMax.z < p.z)
bbox.mMax.z = p.z;
}
return bbox;
}