-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrameworkGuiHelper.cpp
465 lines (372 loc) · 14 KB
/
FrameworkGuiHelper.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
#include "FrameworkDebugDrawer.h"
#include "FrameworkGuiHelper.h"
#include "CommonInterfaces/CommonGraphicsAppInterface.h"
#include "CommonInterfaces/CommonRenderInterface.h"
#include "btBulletDynamicsCommon.h"
#include "BulletSoftBody/btSoftBody.h"
#include "ShapeGeneration.h"
#include "framework.h"
#include <functional>
FrameworkGUIHelperInterface::~FrameworkGUIHelperInterface()
{
}
void FrameworkGUIHelperInterface::createRigidBodyGraphicsObject(btRigidBody* body, const btVector3& color)
{
createCollisionObjectGraphicsObject(body, color);
}
void FrameworkGUIHelperInterface::createCollisionObjectGraphicsObject(btCollisionObject* body, const btVector3& color)
{
::createCollisionObjectGraphicsObject(m_renderInterface, body, color);
}
void FrameworkGUIHelperInterface::createCollisionShapeGraphicsObject(btCollisionShape* collisionShape)
{
::createCollisionShapeGraphicsObject(m_renderInterface, collisionShape);
}
void FrameworkGUIHelperInterface::syncPhysicsToGraphics(const btDiscreteDynamicsWorld* rbWorld)
{
const int numCollisionObjects = rbWorld->getNumCollisionObjects();
{
B3_PROFILE("write all InstanceTransformToCPU");
for (int i = 0; i < numCollisionObjects; i++)
{
//B3_PROFILE("writeSingleInstanceTransformToCPU");
const btCollisionObject * colObj = rbWorld->getCollisionObjectArray()[i];
const btCollisionShape * collisionShape = colObj->getCollisionShape();
if (collisionShape->getShapeType() == SOFTBODY_SHAPE_PROXYTYPE && collisionShape->getUserIndex() >= 0)
{
btAlignedObjectArray<ShapeVertex> vertices;
btAlignedObjectArray<int> indices;
computeSoftBodyVertices(collisionShape, vertices, indices);
if (vertices.size() > 0)
m_renderInterface->updateShape(collisionShape->getUserIndex(), vertices[0].xyzw);
continue;
}
const btVector3 & position = colObj->getWorldTransform().getOrigin();
const btQuaternion rotation = colObj->getWorldTransform().getRotation();
const int instanceId = colObj->getUserIndex();
if (instanceId >= 0)
{
m_renderInterface->writeSingleInstanceTransformToCPU(position, rotation, instanceId);
}
}
}
{
B3_PROFILE("writeTransforms");
m_renderInterface->writeTransforms();
}
}
void FrameworkGUIHelperInterface::syncPhysicsToGraphics2(const btDiscreteDynamicsWorld* rbWorld)
{
// for implementing a physics client-server connection. not implemented
}
void FrameworkGUIHelperInterface::syncPhysicsToGraphics2(const GUISyncPosition* positions, int numPositions)
{
// for implementing a physics client-server connection. not implemented
}
void FrameworkGUIHelperInterface::render(const btDiscreteDynamicsWorld* rbWorld)
{
m_renderInterface->renderScene();
}
void FrameworkGUIHelperInterface::createPhysicsDebugDrawer(btDiscreteDynamicsWorld* rbWorld)
{
btAssert(rbWorld);
if (m_debugDraw != nullptr)
{
delete m_debugDraw;
m_debugDraw = nullptr;
}
m_debugDraw = new FrameworkDebugDrawer();
rbWorld->setDebugDrawer(m_debugDraw);
m_debugDraw->setDebugMode(
btIDebugDraw::DBG_DrawWireframe |
btIDebugDraw::DBG_DrawAabb);
}
int FrameworkGUIHelperInterface::registerTexture(const unsigned char* texels, int width, int height)
{
return m_renderInterface->registerTexture(texels, width, height);
}
int FrameworkGUIHelperInterface::registerGraphicsShape(const float* vertices, int numvertices, const int* indices, int numIndices, int primitiveType, int textureId)
{
return m_renderInterface->registerShape(vertices, numvertices, indices, numIndices, primitiveType, textureId);
}
int FrameworkGUIHelperInterface::registerGraphicsInstance(int shapeId, const float* position, const float* quaternion, const float* color, const float* scaling)
{
return m_renderInterface->registerGraphicsInstance(shapeId, position, quaternion, color, scaling);
}
void FrameworkGUIHelperInterface::removeAllGraphicsInstances()
{
m_renderInterface->removeAllInstances();
}
void FrameworkGUIHelperInterface::removeGraphicsInstance(int graphicsId)
{
m_renderInterface->removeGraphicsInstance(graphicsId);
}
void FrameworkGUIHelperInterface::changeRGBAColor(int instanceId, const double rgbaColor[4])
{
m_renderInterface->writeSingleInstanceColorToCPU(rgbaColor, instanceId);
}
void FrameworkGUIHelperInterface::changeSpecularColor(int instanceId, const double specularColor[3])
{
m_renderInterface->writeSingleInstanceSpecularColorToCPU(specularColor, instanceId);
}
void FrameworkGUIHelperInterface::changeTexture(int textureId, const unsigned char * texels, int width, int height)
{
m_renderInterface->updateTexture(textureId, texels);
}
int FrameworkGUIHelperInterface::getShapeIndexFromInstance(int instanceId)
{
return m_renderInterface->getShapeIndexFromInstance(instanceId);
}
void FrameworkGUIHelperInterface::replaceTexture(int shapeId, int textureId)
{
m_renderInterface->replaceTexture(shapeId, textureId);
}
void FrameworkGUIHelperInterface::removeTexture(int id)
{
m_renderInterface->removeTexture(id);
}
Common2dCanvasInterface* FrameworkGUIHelperInterface::get2dCanvasInterface()
{
return m_2dCanvasInterface;
}
CommonParameterInterface* FrameworkGUIHelperInterface::getParameterInterface()
{
return m_parameterInterface;
}
CommonRenderInterface* FrameworkGUIHelperInterface::getRenderInterface()
{
return m_renderInterface;
}
const CommonRenderInterface* FrameworkGUIHelperInterface::getRenderInterface() const
{
return m_renderInterface;
}
CommonGraphicsApp* FrameworkGUIHelperInterface::getAppInterface()
{
return m_appInterface;
}
void FrameworkGUIHelperInterface::setUpAxis(int axis)
{
m_appInterface->setUpAxis(axis);
}
void FrameworkGUIHelperInterface::resetCamera(float camDist, float yaw, float pitch, float camPosX, float camPosY, float camPosZ)
{
if (getRenderInterface() && getRenderInterface()->getActiveCamera())
{
getRenderInterface()->getActiveCamera()->setCameraDistance(camDist);
getRenderInterface()->getActiveCamera()->setCameraPitch(pitch);
getRenderInterface()->getActiveCamera()->setCameraYaw(yaw);
getRenderInterface()->getActiveCamera()->setCameraTargetPosition(camPosX, camPosY, camPosZ);
}
}
bool FrameworkGUIHelperInterface::getCameraInfo(int* width, int* height, float viewMatrix[16], float projectionMatrix[16], float camUp[3], float camForward[3], float hor[3], float vert[3], float* yaw, float* pitch, float* camDist, float camTarget[3]) const
{
Assert(false);
return false;
}
void FrameworkGUIHelperInterface::setVisualizerFlag(int flag, int enable)
{
// see b3ConfigureDebugVisualizerEnum
// controls things like shadows enabled, planar reflection enabled, wireframe, mouse picking etc
// not implemented
}
void FrameworkGUIHelperInterface::copyCameraImageData(
const float viewMatrix[16], const float projectionMatrix[16],
unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels,
float* depthBuffer, int depthBufferSizeInPixels,
int startPixelIndex, int destinationWidth, int destinationHeight, int* numPixelsCopied)
{
copyCameraImageData(
viewMatrix, projectionMatrix, pixelsRGBA, rgbaBufferSizeInPixels,
depthBuffer, depthBufferSizeInPixels,
0, 0,
startPixelIndex, destinationWidth,
destinationHeight, numPixelsCopied);
}
void FrameworkGUIHelperInterface::copyCameraImageData(
const float viewMatrix[16], const float projectionMatrix[16],
unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels,
float* depthBuffer, int depthBufferSizeInPixels,
int* segmentationMaskBuffer, int segmentationMaskBufferSizeInPixels,
int startPixelIndex, int destinationWidth, int destinationHeight, int* numPixelsCopied)
{
Assert(false);
}
void FrameworkGUIHelperInterface::debugDisplayCameraImageData(
const float viewMatrix[16], const float projectionMatrix[16],
unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels,
float* depthBuffer, int depthBufferSizeInPixels,
int* segmentationMaskBuffer, int segmentationMaskBufferSizeInPixels,
int startPixelIndex, int destinationWidth, int destinationHeight, int* numPixelsCopied)
{
Assert(false);
}
void FrameworkGUIHelperInterface::setProjectiveTextureMatrices(const float viewMatrix[16], const float projectionMatrix[16])
{
}
void FrameworkGUIHelperInterface::setProjectiveTexture(bool useProjectiveTexture)
{
}
void FrameworkGUIHelperInterface::autogenerateGraphicsObjects(btDiscreteDynamicsWorld * rbWorld)
{
::autogenerateGraphicsObjects(m_renderInterface, rbWorld);
}
void FrameworkGUIHelperInterface::drawText3D(const char* txt, float posX, float posY, float posZ, float size)
{
m_appInterface->drawText3D(txt, posX, posY, posZ, size);
}
void FrameworkGUIHelperInterface::drawText3D(const char* txt, float position[3], float orientation[4], float color[4], float size, int optionFlag)
{
m_appInterface->drawText3D(txt, position, orientation, color, size, optionFlag);
}
int FrameworkGUIHelperInterface::addUserDebugText3D(const char* txt, const double positionXYZ[3], const double orientation[4], const double textColorRGB[3], double size, double lifeTime, int trackingVisualShapeIndex, int optionFlags, int replaceItemUid)
{
Assert(false);
return -1;
}
int FrameworkGUIHelperInterface::addUserDebugLine(const double debugLineFromXYZ[3], const double debugLineToXYZ[3], const double debugLineColorRGB[3], double lineWidth, double lifeTime, int trackingVisualShapeIndex, int replaceItemUid)
{
Assert(false);
return -1;
}
int FrameworkGUIHelperInterface::addUserDebugParameter(const char* txt, double rangeMin, double rangeMax, double startValue)
{
return -1;
}
int FrameworkGUIHelperInterface::readUserDebugParameter(int itemUniqueId, double* value)
{
return 0;
}
void FrameworkGUIHelperInterface::removeUserDebugItem(int debugItemUniqueId)
{
}
void FrameworkGUIHelperInterface::removeAllUserDebugItems()
{
}
void FrameworkGUIHelperInterface::setVisualizerFlagCallback(VisualizerFlagCallback callback)
{
}
void FrameworkGUIHelperInterface::dumpFramesToVideo(const char* mp4FileName)
{
}
//
void createCollisionObjectGraphicsObject(CommonRenderInterface * renderInterface, btCollisionObject * body, const btVector3 & color)
{
if (body->getUserIndex() >= 0)
return;
const btCollisionShape * shape = body->getCollisionShape();
const btTransform startTransform = body->getWorldTransform();
const int graphicsShapeId = shape->getUserIndex();
if (graphicsShapeId >= 0)
{
const int graphicsInstanceId = renderInterface->registerGraphicsInstance(
graphicsShapeId,
startTransform.getOrigin(),
startTransform.getRotation(),
color,
btVector3(1., 1., 1.));
body->setUserIndex(graphicsInstanceId);
}
}
void createCollisionShapeGraphicsObject(CommonRenderInterface * renderInterface, btCollisionShape * collisionShape, const int textureId)
{
// already has a graphics object ?
if (collisionShape->getUserIndex() >= 0)
return;
btAlignedObjectArray<ShapeVertex> vertices;
btAlignedObjectArray<int> indices;
generateShapeMesh(collisionShape, vertices, indices);
if (indices.size() > 0)
{
const int graphicsShapeId = renderInterface->registerShape(
vertices[0].xyzw,
vertices.size(),
&indices[0],
indices.size(),
B3_GL_TRIANGLES,
textureId);
collisionShape->setUserIndex(graphicsShapeId);
}
}
static const btVector4 sColors[4] =
{
btVector4(60. / 256., 186. / 256., 84. / 256., 1),
btVector4(244. / 256., 194. / 256., 13. / 256., 1),
btVector4(219. / 256., 50. / 256., 54. / 256., 1),
btVector4(72. / 256., 133. / 256., 237. / 256., 1)
};
static int createCheckeredTexture(CommonRenderInterface * renderInterface, int r, int g, int b)
{
const int sx = 1024;
const int sy = 1024;
uint8_t * texels = new uint8_t[sx * sy * 3];
memset(texels, 0xff, sx * sy * 3);
for (int y = 0; y < 1024; ++y)
{
uint8_t * line = texels + y * sx * 3;
for (int x = 0; x < 1024; ++x)
{
if (((x>>9) ^ (y>>9)) == 0)
{
line[x * 3 + 0] = r;
line[x * 3 + 1] = g;
line[x * 3 + 2] = b;
}
}
}
const int textureId = renderInterface->registerTexture(texels, sx, sy);
delete [] texels;
texels = nullptr;
return textureId;
}
void autogenerateGraphicsObjects(CommonRenderInterface * renderInterface, btDiscreteDynamicsWorld * rbWorld)
{
const int checkeredTextureId = createCheckeredTexture(renderInterface, 192, 192, 192);
for (int i = 0; i < rbWorld->getNumCollisionObjects(); i++)
{
btCollisionObject * colObj = rbWorld->getCollisionObjectArray()[i];
// assign soft body ptr when this is a soft body object
btSoftBody * sb = btSoftBody::upcast(colObj);
if (sb != nullptr)
colObj->getCollisionShape()->setUserPointer(sb);
createCollisionShapeGraphicsObject(renderInterface, colObj->getCollisionShape(), checkeredTextureId);
int colorIndex = colObj->getBroadphaseHandle()->getUid() & 3;
btVector4 color = sColors[colorIndex];
if (colObj->getCollisionShape()->getShapeType() == STATIC_PLANE_PROXYTYPE)
color.setValue(1, 1, 1, 1);
createCollisionObjectGraphicsObject(renderInterface, colObj, color);
}
}
void syncPhysicsToGraphics(const btDiscreteDynamicsWorld * rbWorld, CommonRenderInterface * renderInterface)
{
const int numCollisionObjects = rbWorld->getNumCollisionObjects();
{
B3_PROFILE("write all InstanceTransformToCPU");
for (int i = 0; i < numCollisionObjects; i++)
{
//B3_PROFILE("writeSingleInstanceTransformToCPU");
const btCollisionObject * colObj = rbWorld->getCollisionObjectArray()[i];
const btCollisionShape * collisionShape = colObj->getCollisionShape();
if (collisionShape->getShapeType() == SOFTBODY_SHAPE_PROXYTYPE && collisionShape->getUserIndex() >= 0)
{
btAlignedObjectArray<ShapeVertex> vertices;
btAlignedObjectArray<int> indices;
computeSoftBodyVertices(collisionShape, vertices, indices);
if (vertices.size() > 0)
renderInterface->updateShape(collisionShape->getUserIndex(), vertices[0].xyzw);
continue;
}
const btVector3 & position = colObj->getWorldTransform().getOrigin();
const btQuaternion rotation = colObj->getWorldTransform().getRotation();
const int instanceId = colObj->getUserIndex();
if (instanceId >= 0)
{
renderInterface->writeSingleInstanceTransformToCPU(position, rotation, instanceId);
}
}
}
{
B3_PROFILE("writeTransforms");
renderInterface->writeTransforms();
}
}