-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
486 lines (407 loc) · 13.3 KB
/
main.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <math.h>
#include "wavefront.h"
#include "NvRenderDebug.h"
#include "TestHACD.h"
#include "VHACD.h"
#pragma warning(disable:4456 4457)
#define TSCALE1 (1.0f/4.0f)
RENDER_DEBUG::RenderDebugTyped *gRenderDebugTyped=NULL;
RENDER_DEBUG::RenderDebug *gRenderDebug=NULL;
static bool gShowSourceMesh = true;
static bool gShowConvexDecomposition = true;
static bool gUseHACD = true;
static float gScaleInputMesh = 1;
static float gExplodeViewScale = 1;
static float gCenter[3] { 0, 0, 0 };
static uint32_t gVertexCount = 0;
static uint32_t gTriangleCount = 0;
static double *gVertices = nullptr;
static int *gIndices = nullptr;
static VHACD::IVHACD::Parameters gDesc;
float fm_computePlane(const float *A,const float *B,const float *C,float *n) // returns D
{
float vx = (B[0] - C[0]);
float vy = (B[1] - C[1]);
float vz = (B[2] - C[2]);
float wx = (A[0] - B[0]);
float wy = (A[1] - B[1]);
float wz = (A[2] - B[2]);
float vw_x = vy * wz - vz * wy;
float vw_y = vz * wx - vx * wz;
float vw_z = vx * wy - vy * wx;
float mag = ::sqrtf((vw_x * vw_x) + (vw_y * vw_y) + (vw_z * vw_z));
if ( mag < 0.000001f )
{
mag = 0;
}
else
{
mag = 1.0f/mag;
}
float x = vw_x * mag;
float y = vw_y * mag;
float z = vw_z * mag;
float D = 0.0f - ((x*A[0])+(y*A[1])+(z*A[2]));
n[0] = x;
n[1] = y;
n[2] = z;
return D;
}
class MeshBuilder
{
public:
MeshBuilder(uint32_t maxVertices)
{
mVertices.reserve(maxVertices);
}
void getVertex(const float *p,const float *n,uint32_t i1,uint32_t i2)
{
RENDER_DEBUG::RenderDebugMeshVertex v;
v.mPosition[0] = p[0];
v.mPosition[1] = p[1];
v.mPosition[2] = p[2];
v.mNormal[0] = n[0];
v.mNormal[1] = n[1];
v.mNormal[2] = n[2];
v.mTexel[0] = p[i1]*TSCALE1;
v.mTexel[1] = p[i2]*TSCALE1;
mVertices.push_back(v);
}
void addTriangle(const float *p1,const float *p2,const float *p3)
{
float normal[3];
fm_computePlane(p3,p2,p1,normal);
double nx = fabs(normal[0]);
double ny = fabs(normal[1]);
double nz = fabs(normal[2]);
uint32_t i1 = 0;
uint32_t i2 = 0;
if ( nx <= ny && nx <= nz )
i1 = 0;
if ( ny <= nx && ny <= nz )
i1 = 1;
if ( nz <= nx && nz <= ny )
i1 = 2;
switch ( i1 )
{
case 0:
if ( ny < nz )
i2 = 1;
else
i2 = 2;
break;
case 1:
if ( nx < nz )
i2 = 0;
else
i2 = 2;
break;
case 2:
if ( nx < ny )
i2 = 0;
else
i2 = 1;
break;
}
getVertex(p1,normal,i1,i2);
getVertex(p2,normal,i1,i2);
getVertex(p3,normal,i1,i2);
}
std::vector< RENDER_DEBUG::RenderDebugMeshVertex > mVertices;
};
static void fm_computCenter(uint32_t vcount, const float *vertices, float center[3])
{
float bmin[3];
float bmax[3];
bmin[0] = vertices[0];
bmin[1] = vertices[1];
bmin[2] = vertices[2];
bmax[0] = vertices[0];
bmax[1] = vertices[1];
bmax[2] = vertices[2];
for (uint32_t i = 1; i < vcount; i++)
{
const float *v = &vertices[i * 3];
if (v[0] < bmin[0]) bmin[0] = v[0];
if (v[1] < bmin[1]) bmin[1] = v[1];
if (v[2] < bmin[2]) bmin[2] = v[2];
if (v[0] > bmax[0]) bmax[0] = v[0];
if (v[1] > bmax[1]) bmax[1] = v[1];
if (v[2] > bmax[2]) bmax[2] = v[2];
}
center[0] = (bmax[0] - bmin[0])*0.5f + bmin[0];
center[1] = (bmax[1] - bmin[1])*0.5f + bmin[1];
center[2] = (bmax[2] - bmin[2])*0.5f + bmin[2];
}
void createMenus(void)
{
gRenderDebug->sendRemoteCommand("BeginTab \"Convex Decomposition - V-HACD\""); // Mark the beginning of a new tab display in the DebugView application
gRenderDebug->sendRemoteCommand("BeginGroup \"Controls\""); // Mark the beginning of a group of controls.
gRenderDebug->sendRemoteCommand("FileTransferButton \" Select Wavefront File\" WavefrontFile \"Choose a Wavefront OBJ file to transfer\" *.obj");
gRenderDebug->sendRemoteCommand("CheckBox ShowSourceMesh true ShowSourceMesh");
gRenderDebug->sendRemoteCommand("CheckBox ShowConvexDecomposition true ShowConvexDecomposition");
gRenderDebug->sendRemoteCommand("Slider ScaleInputMesh 1 1 50 ScaleInputMesh");
gRenderDebug->sendRemoteCommand("Slider ExplodeViewScale 1 1 4 ExplodeViewScale");
gRenderDebug->sendRemoteCommand("Button PerformConvexDecomposition \"decomp\"");
gRenderDebug->sendRemoteCommand("Button Cancel \"cancel\"");
gRenderDebug->sendRemoteCommand("EndGroup"); // End the group called 'controls'
gRenderDebug->sendRemoteCommand("BeginGroup \"V-HACD Settings\""); // Mark the beginning of a group of controls.
gRenderDebug->sendRemoteCommand("SliderInt DecompositionDepth 10 1 20 DecompositionDepth");
gRenderDebug->sendRemoteCommand("SliderInt MaxHullVertices 32 8 512 MaxHullVertices");
gRenderDebug->sendRemoteCommand("SliderInt MaxConvexHulls 32 1 512 MaxConvexHulls");
gRenderDebug->sendRemoteCommand("Slider Concavity 0.001 0 0.1 Concavity");
gRenderDebug->sendRemoteCommand("Slider Gamma 0.0005 0 0.1 Gamma");
gRenderDebug->sendRemoteCommand("EndGroup"); // End the group called 'HACD settings'
gRenderDebug->sendRemoteCommand("EndTab"); // End the tab called 'Test RenderDebug'
}
int main(int argc,const char **argv)
{
{
const char *dllName=NULL;
#ifdef _WIN64
dllName = "NvRenderDebug_x64.dll";
#else
dllName = "NvRenderDebug_x86.dll";
#endif
printf("Loading RenderDebug DLL\r\n");
RENDER_DEBUG::RenderDebug::Desc desc;
desc.dllName = dllName;
char recordName[512];
sprintf_s(recordName,512,"%s", argv[1]);
char *obj = strstr(recordName,".obj");
if ( obj )
{
*obj = 0;
strcat(recordName,".rec");
}
desc.applicationName = "ObjView";
desc.recordFileName = recordName;
desc.runMode = RENDER_DEBUG::RenderDebug::RM_CLIENT_OR_FILE;
gRenderDebug = RENDER_DEBUG::createRenderDebug(desc);
if ( gRenderDebug )
{
WavefrontObj sourceMesh;
if (argc == 2)
{
printf("Loading wavefront.obj file '%s'\r\n", argv[1]);
sourceMesh.loadObj(argv[1]);
}
{
TestHACD *thacd = TestHACD::create();
double *meshVertices = nullptr;
printf("Found: %d triangles.\r\n", sourceMesh.mTriCount );
gRenderDebug->addToCurrentState(RENDER_DEBUG::DebugRenderState::CenterText);
{
uint32_t meshId = 0;
uint32_t frameCount = 2;
if ( gRenderDebug->getRunMode() == RENDER_DEBUG::RenderDebug::RM_CLIENT )
{
frameCount = 500000;
}
bool solid=true;
const char *meshName = argv[1];
// main pump loop...
WavefrontObj w;
for (uint32_t i=0; i<frameCount; i++)
{
if (meshId == 0 && sourceMesh.mVertexCount )
{
sourceMesh.deepCopyScale(w, gScaleInputMesh);
gVertexCount = w.mVertexCount;
gTriangleCount = w.mTriCount;
delete[]meshVertices;
meshVertices = new double[w.mVertexCount * 3];
gVertices = meshVertices;
gIndices = (int *)w.mIndices;
for (uint32_t i = 0; i < w.mVertexCount; i++)
{
meshVertices[i * 3 + 0] = w.mVertices[i * 3 + 0];
meshVertices[i * 3 + 1] = w.mVertices[i * 3 + 1];
meshVertices[i * 3 + 2] = w.mVertices[i * 3 + 2];
}
meshId = gRenderDebug->getMeshId();
{
MeshBuilder mb(w.mTriCount * 3);
for (uint32_t i = 0; i < w.mTriCount; i++)
{
uint32_t i1 = w.mIndices[i * 3 + 0];
uint32_t i2 = w.mIndices[i * 3 + 1];
uint32_t i3 = w.mIndices[i * 3 + 2];
const float *p1 = &w.mVertices[i1 * 3];
const float *p2 = &w.mVertices[i2 * 3];
const float *p3 = &w.mVertices[i3 * 3];
mb.addTriangle(p3, p2, p1);
}
gRenderDebug->createTriangleMesh(meshId, (uint32_t)mb.mVertices.size(), &mb.mVertices[0], 0, NULL);
}
fm_computCenter(w.mVertexCount, w.mVertices, gCenter);
}
gRenderDebug->debugText2D(0, 0, 0.5f, 2.0f, false, 0xFFFF00, "%s", meshName);
if (thacd)
{
gRenderDebug->debugText2D(0, 0.04f, 0.5f, 2.0f, false, 0xFFFF00, "HullCount: %d", thacd->getHullCount());
}
gRenderDebug->addToCurrentState(RENDER_DEBUG::DebugRenderState::SolidWireShaded);
gRenderDebug->addToCurrentState(RENDER_DEBUG::DebugRenderState::CameraFacing);
gRenderDebug->setCurrentColor(0xFFFF00);
if (gShowSourceMesh)
{
if (solid)
{
RENDER_DEBUG::RenderDebugInstance instance;
gRenderDebug->renderTriangleMeshInstances(meshId, 1, &instance);
}
else
{
for (uint32_t i = 0; i < w.mTriCount; i++)
{
uint32_t i1 = w.mIndices[i * 3 + 0];
uint32_t i2 = w.mIndices[i * 3 + 1];
uint32_t i3 = w.mIndices[i * 3 + 2];
const float *p1 = &w.mVertices[i1 * 3];
const float *p2 = &w.mVertices[i2 * 3];
const float *p3 = &w.mVertices[i3 * 3];
gRenderDebug->debugTri(p3, p2, p1);
}
}
}
if (thacd == nullptr)
{
thacd = TestHACD::create();
}
if (thacd && gShowConvexDecomposition )
{
thacd->render(gRenderDebug,gExplodeViewScale,gCenter);
}
gRenderDebug->render(1.0f/60.0f,NULL);
uint32_t argc;
const char **argv = gRenderDebug->getRemoteCommand(argc);
if ( argv )
{
const char *cmd = argv[0];
if ( strcmp(cmd,"client_stop") == 0 )
{
break;
}
else if (strcmp(cmd,"toggle") == 0 )
{
solid = solid ? false : true;
}
else if (strcmp(cmd, "decomp") == 0 && thacd)
{
printf("Performing Convex Decomposition\n");
thacd->decompose(gVertices, gVertexCount, gIndices, gTriangleCount, gDesc);
}
else if (strcmp(cmd, "cancel") == 0 && thacd)
{
printf("Canceling Convex Decomposition\n");
thacd->cancel();
}
else if (strcmp(cmd, "DecompositionDepth") == 0 && argc == 2)
{
gDesc.m_depth = atoi(argv[1]);
printf("DecompositionDepth=%d\n", gDesc.m_depth);
}
else if (strcmp(cmd, "MaxHullVertices") == 0 && argc == 2)
{
gDesc.m_maxNumVerticesPerCH = atoi(argv[1]);
printf("MaxHullVertices=%d\n", gDesc.m_maxNumVerticesPerCH);
}
else if (strcmp(cmd, "MaxConvexHulls") == 0 && argc == 2)
{
gDesc.m_maxConvexHulls = atoi(argv[1]);
printf("MaxConvexHulls=%d\n", gDesc.m_maxConvexHulls);
}
else if (strcmp(cmd, "ShowSourceMesh") == 0 && argc == 2)
{
const char *value = argv[1];
gShowSourceMesh = strcmp(value, "true") == 0;
printf("ShowSourceMesh=%s\n", value);
}
else if (strcmp(cmd, "ShowConvexDecomposition") == 0 && argc == 2)
{
const char *value = argv[1];
gShowConvexDecomposition = strcmp(value, "true") == 0;
printf("ShowConvexDecomposition=%s\n", value);
}
else if (strcmp(cmd, "Concavity") == 0 && argc == 2)
{
const char *value = argv[1];
gDesc.m_concavity = (float)atof(value);
printf("Concavity=%0.5f\n", gDesc.m_concavity);
}
else if (strcmp(cmd, "Gamma") == 0 && argc == 2)
{
const char *value = argv[1];
gDesc.m_gamma = (float)atof(value);
printf("Gamma=%0.5f\n", gDesc.m_gamma);
}
else if (strcmp(cmd, "ExplodeViewScale") == 0 && argc == 2)
{
const char *value = argv[1];
gExplodeViewScale = (float)atof(value);
printf("ExplodeViewScale=%0.5f\n", gExplodeViewScale);
}
else if (strcmp(cmd, "ScaleInputMesh") == 0 && argc == 2)
{
const char *value = argv[1];
gScaleInputMesh = (float)atof(value);
printf("ScaleInputMesh=%0.5f\n", gScaleInputMesh);
if (thacd)
{
thacd->release();
thacd = nullptr;
}
gRenderDebug->releaseTriangleMesh(meshId);
meshId = 0;
}
}
const char *nameSpace;
const char *resourceName;
bool isBigEndianRemote;
uint32_t dlen;
const void *data = gRenderDebug->getRemoteResource(nameSpace, resourceName, dlen, isBigEndianRemote);
while (data)
{
printf("Received remote resource %s:%s %d bytes long and remote machine is %sbig endian\r\n",
nameSpace,
resourceName,
dlen,
isBigEndianRemote ? "" : "not ");
if (strcmp(nameSpace, "WavefrontFile") == 0)
{
thacd->release();
thacd = nullptr;
meshName = resourceName;
sourceMesh.loadObj((const uint8_t *)data, dlen);
printf("Loaded Wavefront file %s with %d triangles and %d vertices.\r\n", resourceName, sourceMesh.mTriCount, sourceMesh.mVertexCount);
gRenderDebug->releaseTriangleMesh(meshId);
meshId = 0;
}
data = gRenderDebug->getRemoteResource(nameSpace, resourceName, dlen, isBigEndianRemote);
}
if ( i == 1 )
{
createMenus();
}
}
}
if (thacd)
{
thacd->release();
}
delete[]meshVertices;
}
gRenderDebug->release();
}
else
{
printf("Failed to load RenderDebug DLL (%s)\r\n", desc.dllName );
}
}
return 0;
}