-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShapeGeneration.cpp
551 lines (467 loc) · 15.9 KB
/
ShapeGeneration.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#include "ShapeGeneration.h"
#include "OpenGLWindow/ShapeData.h"
#include "btBulletDynamicsCommon.h"
#include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
#include "BulletCollision/CollisionShapes/btShapeHull.h" // to create a tesselation of a generic btConvexShape
#include "BulletCollision/CollisionShapes/btConvexPolyhedron.h"
#include "BulletSoftBody/btSoftBodyHelpers.h"
#include "LinearMath/btTransform.h"
#if FRAMEWORK_USE_OVR_MOBILE
#define LOWRES 1
#else
#define LOWRES 0
#endif
#if LOWRES
#define sphere_vertices medium_sphere_vertices
#define sphere_indices medium_sphere_indices
#define sphere_scale 1
#else
#define sphere_vertices textured_detailed_sphere_vertices
#define sphere_indices textured_detailed_sphere_indices
#define sphere_scale 2
#endif
static void appendPrim(
const ShapeVertex * __restrict vertices,
const int numVertices,
const int * __restrict indices,
const int numIndices,
const btTransform * transform,
const btVector3 & halfExtents,
const float uvScale,
btAlignedObjectArray<ShapeVertex> & out_vertices,
btAlignedObjectArray<int> & out_indices)
{
const int baseVertex = out_vertices.size();
out_vertices.resize(out_vertices.size() + numVertices);
for (int i = 0; i < numVertices; ++i)
out_vertices[baseVertex + i] = vertices[i];
for (int i = 0; i < numVertices; ++i)
{
float & x = out_vertices[baseVertex + i].xyzw[0];
float & y = out_vertices[baseVertex + i].xyzw[1];
float & z = out_vertices[baseVertex + i].xyzw[2];
x *= halfExtents.x();
y *= halfExtents.y();
z *= halfExtents.z();
if (transform != nullptr)
{
const btVector3 p = (*transform) * btVector3(x, y, z);
x = p.x();
y = p.y();
z = p.z();
}
}
if (uvScale != 1.f)
{
for (int i = 0; i < numVertices; ++i)
{
out_vertices[baseVertex + i].uv[0] *= uvScale;
out_vertices[baseVertex + i].uv[1] *= uvScale;
}
}
const int baseIndex = out_indices.size();
out_indices.resize(out_indices.size() + numIndices);
for (int i = 0; i < numIndices; ++i)
out_indices[baseIndex + i] = baseVertex + indices[i];
}
class MyTriangleCollector2 : public btTriangleCallback, public btInternalTriangleIndexCallback
{
public:
btVector3 m_aabbMin, m_aabbMax;
btScalar m_textureScaling = 1.;
btAlignedObjectArray<ShapeVertex> m_vertices;
btAlignedObjectArray<int> m_indices;
MyTriangleCollector2(
const btVector3& aabbMin,
const btVector3& aabbMax)
: m_aabbMin(aabbMin)
, m_aabbMax(aabbMax)
{
m_vertices.reserve(1 << 16);
m_indices.reserve(1 << 16);
}
virtual void processTriangle(btVector3 * tris, int partId, int triangleIndex) override final
{
const int baseVertex = m_vertices.size();
const int baseIndex = m_indices.size();
m_vertices.resize(m_vertices.size() + 3);
m_indices.resize(m_indices.size() + 3);
m_indices[baseIndex + 0] = baseVertex + 0;
m_indices[baseIndex + 1] = baseVertex + 1;
m_indices[baseIndex + 2] = baseVertex + 2;
const btVector3 extents = m_aabbMax - m_aabbMin;
for (int k = 0; k < 3; k++)
{
const btVector3 normal = (tris[0] - tris[1]).cross(tris[0] - tris[2]).safeNormalize();
auto & v = m_vertices[baseVertex + k];
for (int l = 0; l < 3; l++)
{
v.xyzw[l] = tris[k][l];
v.normal[l] = normal[l];
}
v.uv[0] = (1. - (v.xyzw[0] - m_aabbMin[0]) / extents[0]) * m_textureScaling;
v.uv[1] = (1. - (v.xyzw[1] - m_aabbMin[1]) / extents[1]) * m_textureScaling;
}
}
virtual void internalProcessTriangleIndex(btVector3 * triangle, int partId, int triangleIndex) override final
{
processTriangle(triangle, partId, triangleIndex);
}
};
void computeSoftBodyVertices(
const btCollisionShape * collisionShape,
btAlignedObjectArray<ShapeVertex> & out_vertices,
btAlignedObjectArray<int> & out_indices)
{
btAssert(collisionShape->getUserPointer());
if (collisionShape->getUserPointer() == nullptr)
return;
const btSoftBody * psb = (btSoftBody*)collisionShape->getUserPointer();
out_vertices.resize(psb->m_faces.size() * 3);
for (int i = 0; i < psb->m_faces.size(); ++i) // Foreach face
{
for (int k = 0; k < 3; ++k) // Foreach vertex on a face
{
const int currentIndex = i * 3 + k;
for (int j = 0; j < 3; j++)
out_vertices[currentIndex].xyzw[j] = psb->m_faces[i].m_n[k]->m_x[j];
out_vertices[currentIndex].xyzw[3] = 1.;
for (int j = 0; j < 3; ++j)
out_vertices[currentIndex].normal[j] = psb->m_faces[i].m_n[k]->m_n[j];
for (int j = 0; j < 2; ++j)
out_vertices[currentIndex].uv[j] = 0.5; // we don't have UV info...
out_indices.push_back(currentIndex);
}
}
}
static void appendShape(
const btCollisionShape * collisionShape,
const btTransform * shapeTransform,
btAlignedObjectArray<ShapeVertex> & out_vertices,
btAlignedObjectArray<int> & out_indices)
{
const int vertexStrideInBytes = sizeof(ShapeVertex);
btAssert(vertexStrideInBytes == 9 * sizeof(float));
if (collisionShape->getShapeType() == BOX_SHAPE_PROXYTYPE)
{
const btBoxShape * boxShape = (btBoxShape*)collisionShape;
const btVector3 halfExtents = boxShape->getHalfExtentsWithMargin();
appendPrim(
(ShapeVertex*)cube_vertices_textured,
sizeof(cube_vertices_textured) / vertexStrideInBytes,
cube_indices,
sizeof(cube_indices) / sizeof(cube_indices[0]),
shapeTransform,
halfExtents,
1.f,
out_vertices,
out_indices);
}
else if (collisionShape->getShapeType() == SPHERE_SHAPE_PROXYTYPE)
{
const btSphereShape * sphereShape = (btSphereShape*)collisionShape;
const btScalar sphereSize = sphere_scale * sphereShape->getRadius();
appendPrim(
(ShapeVertex*)sphere_vertices,
sizeof(sphere_vertices) / vertexStrideInBytes,
sphere_indices,
sizeof(sphere_indices) / sizeof(sphere_indices[0]),
shapeTransform,
btVector3(sphereSize, sphereSize, sphereSize),
1.f,
out_vertices,
out_indices);
}
else if (collisionShape->getShapeType() == CAPSULE_SHAPE_PROXYTYPE)
{
const btCapsuleShape * capsuleShape = (btCapsuleShape*)collisionShape;
const int up = capsuleShape->getUpAxis();
const btScalar halfHeight = capsuleShape->getHalfHeight();
const btScalar radius = capsuleShape->getRadius();
const btScalar sphereSize = sphere_scale * radius;
const btVector3 radiusScale = btVector3(sphereSize, sphereSize, sphereSize);
const int numVertices = sizeof(sphere_vertices) / vertexStrideInBytes;
const int numIndices = sizeof(sphere_indices) / sizeof(sphere_indices[0]);
const int * indices = sphere_indices;
const ShapeVertex * vertices = (ShapeVertex*)sphere_vertices;
btAlignedObjectArray<ShapeVertex> transformedVertices;
transformedVertices.resize(numVertices);
for (size_t i = 0; i < numVertices; ++i)
transformedVertices[i] = vertices[i];
for (int i = 0; i < numVertices; ++i)
{
transformedVertices[i].xyzw[0] *= radiusScale.x();
transformedVertices[i].xyzw[1] *= radiusScale.y();
transformedVertices[i].xyzw[2] *= radiusScale.z();
if (transformedVertices[i].xyzw[up] > 0)
transformedVertices[i].xyzw[up] += halfHeight;
else
transformedVertices[i].xyzw[up] -= halfHeight;
}
appendPrim(
&transformedVertices[0],
numVertices,
indices,
numIndices,
shapeTransform,
btVector3(1., 1., 1.),
1.f,
out_vertices,
out_indices);
}
else if (collisionShape->getShapeType() == MULTI_SPHERE_SHAPE_PROXYTYPE)
{
const btMultiSphereShape * multiSphereShape = (btMultiSphereShape*)collisionShape;
for (int i = 0; i < multiSphereShape->getSphereCount(); ++i)
{
const btVector3 position = multiSphereShape->getSpherePosition(i);
btTransform transform;
transform.setIdentity();
transform.setOrigin(position);
if (shapeTransform != nullptr)
transform = (*shapeTransform) * transform;
const btScalar radius = multiSphereShape->getSphereRadius(i);
const btScalar sphereSize = sphere_scale * radius;
appendPrim(
(ShapeVertex*)sphere_vertices,
sizeof(sphere_vertices) / vertexStrideInBytes,
sphere_indices,
sizeof(sphere_indices) / sizeof(sphere_indices[0]),
&transform,
btVector3(sphereSize, sphereSize, sphereSize),
1.f,
out_vertices,
out_indices);
}
}
else if (collisionShape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE)
{
btCompoundShape * compound = (btCompoundShape*)collisionShape;
for (int i = 0; i < compound->getNumChildShapes(); ++i)
appendShape(compound->getChildShape(i), &compound->getChildTransform(i), out_vertices, out_indices);
}
else if (collisionShape->getShapeType() == STATIC_PLANE_PROXYTYPE)
{
const btStaticPlaneShape * staticPlaneShape = static_cast<const btStaticPlaneShape*>(collisionShape);
const btScalar planeConst = staticPlaneShape->getPlaneConstant();
const btVector3 & planeNormal = staticPlaneShape->getPlaneNormal();
const btVector3 planeOrigin = planeNormal * planeConst;
btVector3 vec0, vec1;
btPlaneSpace1(planeNormal, vec0, vec1);
const btScalar vecLen = 128;
const btVector3 verts[4] =
{
planeOrigin + vec0 * vecLen + vec1 * vecLen,
planeOrigin - vec0 * vecLen + vec1 * vecLen,
planeOrigin - vec0 * vecLen - vec1 * vecLen,
planeOrigin + vec0 * vecLen - vec1 * vecLen
};
const int indices[6] =
{
0, 1, 2, 0, 2, 3
};
ShapeVertex vertices[4];
for (int i = 0; i < 4; i++)
{
const btVector3 & pos = verts[i];
vertices[i].xyzw[0] = pos[0];
vertices[i].xyzw[1] = pos[1];
vertices[i].xyzw[2] = pos[2];
vertices[i].xyzw[3] = 1.;
vertices[i].normal[0] = planeNormal[0];
vertices[i].normal[1] = planeNormal[1];
vertices[i].normal[2] = planeNormal[2];
}
vertices[0].uv[0] = +vecLen / 2;
vertices[0].uv[1] = +vecLen / 2;
vertices[1].uv[0] = -vecLen / 2;
vertices[1].uv[1] = +vecLen / 2;
vertices[2].uv[0] = -vecLen / 2;
vertices[2].uv[1] = -vecLen / 2;
vertices[3].uv[0] = +vecLen / 2;
vertices[3].uv[1] = -vecLen / 2;
appendPrim(vertices, 4, indices, 6, shapeTransform, btVector3(1., 1., 1.), 1.f, out_vertices, out_indices);
}
else if (collisionShape->getShapeType() == TERRAIN_SHAPE_PROXYTYPE)
{
const btHeightfieldTerrainShape * heightField = static_cast<const btHeightfieldTerrainShape*>(collisionShape);
btVector3 aabbMin(-BT_LARGE_FLOAT, -BT_LARGE_FLOAT, -BT_LARGE_FLOAT);
btVector3 aabbMax(+BT_LARGE_FLOAT, +BT_LARGE_FLOAT, +BT_LARGE_FLOAT);
MyTriangleCollector2 triangleCollector(aabbMin, aabbMax);
if (heightField->getUserValue3())
triangleCollector.m_textureScaling = heightField->getUserValue3();
heightField->processAllTriangles(&triangleCollector, aabbMin, aabbMax);
// todo : textures for shapes
//int userImage = heightField->getUserIndex2();
//if (userImage == -1)
// userImage = m_data->m_checkedTexture;
if (triangleCollector.m_vertices.size() > 0)
{
appendPrim(
&triangleCollector.m_vertices[0],
triangleCollector.m_vertices.size(),
&triangleCollector.m_indices[0],
triangleCollector.m_indices.size(),
shapeTransform,
btVector3(1., 1., 1.),
1.f,
out_vertices,
out_indices);
}
}
else if (collisionShape->getShapeType() == SOFTBODY_SHAPE_PROXYTYPE)
{
btAlignedObjectArray<ShapeVertex> vertices;
btAlignedObjectArray<int> indices;
computeSoftBodyVertices(collisionShape, vertices, indices);
if (vertices.size() > 0)
appendPrim(&vertices[0], vertices.size(), &indices[0], indices.size(), shapeTransform, btVector3(1., 1., 1.), 1.f, out_vertices, out_indices);
}
else if (collisionShape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
{
const btTriangleMeshShape * triangleMesh = static_cast<const btTriangleMeshShape*>(collisionShape);
btVector3 aabbMin(-BT_LARGE_FLOAT, -BT_LARGE_FLOAT, -BT_LARGE_FLOAT);
btVector3 aabbMax(+BT_LARGE_FLOAT, +BT_LARGE_FLOAT, +BT_LARGE_FLOAT);
MyTriangleCollector2 triangleCollector(aabbMin, aabbMax);
triangleMesh->getMeshInterface()->InternalProcessAllTriangles(&triangleCollector, aabbMin, aabbMax);
if (triangleCollector.m_vertices.size() > 0)
{
appendPrim(
&triangleCollector.m_vertices[0],
triangleCollector.m_vertices.size(),
&triangleCollector.m_indices[0],
triangleCollector.m_indices.size(),
shapeTransform,
btVector3(1., 1., 1.),
1.f,
out_vertices,
out_indices);
}
}
else if (collisionShape->getShapeType() == CONVEX_TRIANGLEMESH_SHAPE_PROXYTYPE)
{
const btConvexTriangleMeshShape * convexMesh = static_cast<const btConvexTriangleMeshShape*>(collisionShape);
btVector3 aabbMin(-BT_LARGE_FLOAT, -BT_LARGE_FLOAT, -BT_LARGE_FLOAT);
btVector3 aabbMax(+BT_LARGE_FLOAT, +BT_LARGE_FLOAT, +BT_LARGE_FLOAT);
MyTriangleCollector2 triangleCollector(aabbMin, aabbMax);
convexMesh->getMeshInterface()->InternalProcessAllTriangles(&triangleCollector, aabbMin, aabbMax);
if (triangleCollector.m_vertices.size() > 0)
{
appendPrim(
&triangleCollector.m_vertices[0],
triangleCollector.m_vertices.size(),
&triangleCollector.m_indices[0],
triangleCollector.m_indices.size(),
shapeTransform,
btVector3(1., 1., 1.),
1.f,
out_vertices,
out_indices);
}
}
else if (collisionShape->isConvex())
{
const btConvexShape * convex = (btConvexShape*)collisionShape;
const btConvexPolyhedron * pol = nullptr;
if (convex->isPolyhedral())
{
const btPolyhedralConvexShape * poly = (btPolyhedralConvexShape*)convex;
pol = poly->getConvexPolyhedron();
}
btAlignedObjectArray<ShapeVertex> vertices;
btAlignedObjectArray<int> indices;
if (pol != nullptr)
{
for (int v = 0; v < pol->m_vertices.size(); v++)
{
ShapeVertex vertex;
vertex.xyzw[0] = pol->m_vertices[v][0];
vertex.xyzw[1] = pol->m_vertices[v][1];
vertex.xyzw[2] = pol->m_vertices[v][2];
vertex.xyzw[3] = 1.;
btVector3 norm = pol->m_vertices[v];
norm.safeNormalize();
vertex.normal[0] = norm[0];
vertex.normal[1] = norm[1];
vertex.normal[2] = norm[2];
vertex.uv[0] = .5;
vertex.uv[1] = .5;
vertices.push_back(vertex);
}
for (int f = 0; f < pol->m_faces.size(); f++)
{
for (int ii = 2; ii < pol->m_faces[f].m_indices.size(); ii++)
{
indices.push_back(pol->m_faces[f].m_indices[0]);
indices.push_back(pol->m_faces[f].m_indices[ii - 1]);
indices.push_back(pol->m_faces[f].m_indices[ii]);
}
}
}
else
{
btShapeHull * hull = new btShapeHull(convex);
hull->buildHull(0.0, 1);
{
for (int t = 0; t < hull->numTriangles(); ++t)
{
const int index0 = hull->getIndexPointer()[t * 3 + 0];
const int index1 = hull->getIndexPointer()[t * 3 + 1];
const int index2 = hull->getIndexPointer()[t * 3 + 2];
const btVector3 & pos0 = hull->getVertexPointer()[index0];
const btVector3 & pos1 = hull->getVertexPointer()[index1];
const btVector3 & pos2 = hull->getVertexPointer()[index2];
const btVector3 triNormal = (pos1 - pos0).cross(pos2 - pos0).safeNormalize();
for (int v = 0; v < 3; v++)
{
const int index = hull->getIndexPointer()[t * 3 + v];
const btVector3 & pos = hull->getVertexPointer()[index];
ShapeVertex vertex;
vertex.xyzw[0] = pos[0];
vertex.xyzw[1] = pos[1];
vertex.xyzw[2] = pos[2];
vertex.xyzw[3] = 1.;
vertex.normal[0] = triNormal[0];
vertex.normal[1] = triNormal[1];
vertex.normal[2] = triNormal[2];
vertex.uv[0] = .5;
vertex.uv[1] = .5;
indices.push_back(vertices.size());
vertices.push_back(vertex);
}
}
}
delete hull;
}
if (vertices.size() > 0)
{
appendPrim(
&vertices[0],
vertices.size(),
&indices[0],
indices.size(),
shapeTransform,
btVector3(1., 1., 1.),
1.f,
out_vertices,
out_indices);
}
}
else
{
//printf("unknown collision shape type: %d\n", collisionShape->getShapeType());
/*
TRIANGLE_SHAPE_PROXYTYPE
TETRAHEDRAL_SHAPE_PROXYTYPE
CONE_SHAPE_PROXYTYPE
CYLINDER_SHAPE_PROXYTYPE
*/
}
}
void generateShapeMesh(
btCollisionShape * collisionShape,
btAlignedObjectArray<ShapeVertex> & out_vertices,
btAlignedObjectArray<int> & out_indices)
{
appendShape(collisionShape, nullptr, out_vertices, out_indices);
}