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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
/*
* Copyright (c) 2014-2017, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#include "mesh.h"
#include <vector>
struct Mesh
{
std::vector<MeshVertex> m_vertices;
std::vector<MeshUint> m_indices;
float m_bounds[6] = { 0.f, 0.f, 0.f, 0.f, 0.f, 0.f };
MeshContext* m_context = nullptr;
MeshIndexBuffer* m_indexBuffer = nullptr;
MeshVertexBuffer* m_vertexBuffer = nullptr;
Mesh() {}
void loadFromPLY(const char* filename);
void computeBounds();
void normalize();
};
Mesh* MeshCreate(MeshContext* context)
{
Mesh* mesh = new Mesh;
mesh->m_context = context;
return mesh;
}
void MeshLoadFromFile(Mesh* mesh, const char* filename)
{
MeshIndexBufferRelease(mesh->m_indexBuffer);
MeshVertexBufferRelease(mesh->m_vertexBuffer);
mesh->loadFromPLY(filename);
mesh->computeBounds();
mesh->normalize();
mesh->m_vertexBuffer = MeshVertexBufferCreate(mesh->m_context, &mesh->m_vertices[0], (MeshUint)mesh->m_vertices.size());
mesh->m_indexBuffer = MeshIndexBufferCreate(mesh->m_context, &mesh->m_indices[0], (MeshUint)mesh->m_indices.size());
}
void MeshGetData(Mesh* mesh, MeshData* data)
{
data->numVertices = (MeshUint) mesh->m_vertices.size();
data->positions = &mesh->m_vertices[0].x;
data->positionStride = sizeof(MeshVertex);
data->normals = &mesh->m_vertices[0].nx;
data->normalStride = sizeof(MeshVertex);
data->numIndices = (MeshUint) mesh->m_indices.size();
data->indices = &mesh->m_indices[0];
data->boundsMin[0] = mesh->m_bounds[0];
data->boundsMin[1] = mesh->m_bounds[1];
data->boundsMin[2] = mesh->m_bounds[2];
data->boundsMax[0] = mesh->m_bounds[3];
data->boundsMax[1] = mesh->m_bounds[4];
data->boundsMax[2] = mesh->m_bounds[5];
}
void MeshDraw(Mesh* mesh, const MeshDrawParams* params)
{
MeshContextDrawParams drawParams;
drawParams.params = params;
drawParams.indexBuffer = mesh->m_indexBuffer;
drawParams.vertexBuffer = mesh->m_vertexBuffer;
MeshContextDraw(mesh->m_context, &drawParams);
}
void MeshRelease(Mesh* mesh)
{
if (mesh == nullptr) return;
MeshIndexBufferRelease(mesh->m_indexBuffer);
MeshVertexBufferRelease(mesh->m_vertexBuffer);
delete mesh;
}
/// **************** Private functions *******************************
void Mesh::computeBounds()
{
size_t imax = m_vertices.size();
if (imax >= 1)
{
m_bounds[0] = m_bounds[3] = m_vertices[0].x;
m_bounds[1] = m_bounds[4] = m_vertices[0].y;
m_bounds[2] = m_bounds[5] = m_vertices[0].z;
for (size_t i = 1; i < imax; i++)
{
m_bounds[0] = fminf(m_bounds[0], m_vertices[i].x);
m_bounds[3] = fmaxf(m_bounds[3], m_vertices[i].x);
m_bounds[1] = fminf(m_bounds[1], m_vertices[i].y);
m_bounds[4] = fmaxf(m_bounds[4], m_vertices[i].y);
m_bounds[2] = fminf(m_bounds[2], m_vertices[i].z);
m_bounds[5] = fmaxf(m_bounds[5], m_vertices[i].z);
}
}
}
void Mesh::normalize()
{
size_t imax = m_vertices.size();
for (size_t i = 0; i < imax; i++)
{
float x, y, z, w;
x = m_vertices[i].nx;
y = m_vertices[i].ny;
z = m_vertices[i].nz;
w = sqrtf(x*x + y*y + z*z);
if (w > 0.f)
{
x /= w;
y /= w;
z /= w;
}
m_vertices[i].nx = x;
m_vertices[i].ny = y;
m_vertices[i].nz = z;
}
}
/// ****************** PLY mesh support *******************************
namespace
{
struct PLYLoader
{
enum ElementType
{
ELEM_VERTEX = 0,
ELEM_FACE,
ELEM_EDGE,
ELEM_INVALID
};
enum Format
{
FORMAT_ASCII = 0,
FORMAT_BINARY_LITTLE_ENDIAN,
FORMAT_BINARY_BIG_ENDIAN,
FORMAT_INVALID
};
// temporary variables
FILE* file = nullptr;
static const int bufSize = 1024u;
char buf[bufSize];
ElementType elementType = ELEM_INVALID;
Format format = FORMAT_INVALID;
int numElements[ELEM_INVALID] = { 0 };
int numProperties[ELEM_INVALID] = { 0 };
int elementList[ELEM_INVALID] = { 0 };
int elementListIdx = 0;
// captured variables
std::vector<MeshVertex>& m_vertices;
std::vector<uint32_t>& m_indices;
// capture in constructor
PLYLoader(Mesh& mesh) :
m_vertices(mesh.m_vertices),
m_indices(mesh.m_indices)
{
}
// utility functions
void getline()
{
fgets(buf, bufSize, file);
};
void getstr()
{
fscanf_s(file, "%s", buf, bufSize);
};
int getint()
{
int i = 0;
fscanf_s(file, "%d", &i);
return i;
};
bool match(const char* key)
{
return strncmp(buf, key, bufSize) == 0;
};
// temporary state
int elemType = ELEM_INVALID;
int elemNum = 0;
int elemProperties = 0;
template<class getFloatType, class advanceFuncType>
void getVertices(getFloatType getFloat, advanceFuncType advanceFunc)
{
for (int i = 0; i < elemNum; i++)
{
float data[6];
for (int j = 0; j < elemProperties; j++)
{
float val = getFloat();
if (j < 6) data[j] = val;
}
m_vertices.push_back(MeshVertex{
data[0], data[1], data[2],
data[3], data[4], data[5]
});
advanceFunc();
}
}
template<class getCountType, class getIndexType, class advanceFuncType>
void getIndices(getCountType getCount, getIndexType getIndex, advanceFuncType advanceFunc)
{
for (int i = 0; i < elemNum; i++)
{
int count = getCount();
int indices[4];
if (count > 0) indices[0] = getIndex();
if (count > 1) indices[1] = getIndex();
if (count > 2) indices[2] = getIndex();
if (count > 3) indices[3] = getIndex();
if (count >= 3)
{
m_indices.push_back(indices[0]);
m_indices.push_back(indices[1]);
m_indices.push_back(indices[2]);
}
if (count == 4)
{
m_indices.push_back(indices[2]);
m_indices.push_back(indices[3]);
m_indices.push_back(indices[0]);
}
advanceFunc();
}
}
// main phases
bool parseHeader()
{
// verify file format
getstr();
if (match("ply"))
{
// extra header information
while (feof(file) == 0)
{
getstr();
if (match("element"))
{
getstr();
if (match("vertex")) {
elementType = ELEM_VERTEX;
}
else if (match("face")) {
elementType = ELEM_FACE;
}
else if (match("edge")) {
elementType = ELEM_EDGE;
}
unsigned int idx = (unsigned int)elementType;
if (idx < ELEM_INVALID)
{
numElements[idx] = getint();
}
if (elementListIdx < ELEM_INVALID)
{
elementList[elementListIdx++] = elementType;
}
}
else if (match("format"))
{
getstr();
if (match("ascii")) {
format = FORMAT_ASCII;
}
else if (match("binary_big_endian")) {
format = FORMAT_BINARY_BIG_ENDIAN;
}
else if (match("binary_little_endian")) {
format = FORMAT_BINARY_LITTLE_ENDIAN;
}
}
else if (match("property"))
{
unsigned int idx = (unsigned int)elementType;
if (idx < ELEM_INVALID)
{
numProperties[idx]++;
}
}
else if (match("end_header"))
{
break;
}
} // end read header
// advance past newline
getline();
return true;
}
return false;
}
void loadData()
{
// read in each element type
for (int eidx = 0; eidx < elementListIdx; eidx++)
{
elemType = elementList[eidx];
elemNum = numElements[elemType];
elemProperties = numProperties[elemType];
if (elemType == ELEM_VERTEX)
{
// size vertex buffers
m_vertices.reserve(elemNum);
if (format == FORMAT_ASCII)
{
getVertices(
[&]()
{
float val = 0.f;
fscanf_s(file, "%f", &val);
return val;
},
[&]()
{
getline();
}
);
}
else if (format == FORMAT_BINARY_BIG_ENDIAN)
{
getVertices(
[&]()
{
char data0[4];
fread(data0, sizeof(float), 1, file);
union
{
char data1[4];
float val;
};
data1[0] = data0[3];
data1[1] = data0[2];
data1[2] = data0[1];
data1[3] = data0[0];
return val;
},
[&]()
{
}
);
}
else if (format == FORMAT_BINARY_LITTLE_ENDIAN)
{
getVertices(
[&]()
{
float val = 0.f;
fread(&val, sizeof(float), 1, file);
return val;
},
[&]()
{
}
);
}
}
else if (elemType == ELEM_FACE)
{
// size vertex buffers
m_indices.reserve(3 * elemNum);
if (format == FORMAT_ASCII)
{
getIndices(
[&]()
{
return getint();
},
[&]()
{
return getint();
},
[&]()
{
getline();
}
);
}
else if (format == FORMAT_BINARY_BIG_ENDIAN)
{
getIndices(
[&]()
{
int val = 0;
fread(&val, 1, 1, file);
return val;
},
[&]()
{
char data0[4];
fread(data0, sizeof(float), 1, file);
union
{
char data1[4];
int val;
};
data1[0] = data0[3];
data1[1] = data0[2];
data1[2] = data0[1];
data1[3] = data0[0];
return val;
},
[&]()
{
}
);
}
else if (format == FORMAT_BINARY_LITTLE_ENDIAN)
{
getIndices(
[&]()
{
int val = 0;
fread(&val, 1, 1, file);
return val;
},
[&]()
{
int val;
fread(&val, sizeof(int), 1, file);
return val;
},
[&]()
{
}
);
}
}
else if (elemType == ELEM_EDGE)
{
}
}
}
// main entry point
void operator()(const char* filename)
{
fopen_s(&file, filename, "rb");
if (file)
{
if (parseHeader())
{
loadData();
}
fclose(file);
}
}
};
}
void Mesh::loadFromPLY(const char* filename)
{
PLYLoader loader(*this);
loader(filename);
}
|