1 #include "OpenGLGuiHelper.h" 2 3 #include "btBulletDynamicsCommon.h" 4 #include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h" 5 #include "../CommonInterfaces/CommonGraphicsAppInterface.h" 6 #include "../CommonInterfaces/CommonRenderInterface.h" 7 #include "Bullet3Common/b3Scalar.h" 8 #include "CollisionShape2TriangleMesh.h" 9 #include "BulletSoftBody/btSoftBodyHelpers.h" 10 11 #include "../OpenGLWindow/ShapeData.h" 12 13 #include "../OpenGLWindow/SimpleCamera.h" 14 15 #define BT_LINE_BATCH_SIZE 512 16 17 struct MyDebugVec3 18 { MyDebugVec3MyDebugVec319 MyDebugVec3(const btVector3& org) 20 : x(org.x()), 21 y(org.y()), 22 z(org.z()) 23 { 24 } 25 26 float x; 27 float y; 28 float z; 29 }; 30 31 ATTRIBUTE_ALIGNED16(class) 32 MyDebugDrawer : public btIDebugDraw 33 { 34 CommonGraphicsApp* m_glApp; 35 int m_debugMode; 36 37 btAlignedObjectArray<MyDebugVec3> m_linePoints; 38 btAlignedObjectArray<unsigned int> m_lineIndices; 39 40 btVector3 m_currentLineColor; 41 DefaultColors m_ourColors; 42 43 public: 44 BT_DECLARE_ALIGNED_ALLOCATOR(); 45 46 MyDebugDrawer(CommonGraphicsApp * app) 47 : m_glApp(app), m_debugMode(btIDebugDraw::DBG_DrawWireframe | btIDebugDraw::DBG_DrawAabb), m_currentLineColor(-1, -1, -1) 48 { 49 } 50 51 virtual ~MyDebugDrawer() 52 { 53 } 54 virtual DefaultColors getDefaultColors() const 55 { 56 return m_ourColors; 57 } 58 ///the default implementation for setDefaultColors has no effect. A derived class can implement it and store the colors. 59 virtual void setDefaultColors(const DefaultColors& colors) 60 { 61 m_ourColors = colors; 62 } 63 64 virtual void drawLine(const btVector3& from1, const btVector3& to1, const btVector3& color1) 65 { 66 //float from[4] = {from1[0],from1[1],from1[2],from1[3]}; 67 //float to[4] = {to1[0],to1[1],to1[2],to1[3]}; 68 //float color[4] = {color1[0],color1[1],color1[2],color1[3]}; 69 //m_glApp->m_instancingRenderer->drawLine(from,to,color); 70 if (m_currentLineColor != color1 || m_linePoints.size() >= BT_LINE_BATCH_SIZE) 71 { 72 flushLines(); 73 m_currentLineColor = color1; 74 } 75 MyDebugVec3 from(from1); 76 MyDebugVec3 to(to1); 77 78 m_linePoints.push_back(from); 79 m_linePoints.push_back(to); 80 81 m_lineIndices.push_back(m_lineIndices.size()); 82 m_lineIndices.push_back(m_lineIndices.size()); 83 } 84 85 virtual void drawContactPoint(const btVector3& PointOnB, const btVector3& normalOnB, btScalar distance, int lifeTime, const btVector3& color) 86 { 87 drawLine(PointOnB, PointOnB + normalOnB * distance, color); 88 btVector3 ncolor(0, 0, 0); 89 drawLine(PointOnB, PointOnB + normalOnB * 0.01, ncolor); 90 } 91 92 virtual void reportErrorWarning(const char* warningString) 93 { 94 } 95 96 virtual void draw3dText(const btVector3& location, const char* textString) 97 { 98 } 99 100 virtual void setDebugMode(int debugMode) 101 { 102 m_debugMode = debugMode; 103 } 104 105 virtual int getDebugMode() const 106 { 107 return m_debugMode; 108 } 109 110 virtual void flushLines() 111 { 112 int sz = m_linePoints.size(); 113 if (sz) 114 { 115 float debugColor[4]; 116 debugColor[0] = m_currentLineColor.x(); 117 debugColor[1] = m_currentLineColor.y(); 118 debugColor[2] = m_currentLineColor.z(); 119 debugColor[3] = 1.f; 120 m_glApp->m_renderer->drawLines(&m_linePoints[0].x, debugColor, 121 m_linePoints.size(), sizeof(MyDebugVec3), 122 &m_lineIndices[0], 123 m_lineIndices.size(), 124 1); 125 m_linePoints.clear(); 126 m_lineIndices.clear(); 127 } 128 } 129 }; 130 131 static btVector4 sColors[4] = 132 { 133 btVector4(60. / 256., 186. / 256., 84. / 256., 1), 134 btVector4(244. / 256., 194. / 256., 13. / 256., 1), 135 btVector4(219. / 256., 50. / 256., 54. / 256., 1), 136 btVector4(72. / 256., 133. / 256., 237. / 256., 1), 137 138 //btVector4(1,1,0,1), 139 }; 140 141 struct MyHashShape 142 { 143 int m_shapeKey; 144 int m_shapeType; 145 btVector3 m_sphere0Pos; 146 btVector3 m_sphere1Pos; 147 btVector3 m_halfExtents; 148 btScalar m_radius0; 149 btScalar m_radius1; 150 btTransform m_childTransform; 151 int m_deformFunc; 152 int m_upAxis; 153 btScalar m_halfHeight; 154 155 MyHashShape() 156 : m_shapeKey(0), 157 m_shapeType(0), 158 m_sphere0Pos(btVector3(0, 0, 0)), 159 m_sphere1Pos(btVector3(0, 0, 0)), 160 m_halfExtents(btVector3(0, 0, 0)), 161 m_radius0(0), 162 m_radius1(0), 163 m_deformFunc(0), 164 m_upAxis(-1), 165 m_halfHeight(0) 166 { 167 m_childTransform.setIdentity(); 168 } 169 170 bool equals(const MyHashShape& other) const 171 { 172 bool sameShapeType = m_shapeType == other.m_shapeType; 173 bool sameSphere0 = m_sphere0Pos == other.m_sphere0Pos; 174 bool sameSphere1 = m_sphere1Pos == other.m_sphere1Pos; 175 bool sameHalfExtents = m_halfExtents == other.m_halfExtents; 176 bool sameRadius0 = m_radius0 == other.m_radius0; 177 bool sameRadius1 = m_radius1 == other.m_radius1; 178 bool sameTransform = m_childTransform == other.m_childTransform; 179 bool sameUpAxis = m_upAxis == other.m_upAxis; 180 bool sameHalfHeight = m_halfHeight == other.m_halfHeight; 181 return sameShapeType && sameSphere0 && sameSphere1 && sameHalfExtents && sameRadius0 && sameRadius1 && sameTransform && sameUpAxis && sameHalfHeight; 182 } 183 //to our success 184 SIMD_FORCE_INLINE unsigned int getHash() const 185 { 186 unsigned int key = m_shapeKey; 187 // Thomas Wang's hash 188 key += ~(key << 15); 189 key ^= (key >> 10); 190 key += (key << 3); 191 key ^= (key >> 6); 192 key += ~(key << 11); 193 key ^= (key >> 16); 194 195 return key; 196 } 197 }; 198 199 struct OpenGLGuiHelperInternalData 200 { 201 struct CommonGraphicsApp* m_glApp; 202 class MyDebugDrawer* m_debugDraw; 203 bool m_vrMode; 204 int m_vrSkipShadowPass; 205 206 btAlignedObjectArray<unsigned char> m_rgbaPixelBuffer1; 207 btAlignedObjectArray<float> m_depthBuffer1; 208 btAlignedObjectArray<int> m_segmentationMaskBuffer; 209 btHashMap<MyHashShape, int> m_hashShapes; 210 211 VisualizerFlagCallback m_visualizerFlagCallback; 212 213 int m_checkedTexture; 214 int m_checkedTextureGrey; 215 216 OpenGLGuiHelperInternalData() 217 : m_vrMode(false), 218 m_vrSkipShadowPass(0), 219 m_visualizerFlagCallback(0), 220 m_checkedTexture(-1), 221 m_checkedTextureGrey(-1) 222 { 223 } 224 }; 225 226 void OpenGLGuiHelper::setVRMode(bool vrMode) 227 { 228 m_data->m_vrMode = vrMode; 229 m_data->m_vrSkipShadowPass = 0; 230 } 231 232 OpenGLGuiHelper::OpenGLGuiHelper(CommonGraphicsApp* glApp, bool useOpenGL2) 233 { 234 m_data = new OpenGLGuiHelperInternalData; 235 m_data->m_glApp = glApp; 236 m_data->m_debugDraw = 0; 237 } 238 239 240 OpenGLGuiHelper::~OpenGLGuiHelper() 241 { 242 delete m_data->m_debugDraw; 243 244 delete m_data; 245 } 246 247 void OpenGLGuiHelper::setBackgroundColor(const double rgbBackground[3]) 248 { 249 this->getRenderInterface()->setBackgroundColor(rgbBackground); 250 } 251 252 struct CommonRenderInterface* OpenGLGuiHelper::getRenderInterface() 253 { 254 return m_data->m_glApp->m_renderer; 255 } 256 257 const struct CommonRenderInterface* OpenGLGuiHelper::getRenderInterface() const 258 { 259 return m_data->m_glApp->m_renderer; 260 } 261 262 void OpenGLGuiHelper::createRigidBodyGraphicsObject(btRigidBody* body, const btVector3& color) 263 { 264 createCollisionObjectGraphicsObject(body, color); 265 } 266 267 268 class MyTriangleCollector2 : public btTriangleCallback 269 { 270 public: 271 btAlignedObjectArray<GLInstanceVertex>* m_pVerticesOut; 272 btAlignedObjectArray<int>* m_pIndicesOut; 273 btVector3 m_aabbMin, m_aabbMax; 274 btScalar m_textureScaling; 275 276 MyTriangleCollector2(const btVector3& aabbMin, const btVector3& aabbMax) 277 :m_aabbMin(aabbMin), m_aabbMax(aabbMax), m_textureScaling(1) 278 { 279 m_pVerticesOut = 0; 280 m_pIndicesOut = 0; 281 } 282 283 virtual void processTriangle(btVector3* tris, int partId, int triangleIndex) 284 { 285 for (int k = 0; k < 3; k++) 286 { 287 GLInstanceVertex v; 288 v.xyzw[3] = 0; 289 290 btVector3 normal = (tris[0] - tris[1]).cross(tris[0] - tris[2]); 291 normal.safeNormalize(); 292 for (int l = 0; l < 3; l++) 293 { 294 v.xyzw[l] = tris[k][l]; 295 v.normal[l] = normal[l]; 296 } 297 298 btVector3 extents = m_aabbMax - m_aabbMin; 299 300 v.uv[0] = (1.-((v.xyzw[0] - m_aabbMin[0]) / (m_aabbMax[0] - m_aabbMin[0])))*m_textureScaling; 301 v.uv[1] = (1.-(v.xyzw[1] - m_aabbMin[1]) / (m_aabbMax[1] - m_aabbMin[1]))*m_textureScaling; 302 303 m_pIndicesOut->push_back(m_pVerticesOut->size()); 304 m_pVerticesOut->push_back(v); 305 } 306 } 307 }; 308 void OpenGLGuiHelper::createCollisionObjectGraphicsObject(btCollisionObject* body, const btVector3& color) 309 { 310 if (body->getUserIndex() < 0) 311 { 312 btCollisionShape* shape = body->getCollisionShape(); 313 btTransform startTransform = body->getWorldTransform(); 314 int graphicsShapeId = shape->getUserIndex(); 315 if (graphicsShapeId >= 0) 316 { 317 // btAssert(graphicsShapeId >= 0); 318 //the graphics shape is already scaled 319 btVector3 localScaling(1, 1, 1); 320 int graphicsInstanceId = m_data->m_glApp->m_renderer->registerGraphicsInstance(graphicsShapeId, startTransform.getOrigin(), startTransform.getRotation(), color, localScaling); 321 body->setUserIndex(graphicsInstanceId); 322 323 btSoftBody* sb = btSoftBody::upcast(body); 324 if (sb) 325 { 326 int graphicsInstanceId = body->getUserIndex(); 327 changeInstanceFlags(graphicsInstanceId, B3_INSTANCE_DOUBLE_SIDED); 328 } 329 } 330 } 331 } 332 333 int OpenGLGuiHelper::registerTexture(const unsigned char* texels, int width, int height) 334 { 335 int textureId = m_data->m_glApp->m_renderer->registerTexture(texels, width, height); 336 return textureId; 337 } 338 339 void OpenGLGuiHelper::removeTexture(int textureUid) 340 { 341 m_data->m_glApp->m_renderer->removeTexture(textureUid); 342 } 343 344 void OpenGLGuiHelper::changeTexture(int textureUniqueId, const unsigned char* rgbTexels, int width, int height) 345 { 346 bool flipPixelsY = true; 347 m_data->m_glApp->m_renderer->updateTexture(textureUniqueId, rgbTexels, flipPixelsY); 348 } 349 350 int OpenGLGuiHelper::registerGraphicsShape(const float* vertices, int numvertices, const int* indices, int numIndices, int primitiveType, int textureId) 351 { 352 if (textureId == -2) 353 { 354 if (m_data->m_checkedTextureGrey < 0) 355 { 356 m_data->m_checkedTextureGrey = createCheckeredTexture(192, 192, 192); 357 } 358 textureId = m_data->m_checkedTextureGrey; 359 } 360 361 int shapeId = m_data->m_glApp->m_renderer->registerShape(vertices, numvertices, indices, numIndices, primitiveType, textureId); 362 return shapeId; 363 } 364 365 int OpenGLGuiHelper::registerGraphicsInstance(int shapeIndex, const float* position, const float* quaternion, const float* color, const float* scaling) 366 { 367 return m_data->m_glApp->m_renderer->registerGraphicsInstance(shapeIndex, position, quaternion, color, scaling); 368 } 369 370 void OpenGLGuiHelper::removeAllGraphicsInstances() 371 { 372 m_data->m_hashShapes.clear(); 373 m_data->m_glApp->m_renderer->removeAllInstances(); 374 } 375 376 void OpenGLGuiHelper::removeGraphicsInstance(int graphicsUid) 377 { 378 if (graphicsUid >= 0) 379 { 380 m_data->m_glApp->m_renderer->removeGraphicsInstance(graphicsUid); 381 }; 382 } 383 384 int OpenGLGuiHelper::getShapeIndexFromInstance(int instanceUid) 385 { 386 return m_data->m_glApp->m_renderer->getShapeIndexFromInstance(instanceUid); 387 } 388 389 void OpenGLGuiHelper::replaceTexture(int shapeIndex, int textureUid) 390 { 391 if (shapeIndex >= 0) 392 { 393 m_data->m_glApp->m_renderer->replaceTexture(shapeIndex, textureUid); 394 }; 395 } 396 void OpenGLGuiHelper::changeInstanceFlags(int instanceUid, int flags) 397 { 398 if (instanceUid >= 0) 399 { 400 //careful, flags/instanceUid is swapped 401 m_data->m_glApp->m_renderer->writeSingleInstanceFlagsToCPU( flags, instanceUid); 402 } 403 } 404 void OpenGLGuiHelper::changeScaling(int instanceUid, const double scaling[3]) 405 { 406 if (instanceUid >= 0) 407 { 408 m_data->m_glApp->m_renderer->writeSingleInstanceScaleToCPU(scaling, instanceUid); 409 }; 410 } 411 412 void OpenGLGuiHelper::changeRGBAColor(int instanceUid, const double rgbaColor[4]) 413 { 414 if (instanceUid >= 0) 415 { 416 m_data->m_glApp->m_renderer->writeSingleInstanceColorToCPU(rgbaColor, instanceUid); 417 }; 418 } 419 void OpenGLGuiHelper::changeSpecularColor(int instanceUid, const double specularColor[3]) 420 { 421 if (instanceUid >= 0) 422 { 423 m_data->m_glApp->m_renderer->writeSingleInstanceSpecularColorToCPU(specularColor, instanceUid); 424 }; 425 } 426 int OpenGLGuiHelper::createCheckeredTexture(int red, int green, int blue) 427 { 428 int texWidth = 1024; 429 int texHeight = 1024; 430 btAlignedObjectArray<unsigned char> texels; 431 texels.resize(texWidth * texHeight * 3); 432 for (int i = 0; i < texWidth * texHeight * 3; i++) 433 texels[i] = 255; 434 435 for (int i = 0; i < texWidth; i++) 436 { 437 for (int j = 0; j < texHeight; j++) 438 { 439 int a = i < texWidth / 2 ? 1 : 0; 440 int b = j < texWidth / 2 ? 1 : 0; 441 442 if (a == b) 443 { 444 texels[(i + j * texWidth) * 3 + 0] = red; 445 texels[(i + j * texWidth) * 3 + 1] = green; 446 texels[(i + j * texWidth) * 3 + 2] = blue; 447 // texels[(i+j*texWidth)*4+3] = 255; 448 } 449 /*else 450 { 451 texels[i*3+0+j*texWidth] = 255; 452 texels[i*3+1+j*texWidth] = 255; 453 texels[i*3+2+j*texWidth] = 255; 454 } 455 */ 456 } 457 } 458 459 int texId = registerTexture(&texels[0], texWidth, texHeight); 460 return texId; 461 } 462 463 void OpenGLGuiHelper::createCollisionShapeGraphicsObject(btCollisionShape* collisionShape) 464 { 465 //already has a graphics object? 466 if (collisionShape->getUserIndex() >= 0) 467 return; 468 469 if (m_data->m_checkedTexture < 0) 470 { 471 m_data->m_checkedTexture = createCheckeredTexture(173, 199, 255); 472 } 473 474 if (m_data->m_checkedTextureGrey < 0) 475 { 476 m_data->m_checkedTextureGrey = createCheckeredTexture(192, 192, 192); 477 } 478 479 btAlignedObjectArray<GLInstanceVertex> gfxVertices; 480 btAlignedObjectArray<int> indices; 481 int strideInBytes = 9 * sizeof(float); 482 if (collisionShape->getShapeType() == BOX_SHAPE_PROXYTYPE) 483 { 484 btBoxShape* boxShape = (btBoxShape*)collisionShape; 485 486 487 btAlignedObjectArray<float> transformedVertices; 488 489 btVector3 halfExtents = boxShape->getHalfExtentsWithMargin(); 490 491 MyHashShape shape; 492 shape.m_shapeType = boxShape->getShapeType(); 493 shape.m_halfExtents = halfExtents; 494 shape.m_deformFunc = 0; ////no deform 495 int graphicsShapeIndex = -1; 496 int* graphicsShapeIndexPtr = m_data->m_hashShapes[shape]; 497 498 if (graphicsShapeIndexPtr) 499 { 500 graphicsShapeIndex = *graphicsShapeIndexPtr; 501 } 502 else 503 { 504 int numVertices = sizeof(cube_vertices_textured) / strideInBytes; 505 transformedVertices.resize(numVertices * 9); 506 for (int i = 0; i < numVertices; i++) 507 { 508 btVector3 vert; 509 vert.setValue(cube_vertices_textured[i * 9 + 0], 510 cube_vertices_textured[i * 9 + 1], 511 cube_vertices_textured[i * 9 + 2]); 512 513 btVector3 trVer = halfExtents * vert; 514 transformedVertices[i * 9 + 0] = trVer[0]; 515 transformedVertices[i * 9 + 1] = trVer[1]; 516 transformedVertices[i * 9 + 2] = trVer[2]; 517 transformedVertices[i * 9 + 3] = cube_vertices_textured[i * 9 + 3]; 518 transformedVertices[i * 9 + 4] = cube_vertices_textured[i * 9 + 4]; 519 transformedVertices[i * 9 + 5] = cube_vertices_textured[i * 9 + 5]; 520 transformedVertices[i * 9 + 6] = cube_vertices_textured[i * 9 + 6]; 521 transformedVertices[i * 9 + 7] = cube_vertices_textured[i * 9 + 7]; 522 transformedVertices[i * 9 + 8] = cube_vertices_textured[i * 9 + 8]; 523 } 524 525 int numIndices = sizeof(cube_indices) / sizeof(int); 526 graphicsShapeIndex = registerGraphicsShape(&transformedVertices[0], numVertices, cube_indices, numIndices, B3_GL_TRIANGLES, m_data->m_checkedTextureGrey); 527 m_data->m_hashShapes.insert(shape, graphicsShapeIndex); 528 } 529 530 collisionShape->setUserIndex(graphicsShapeIndex); 531 return; 532 } 533 534 535 if (collisionShape->getShapeType() == TERRAIN_SHAPE_PROXYTYPE) 536 { 537 const btHeightfieldTerrainShape* heightField = static_cast<const btHeightfieldTerrainShape*>(collisionShape); 538 539 540 btVector3 aabbMin, aabbMax; 541 btTransform tr; 542 tr.setIdentity(); 543 heightField->getAabb(tr, aabbMin, aabbMax); 544 MyTriangleCollector2 col(aabbMin, aabbMax); 545 if (heightField->getUserValue3()) 546 { 547 col.m_textureScaling = heightField->getUserValue3(); 548 } 549 col.m_pVerticesOut = &gfxVertices; 550 col.m_pIndicesOut = &indices; 551 for (int k = 0; k < 3; k++) 552 { 553 aabbMin[k] = -BT_LARGE_FLOAT; 554 aabbMax[k] = BT_LARGE_FLOAT; 555 } 556 heightField->processAllTriangles(&col, aabbMin, aabbMax); 557 if (gfxVertices.size() && indices.size()) 558 { 559 int userImage = heightField->getUserIndex2(); 560 if (userImage == -1) 561 { 562 userImage = m_data->m_checkedTexture; 563 } 564 int shapeId = m_data->m_glApp->m_renderer->registerShape(&gfxVertices[0].xyzw[0], gfxVertices.size(), &indices[0], indices.size(),1, userImage); 565 collisionShape->setUserIndex(shapeId); 566 } 567 return; 568 } 569 570 571 if (collisionShape->getShapeType() == SOFTBODY_SHAPE_PROXYTYPE) 572 { 573 computeSoftBodyVertices(collisionShape, gfxVertices, indices); 574 if (gfxVertices.size() && indices.size()) 575 { 576 int shapeId = registerGraphicsShape(&gfxVertices[0].xyzw[0], gfxVertices.size(), &indices[0], indices.size(), B3_GL_TRIANGLES,m_data->m_checkedTexture); 577 578 b3Assert(shapeId >= 0); 579 collisionShape->setUserIndex(shapeId); 580 } 581 } 582 if (collisionShape->getShapeType() == MULTI_SPHERE_SHAPE_PROXYTYPE) 583 { 584 btMultiSphereShape* ms = (btMultiSphereShape*)collisionShape; 585 if (ms->getSphereCount() == 2) 586 { 587 btAlignedObjectArray<float> transformedVertices; 588 int numVertices = sizeof(textured_detailed_sphere_vertices) / strideInBytes; 589 transformedVertices.resize(numVertices * 9); 590 btVector3 sphere0Pos = ms->getSpherePosition(0); 591 btVector3 sphere1Pos = ms->getSpherePosition(1); 592 btVector3 fromTo = sphere1Pos - sphere0Pos; 593 MyHashShape shape; 594 shape.m_sphere0Pos = sphere0Pos; 595 shape.m_sphere1Pos = sphere1Pos; 596 shape.m_radius0 = 2. * ms->getSphereRadius(0); 597 shape.m_radius1 = 2. * ms->getSphereRadius(1); 598 shape.m_deformFunc = 1; //vert.dot(fromTo) 599 int graphicsShapeIndex = -1; 600 int* graphicsShapeIndexPtr = m_data->m_hashShapes[shape]; 601 602 if (graphicsShapeIndexPtr) 603 { 604 //cache hit 605 graphicsShapeIndex = *graphicsShapeIndexPtr; 606 } 607 else 608 { 609 //cache miss 610 for (int i = 0; i < numVertices; i++) 611 { 612 btVector3 vert; 613 vert.setValue(textured_detailed_sphere_vertices[i * 9 + 0], 614 textured_detailed_sphere_vertices[i * 9 + 1], 615 textured_detailed_sphere_vertices[i * 9 + 2]); 616 617 btVector3 trVer(0, 0, 0); 618 619 if (vert.dot(fromTo) > 0) 620 { 621 btScalar radiusScale = 2. * ms->getSphereRadius(1); 622 trVer = radiusScale * vert; 623 trVer += sphere1Pos; 624 } 625 else 626 { 627 btScalar radiusScale = 2. * ms->getSphereRadius(0); 628 trVer = radiusScale * vert; 629 trVer += sphere0Pos; 630 } 631 632 transformedVertices[i * 9 + 0] = trVer[0]; 633 transformedVertices[i * 9 + 1] = trVer[1]; 634 transformedVertices[i * 9 + 2] = trVer[2]; 635 transformedVertices[i * 9 + 3] = textured_detailed_sphere_vertices[i * 9 + 3]; 636 transformedVertices[i * 9 + 4] = textured_detailed_sphere_vertices[i * 9 + 4]; 637 transformedVertices[i * 9 + 5] = textured_detailed_sphere_vertices[i * 9 + 5]; 638 transformedVertices[i * 9 + 6] = textured_detailed_sphere_vertices[i * 9 + 6]; 639 transformedVertices[i * 9 + 7] = textured_detailed_sphere_vertices[i * 9 + 7]; 640 transformedVertices[i * 9 + 8] = textured_detailed_sphere_vertices[i * 9 + 8]; 641 } 642 643 int numIndices = sizeof(textured_detailed_sphere_indices) / sizeof(int); 644 graphicsShapeIndex = registerGraphicsShape(&transformedVertices[0], numVertices, textured_detailed_sphere_indices, numIndices, B3_GL_TRIANGLES, m_data->m_checkedTextureGrey); 645 646 m_data->m_hashShapes.insert(shape, graphicsShapeIndex); 647 } 648 collisionShape->setUserIndex(graphicsShapeIndex); 649 return; 650 } 651 } 652 653 654 655 if (collisionShape->getShapeType() == SPHERE_SHAPE_PROXYTYPE) 656 { 657 btSphereShape* sphereShape = (btSphereShape*)collisionShape; 658 btScalar radius = sphereShape->getRadius(); 659 btScalar sphereSize = 2. * radius; 660 btVector3 radiusScale(sphereSize, sphereSize, sphereSize); 661 btAlignedObjectArray<float> transformedVertices; 662 663 MyHashShape shape; 664 shape.m_radius0 = sphereSize; 665 shape.m_deformFunc = 0; ////no deform 666 int graphicsShapeIndex = -1; 667 int* graphicsShapeIndexPtr = m_data->m_hashShapes[shape]; 668 669 if (graphicsShapeIndexPtr) 670 { 671 graphicsShapeIndex = *graphicsShapeIndexPtr; 672 } 673 else 674 { 675 int numVertices = sizeof(textured_detailed_sphere_vertices) / strideInBytes; 676 transformedVertices.resize(numVertices * 9); 677 for (int i = 0; i < numVertices; i++) 678 { 679 btVector3 vert; 680 vert.setValue(textured_detailed_sphere_vertices[i * 9 + 0], 681 textured_detailed_sphere_vertices[i * 9 + 1], 682 textured_detailed_sphere_vertices[i * 9 + 2]); 683 684 btVector3 trVer = radiusScale * vert; 685 transformedVertices[i * 9 + 0] = trVer[0]; 686 transformedVertices[i * 9 + 1] = trVer[1]; 687 transformedVertices[i * 9 + 2] = trVer[2]; 688 transformedVertices[i * 9 + 3] = textured_detailed_sphere_vertices[i * 9 + 3]; 689 transformedVertices[i * 9 + 4] = textured_detailed_sphere_vertices[i * 9 + 4]; 690 transformedVertices[i * 9 + 5] = textured_detailed_sphere_vertices[i * 9 + 5]; 691 transformedVertices[i * 9 + 6] = textured_detailed_sphere_vertices[i * 9 + 6]; 692 transformedVertices[i * 9 + 7] = textured_detailed_sphere_vertices[i * 9 + 7]; 693 transformedVertices[i * 9 + 8] = textured_detailed_sphere_vertices[i * 9 + 8]; 694 } 695 696 int numIndices = sizeof(textured_detailed_sphere_indices) / sizeof(int); 697 graphicsShapeIndex = registerGraphicsShape(&transformedVertices[0], numVertices, textured_detailed_sphere_indices, numIndices, B3_GL_TRIANGLES, m_data->m_checkedTextureGrey); 698 m_data->m_hashShapes.insert(shape, graphicsShapeIndex); 699 } 700 701 collisionShape->setUserIndex(graphicsShapeIndex); 702 return; 703 } 704 if (collisionShape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE) 705 { 706 btCompoundShape* compound = (btCompoundShape*)collisionShape; 707 if (compound->getNumChildShapes() == 1) 708 { 709 if (compound->getChildShape(0)->getShapeType() == SPHERE_SHAPE_PROXYTYPE) 710 { 711 btSphereShape* sphereShape = (btSphereShape*)compound->getChildShape(0); 712 btScalar radius = sphereShape->getRadius(); 713 btScalar sphereSize = 2. * radius; 714 btVector3 radiusScale(sphereSize, sphereSize, sphereSize); 715 716 MyHashShape shape; 717 shape.m_radius0 = sphereSize; 718 shape.m_deformFunc = 0; //no deform 719 shape.m_childTransform = compound->getChildTransform(0); 720 721 int graphicsShapeIndex = -1; 722 int* graphicsShapeIndexPtr = m_data->m_hashShapes[shape]; 723 724 if (graphicsShapeIndexPtr) 725 { 726 graphicsShapeIndex = *graphicsShapeIndexPtr; 727 } 728 else 729 { 730 btAlignedObjectArray<float> transformedVertices; 731 int numVertices = sizeof(textured_detailed_sphere_vertices) / strideInBytes; 732 transformedVertices.resize(numVertices * 9); 733 for (int i = 0; i < numVertices; i++) 734 { 735 btVector3 vert; 736 vert.setValue(textured_detailed_sphere_vertices[i * 9 + 0], 737 textured_detailed_sphere_vertices[i * 9 + 1], 738 textured_detailed_sphere_vertices[i * 9 + 2]); 739 740 btVector3 trVer = compound->getChildTransform(0) * (radiusScale * vert); 741 transformedVertices[i * 9 + 0] = trVer[0]; 742 transformedVertices[i * 9 + 1] = trVer[1]; 743 transformedVertices[i * 9 + 2] = trVer[2]; 744 transformedVertices[i * 9 + 3] = textured_detailed_sphere_vertices[i * 9 + 3]; 745 transformedVertices[i * 9 + 4] = textured_detailed_sphere_vertices[i * 9 + 4]; 746 transformedVertices[i * 9 + 5] = textured_detailed_sphere_vertices[i * 9 + 5]; 747 transformedVertices[i * 9 + 6] = textured_detailed_sphere_vertices[i * 9 + 6]; 748 transformedVertices[i * 9 + 7] = textured_detailed_sphere_vertices[i * 9 + 7]; 749 transformedVertices[i * 9 + 8] = textured_detailed_sphere_vertices[i * 9 + 8]; 750 } 751 752 int numIndices = sizeof(textured_detailed_sphere_indices) / sizeof(int); 753 graphicsShapeIndex = registerGraphicsShape(&transformedVertices[0], numVertices, textured_detailed_sphere_indices, numIndices, B3_GL_TRIANGLES, m_data->m_checkedTextureGrey); 754 m_data->m_hashShapes.insert(shape, graphicsShapeIndex); 755 } 756 757 collisionShape->setUserIndex(graphicsShapeIndex); 758 return; 759 } 760 if (compound->getChildShape(0)->getShapeType() == CAPSULE_SHAPE_PROXYTYPE) 761 { 762 btCapsuleShape* sphereShape = (btCapsuleShape*)compound->getChildShape(0); 763 int up = sphereShape->getUpAxis(); 764 btScalar halfHeight = sphereShape->getHalfHeight(); 765 766 btScalar radius = sphereShape->getRadius(); 767 btScalar sphereSize = 2. * radius; 768 769 btVector3 radiusScale = btVector3(sphereSize, sphereSize, sphereSize); 770 771 MyHashShape shape; 772 shape.m_radius0 = sphereSize; 773 shape.m_deformFunc = 2; //no deform 774 shape.m_childTransform = compound->getChildTransform(0); 775 shape.m_upAxis = up; 776 777 int graphicsShapeIndex = -1; 778 int* graphicsShapeIndexPtr = m_data->m_hashShapes[shape]; 779 780 if (graphicsShapeIndexPtr) 781 { 782 graphicsShapeIndex = *graphicsShapeIndexPtr; 783 } 784 else 785 { 786 btAlignedObjectArray<float> transformedVertices; 787 int numVertices = sizeof(textured_detailed_sphere_vertices) / strideInBytes; 788 transformedVertices.resize(numVertices * 9); 789 for (int i = 0; i < numVertices; i++) 790 { 791 btVector3 vert; 792 vert.setValue(textured_detailed_sphere_vertices[i * 9 + 0], 793 textured_detailed_sphere_vertices[i * 9 + 1], 794 textured_detailed_sphere_vertices[i * 9 + 2]); 795 796 btVector3 trVer = (radiusScale * vert); 797 if (trVer[up] > 0) 798 trVer[up] += halfHeight; 799 else 800 trVer[up] -= halfHeight; 801 802 trVer = compound->getChildTransform(0) * trVer; 803 804 transformedVertices[i * 9 + 0] = trVer[0]; 805 transformedVertices[i * 9 + 1] = trVer[1]; 806 transformedVertices[i * 9 + 2] = trVer[2]; 807 transformedVertices[i * 9 + 3] = textured_detailed_sphere_vertices[i * 9 + 3]; 808 transformedVertices[i * 9 + 4] = textured_detailed_sphere_vertices[i * 9 + 4]; 809 transformedVertices[i * 9 + 5] = textured_detailed_sphere_vertices[i * 9 + 5]; 810 transformedVertices[i * 9 + 6] = textured_detailed_sphere_vertices[i * 9 + 6]; 811 transformedVertices[i * 9 + 7] = textured_detailed_sphere_vertices[i * 9 + 7]; 812 transformedVertices[i * 9 + 8] = textured_detailed_sphere_vertices[i * 9 + 8]; 813 } 814 815 int numIndices = sizeof(textured_detailed_sphere_indices) / sizeof(int); 816 graphicsShapeIndex = registerGraphicsShape(&transformedVertices[0], numVertices, textured_detailed_sphere_indices, numIndices, B3_GL_TRIANGLES, m_data->m_checkedTextureGrey); 817 m_data->m_hashShapes.insert(shape, graphicsShapeIndex); 818 } 819 820 collisionShape->setUserIndex(graphicsShapeIndex); 821 return; 822 } 823 824 if (compound->getChildShape(0)->getShapeType() == MULTI_SPHERE_SHAPE_PROXYTYPE) 825 { 826 btMultiSphereShape* ms = (btMultiSphereShape*)compound->getChildShape(0); 827 if (ms->getSphereCount() == 2) 828 { 829 btAlignedObjectArray<float> transformedVertices; 830 int numVertices = sizeof(textured_detailed_sphere_vertices) / strideInBytes; 831 transformedVertices.resize(numVertices * 9); 832 btVector3 sphere0Pos = ms->getSpherePosition(0); 833 btVector3 sphere1Pos = ms->getSpherePosition(1); 834 btVector3 fromTo = sphere1Pos - sphere0Pos; 835 btScalar radiusScale1 = 2.0 * ms->getSphereRadius(1); 836 btScalar radiusScale0 = 2.0 * ms->getSphereRadius(0); 837 838 MyHashShape shape; 839 shape.m_radius0 = radiusScale0; 840 shape.m_radius1 = radiusScale1; 841 shape.m_deformFunc = 4; 842 shape.m_sphere0Pos = sphere0Pos; 843 shape.m_sphere1Pos = sphere1Pos; 844 shape.m_childTransform = compound->getChildTransform(0); 845 846 int graphicsShapeIndex = -1; 847 int* graphicsShapeIndexPtr = m_data->m_hashShapes[shape]; 848 849 if (graphicsShapeIndexPtr) 850 { 851 graphicsShapeIndex = *graphicsShapeIndexPtr; 852 } 853 else 854 { 855 for (int i = 0; i < numVertices; i++) 856 { 857 btVector3 vert; 858 vert.setValue(textured_detailed_sphere_vertices[i * 9 + 0], 859 textured_detailed_sphere_vertices[i * 9 + 1], 860 textured_detailed_sphere_vertices[i * 9 + 2]); 861 862 btVector3 trVer(0, 0, 0); 863 if (vert.dot(fromTo) > 0) 864 { 865 trVer = vert * radiusScale1; 866 trVer += sphere1Pos; 867 trVer = compound->getChildTransform(0) * trVer; 868 } 869 else 870 { 871 trVer = vert * radiusScale0; 872 trVer += sphere0Pos; 873 trVer = compound->getChildTransform(0) * trVer; 874 } 875 876 transformedVertices[i * 9 + 0] = trVer[0]; 877 transformedVertices[i * 9 + 1] = trVer[1]; 878 transformedVertices[i * 9 + 2] = trVer[2]; 879 transformedVertices[i * 9 + 3] = textured_detailed_sphere_vertices[i * 9 + 3]; 880 transformedVertices[i * 9 + 4] = textured_detailed_sphere_vertices[i * 9 + 4]; 881 transformedVertices[i * 9 + 5] = textured_detailed_sphere_vertices[i * 9 + 5]; 882 transformedVertices[i * 9 + 6] = textured_detailed_sphere_vertices[i * 9 + 6]; 883 transformedVertices[i * 9 + 7] = textured_detailed_sphere_vertices[i * 9 + 7]; 884 transformedVertices[i * 9 + 8] = textured_detailed_sphere_vertices[i * 9 + 8]; 885 } 886 887 int numIndices = sizeof(textured_detailed_sphere_indices) / sizeof(int); 888 graphicsShapeIndex = registerGraphicsShape(&transformedVertices[0], numVertices, textured_detailed_sphere_indices, numIndices, B3_GL_TRIANGLES, m_data->m_checkedTextureGrey); 889 m_data->m_hashShapes.insert(shape, graphicsShapeIndex); 890 } 891 collisionShape->setUserIndex(graphicsShapeIndex); 892 return; 893 } 894 } 895 } 896 } 897 if (collisionShape->getShapeType() == CAPSULE_SHAPE_PROXYTYPE) 898 { 899 btCapsuleShape* sphereShape = (btCapsuleShape*)collisionShape; //Y up 900 int up = sphereShape->getUpAxis(); 901 btScalar halfHeight = sphereShape->getHalfHeight(); 902 903 btScalar radius = sphereShape->getRadius(); 904 btScalar sphereSize = 2. * radius; 905 btVector3 radiusScale(sphereSize, sphereSize, sphereSize); 906 907 MyHashShape shape; 908 shape.m_radius0 = sphereSize; 909 shape.m_deformFunc = 3; 910 shape.m_upAxis = up; 911 shape.m_halfHeight = halfHeight; 912 int graphicsShapeIndex = -1; 913 int* graphicsShapeIndexPtr = m_data->m_hashShapes[shape]; 914 915 if (graphicsShapeIndexPtr) 916 { 917 graphicsShapeIndex = *graphicsShapeIndexPtr; 918 } 919 else 920 { 921 btAlignedObjectArray<float> transformedVertices; 922 int numVertices = sizeof(textured_detailed_sphere_vertices) / strideInBytes; 923 transformedVertices.resize(numVertices * 9); 924 for (int i = 0; i < numVertices; i++) 925 { 926 btVector3 vert; 927 vert.setValue(textured_detailed_sphere_vertices[i * 9 + 0], 928 textured_detailed_sphere_vertices[i * 9 + 1], 929 textured_detailed_sphere_vertices[i * 9 + 2]); 930 931 btVector3 trVer = radiusScale * vert; 932 if (trVer[up] > 0) 933 trVer[up] += halfHeight; 934 else 935 trVer[up] -= halfHeight; 936 937 transformedVertices[i * 9 + 0] = trVer[0]; 938 transformedVertices[i * 9 + 1] = trVer[1]; 939 transformedVertices[i * 9 + 2] = trVer[2]; 940 transformedVertices[i * 9 + 3] = textured_detailed_sphere_vertices[i * 9 + 3]; 941 transformedVertices[i * 9 + 4] = textured_detailed_sphere_vertices[i * 9 + 4]; 942 transformedVertices[i * 9 + 5] = textured_detailed_sphere_vertices[i * 9 + 5]; 943 transformedVertices[i * 9 + 6] = textured_detailed_sphere_vertices[i * 9 + 6]; 944 transformedVertices[i * 9 + 7] = textured_detailed_sphere_vertices[i * 9 + 7]; 945 transformedVertices[i * 9 + 8] = textured_detailed_sphere_vertices[i * 9 + 8]; 946 } 947 948 int numIndices = sizeof(textured_detailed_sphere_indices) / sizeof(int); 949 graphicsShapeIndex = registerGraphicsShape(&transformedVertices[0], numVertices, textured_detailed_sphere_indices, numIndices, B3_GL_TRIANGLES, m_data->m_checkedTextureGrey); 950 m_data->m_hashShapes.insert(shape, graphicsShapeIndex); 951 } 952 collisionShape->setUserIndex(graphicsShapeIndex); 953 return; 954 } 955 if (collisionShape->getShapeType() == STATIC_PLANE_PROXYTYPE) 956 { 957 const btStaticPlaneShape* staticPlaneShape = static_cast<const btStaticPlaneShape*>(collisionShape); 958 btScalar planeConst = staticPlaneShape->getPlaneConstant(); 959 const btVector3& planeNormal = staticPlaneShape->getPlaneNormal(); 960 btVector3 planeOrigin = planeNormal * planeConst; 961 btVector3 vec0, vec1; 962 btPlaneSpace1(planeNormal, vec0, vec1); 963 964 btScalar vecLen = 128; 965 btVector3 verts[4]; 966 967 verts[0] = planeOrigin + vec0 * vecLen + vec1 * vecLen; 968 verts[1] = planeOrigin - vec0 * vecLen + vec1 * vecLen; 969 verts[2] = planeOrigin - vec0 * vecLen - vec1 * vecLen; 970 verts[3] = planeOrigin + vec0 * vecLen - vec1 * vecLen; 971 972 int startIndex = 0; 973 indices.push_back(startIndex + 0); 974 indices.push_back(startIndex + 1); 975 indices.push_back(startIndex + 2); 976 indices.push_back(startIndex + 0); 977 indices.push_back(startIndex + 2); 978 indices.push_back(startIndex + 3); 979 btTransform parentTransform; 980 parentTransform.setIdentity(); 981 btVector3 triNormal = parentTransform.getBasis() * planeNormal; 982 983 gfxVertices.resize(4); 984 985 for (int i = 0; i < 4; i++) 986 { 987 btVector3 vtxPos; 988 btVector3 pos = parentTransform * verts[i]; 989 990 gfxVertices[i].xyzw[0] = pos[0]; 991 gfxVertices[i].xyzw[1] = pos[1]; 992 gfxVertices[i].xyzw[2] = pos[2]; 993 gfxVertices[i].xyzw[3] = 1; 994 gfxVertices[i].normal[0] = triNormal[0]; 995 gfxVertices[i].normal[1] = triNormal[1]; 996 gfxVertices[i].normal[2] = triNormal[2]; 997 } 998 999 //verts[0] = planeOrigin + vec0*vecLen + vec1*vecLen; 1000 //verts[1] = planeOrigin - vec0*vecLen + vec1*vecLen; 1001 //verts[2] = planeOrigin - vec0*vecLen - vec1*vecLen; 1002 //verts[3] = planeOrigin + vec0*vecLen - vec1*vecLen; 1003 1004 gfxVertices[0].uv[0] = vecLen / 2; 1005 gfxVertices[0].uv[1] = vecLen / 2; 1006 gfxVertices[1].uv[0] = -vecLen / 2; 1007 gfxVertices[1].uv[1] = vecLen / 2; 1008 gfxVertices[2].uv[0] = -vecLen / 2; 1009 gfxVertices[2].uv[1] = -vecLen / 2; 1010 gfxVertices[3].uv[0] = vecLen / 2; 1011 gfxVertices[3].uv[1] = -vecLen / 2; 1012 1013 int shapeId = registerGraphicsShape(&gfxVertices[0].xyzw[0], gfxVertices.size(), &indices[0], indices.size(), B3_GL_TRIANGLES, m_data->m_checkedTexture); 1014 collisionShape->setUserIndex(shapeId); 1015 return; 1016 } 1017 1018 btTransform startTrans; 1019 startTrans.setIdentity(); 1020 //todo: create some textured objects for popular objects, like plane, cube, sphere, capsule 1021 1022 { 1023 btAlignedObjectArray<btVector3> vertexPositions; 1024 btAlignedObjectArray<btVector3> vertexNormals; 1025 CollisionShape2TriangleMesh(collisionShape, startTrans, vertexPositions, vertexNormals, indices); 1026 gfxVertices.resize(vertexPositions.size()); 1027 for (int i = 0; i < vertexPositions.size(); i++) 1028 { 1029 for (int j = 0; j < 4; j++) 1030 { 1031 gfxVertices[i].xyzw[j] = vertexPositions[i][j]; 1032 } 1033 for (int j = 0; j < 3; j++) 1034 { 1035 gfxVertices[i].normal[j] = vertexNormals[i][j]; 1036 } 1037 for (int j = 0; j < 2; j++) 1038 { 1039 gfxVertices[i].uv[j] = 0.5; //we don't have UV info... 1040 } 1041 } 1042 } 1043 1044 if (gfxVertices.size() && indices.size()) 1045 { 1046 int shapeId = registerGraphicsShape(&gfxVertices[0].xyzw[0], gfxVertices.size(), &indices[0], indices.size(), B3_GL_TRIANGLES, -1); 1047 collisionShape->setUserIndex(shapeId); 1048 } 1049 } 1050 void OpenGLGuiHelper::syncPhysicsToGraphics(const btDiscreteDynamicsWorld* rbWorld) 1051 { 1052 //in VR mode, we skip the synchronization for the second eye 1053 if (m_data->m_vrMode && m_data->m_vrSkipShadowPass == 1) 1054 return; 1055 1056 int numCollisionObjects = rbWorld->getNumCollisionObjects(); 1057 { 1058 B3_PROFILE("write all InstanceTransformToCPU"); 1059 for (int i = 0; i < numCollisionObjects; i++) 1060 { 1061 //B3_PROFILE("writeSingleInstanceTransformToCPU"); 1062 btCollisionObject* colObj = rbWorld->getCollisionObjectArray()[i]; 1063 btCollisionShape* collisionShape = colObj->getCollisionShape(); 1064 if (collisionShape->getShapeType() == SOFTBODY_SHAPE_PROXYTYPE && collisionShape->getUserIndex() >= 0) 1065 { 1066 const btSoftBody* psb = (const btSoftBody*)colObj; 1067 btAlignedObjectArray<GLInstanceVertex> gfxVertices; 1068 1069 if (psb->m_renderNodes.size() > 0) 1070 { 1071 1072 gfxVertices.resize(psb->m_renderNodes.size()); 1073 for (int i = 0; i < psb->m_renderNodes.size(); i++) // Foreach face 1074 { 1075 gfxVertices[i].xyzw[0] = psb->m_renderNodes[i].m_x[0]; 1076 gfxVertices[i].xyzw[1] = psb->m_renderNodes[i].m_x[1]; 1077 gfxVertices[i].xyzw[2] = psb->m_renderNodes[i].m_x[2]; 1078 gfxVertices[i].xyzw[3] = psb->m_renderNodes[i].m_x[3]; 1079 gfxVertices[i].uv[0] = psb->m_renderNodes[i].m_uv1[0]; 1080 gfxVertices[i].uv[1] = psb->m_renderNodes[i].m_uv1[1]; 1081 //gfxVertices[i].normal[0] = psb->m_renderNodes[i]. 1082 gfxVertices[i].normal[0] = psb->m_renderNodes[i].m_normal[0]; 1083 gfxVertices[i].normal[1] = psb->m_renderNodes[i].m_normal[1]; 1084 gfxVertices[i].normal[2] = psb->m_renderNodes[i].m_normal[2]; 1085 } 1086 } 1087 else 1088 { 1089 btAlignedObjectArray<int> indices; 1090 computeSoftBodyVertices(collisionShape, gfxVertices, indices); 1091 } 1092 m_data->m_glApp->m_renderer->updateShape(collisionShape->getUserIndex(), &gfxVertices[0].xyzw[0], gfxVertices.size()); 1093 continue; 1094 } 1095 btVector3 pos = colObj->getWorldTransform().getOrigin(); 1096 btQuaternion orn = colObj->getWorldTransform().getRotation(); 1097 int index = colObj->getUserIndex(); 1098 if (index >= 0) 1099 { 1100 m_data->m_glApp->m_renderer->writeSingleInstanceTransformToCPU(pos, orn, index); 1101 } 1102 } 1103 } 1104 { 1105 B3_PROFILE("writeTransforms"); 1106 m_data->m_glApp->m_renderer->writeTransforms(); 1107 } 1108 } 1109 1110 void OpenGLGuiHelper::render(const btDiscreteDynamicsWorld* rbWorld) 1111 { 1112 if (m_data->m_vrMode) 1113 { 1114 //in VR, we skip the shadow generation for the second eye 1115 1116 if (m_data->m_vrSkipShadowPass >= 1) 1117 { 1118 m_data->m_glApp->m_renderer->renderSceneInternal(B3_USE_SHADOWMAP_RENDERMODE); 1119 m_data->m_vrSkipShadowPass = 0; 1120 } 1121 else 1122 { 1123 m_data->m_glApp->m_renderer->renderScene(); 1124 m_data->m_vrSkipShadowPass++; 1125 } 1126 } 1127 else 1128 { 1129 m_data->m_glApp->m_renderer->renderScene(); 1130 } 1131 } 1132 void OpenGLGuiHelper::createPhysicsDebugDrawer(btDiscreteDynamicsWorld* rbWorld) 1133 { 1134 btAssert(rbWorld); 1135 if (m_data->m_debugDraw) 1136 { 1137 delete m_data->m_debugDraw; 1138 m_data->m_debugDraw = 0; 1139 } 1140 1141 m_data->m_debugDraw = new MyDebugDrawer(m_data->m_glApp); 1142 rbWorld->setDebugDrawer(m_data->m_debugDraw); 1143 1144 m_data->m_debugDraw->setDebugMode( 1145 btIDebugDraw::DBG_DrawWireframe + btIDebugDraw::DBG_DrawAabb 1146 //btIDebugDraw::DBG_DrawContactPoints 1147 ); 1148 } 1149 1150 struct Common2dCanvasInterface* OpenGLGuiHelper::get2dCanvasInterface() 1151 { 1152 return m_data->m_glApp->m_2dCanvasInterface; 1153 } 1154 1155 CommonParameterInterface* OpenGLGuiHelper::getParameterInterface() 1156 { 1157 return m_data->m_glApp->m_parameterInterface; 1158 } 1159 1160 void OpenGLGuiHelper::setUpAxis(int axis) 1161 { 1162 m_data->m_glApp->setUpAxis(axis); 1163 } 1164 1165 void OpenGLGuiHelper::setVisualizerFlagCallback(VisualizerFlagCallback callback) 1166 { 1167 m_data->m_visualizerFlagCallback = callback; 1168 } 1169 1170 void OpenGLGuiHelper::setVisualizerFlag(int flag, int enable) 1171 { 1172 if (getRenderInterface() && flag == 16) //COV_ENABLE_PLANAR_REFLECTION 1173 { 1174 getRenderInterface()->setPlaneReflectionShapeIndex(enable); 1175 } 1176 if (m_data->m_visualizerFlagCallback) 1177 (m_data->m_visualizerFlagCallback)(flag, enable != 0); 1178 } 1179 1180 void OpenGLGuiHelper::resetCamera(float camDist, float yaw, float pitch, float camPosX, float camPosY, float camPosZ) 1181 { 1182 if (getRenderInterface() && getRenderInterface()->getActiveCamera()) 1183 { 1184 getRenderInterface()->getActiveCamera()->setCameraDistance(camDist); 1185 getRenderInterface()->getActiveCamera()->setCameraPitch(pitch); 1186 getRenderInterface()->getActiveCamera()->setCameraYaw(yaw); 1187 getRenderInterface()->getActiveCamera()->setCameraTargetPosition(camPosX, camPosY, camPosZ); 1188 } 1189 } 1190 1191 bool OpenGLGuiHelper::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 cameraTarget[3]) const 1192 { 1193 if (getRenderInterface() && getRenderInterface()->getActiveCamera()) 1194 { 1195 *width = m_data->m_glApp->m_window->getWidth(); 1196 *height = m_data->m_glApp->m_window->getHeight(); 1197 getRenderInterface()->getActiveCamera()->getCameraViewMatrix(viewMatrix); 1198 getRenderInterface()->getActiveCamera()->getCameraProjectionMatrix(projectionMatrix); 1199 getRenderInterface()->getActiveCamera()->getCameraUpVector(camUp); 1200 getRenderInterface()->getActiveCamera()->getCameraForwardVector(camForward); 1201 1202 float top = 1.f; 1203 float bottom = -1.f; 1204 float tanFov = (top - bottom) * 0.5f / 1; 1205 float fov = btScalar(2.0) * btAtan(tanFov); 1206 btVector3 camPos, camTarget; 1207 getRenderInterface()->getActiveCamera()->getCameraPosition(camPos); 1208 getRenderInterface()->getActiveCamera()->getCameraTargetPosition(camTarget); 1209 btVector3 rayFrom = camPos; 1210 btVector3 rayForward = (camTarget - camPos); 1211 rayForward.normalize(); 1212 float farPlane = 10000.f; 1213 rayForward *= farPlane; 1214 1215 btVector3 rightOffset; 1216 btVector3 cameraUp = btVector3(camUp[0], camUp[1], camUp[2]); 1217 btVector3 vertical = cameraUp; 1218 btVector3 hori; 1219 hori = rayForward.cross(vertical); 1220 hori.normalize(); 1221 vertical = hori.cross(rayForward); 1222 vertical.normalize(); 1223 float tanfov = tanf(0.5f * fov); 1224 hori *= 2.f * farPlane * tanfov; 1225 vertical *= 2.f * farPlane * tanfov; 1226 btScalar aspect = float(*width) / float(*height); 1227 hori *= aspect; 1228 //compute 'hor' and 'vert' vectors, useful to generate raytracer rays 1229 hor[0] = hori[0]; 1230 hor[1] = hori[1]; 1231 hor[2] = hori[2]; 1232 vert[0] = vertical[0]; 1233 vert[1] = vertical[1]; 1234 vert[2] = vertical[2]; 1235 1236 *yaw = getRenderInterface()->getActiveCamera()->getCameraYaw(); 1237 *pitch = getRenderInterface()->getActiveCamera()->getCameraPitch(); 1238 *camDist = getRenderInterface()->getActiveCamera()->getCameraDistance(); 1239 cameraTarget[0] = camTarget[0]; 1240 cameraTarget[1] = camTarget[1]; 1241 cameraTarget[2] = camTarget[2]; 1242 return true; 1243 } 1244 return false; 1245 } 1246 1247 void OpenGLGuiHelper::setProjectiveTextureMatrices(const float viewMatrix[16], const float projectionMatrix[16]) 1248 { 1249 m_data->m_glApp->m_renderer->setProjectiveTextureMatrices(viewMatrix, projectionMatrix); 1250 } 1251 1252 void OpenGLGuiHelper::setProjectiveTexture(bool useProjectiveTexture) 1253 { 1254 m_data->m_glApp->m_renderer->setProjectiveTexture(useProjectiveTexture); 1255 } 1256 1257 void OpenGLGuiHelper::copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16], 1258 unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels, 1259 float* depthBuffer, int depthBufferSizeInPixels, 1260 int* segmentationMaskBuffer, int segmentationMaskBufferSizeInPixels, 1261 int startPixelIndex, int destinationWidth, 1262 int destinationHeight, int* numPixelsCopied) 1263 { 1264 int sourceWidth = btMin(destinationWidth, (int)(m_data->m_glApp->m_window->getWidth() * m_data->m_glApp->m_window->getRetinaScale())); 1265 int sourceHeight = btMin(destinationHeight, (int)(m_data->m_glApp->m_window->getHeight() * m_data->m_glApp->m_window->getRetinaScale())); 1266 m_data->m_glApp->setViewport(sourceWidth, sourceHeight); 1267 1268 if (numPixelsCopied) 1269 *numPixelsCopied = 0; 1270 1271 int numTotalPixels = destinationWidth * destinationHeight; 1272 int numRemainingPixels = numTotalPixels - startPixelIndex; 1273 int numBytesPerPixel = 4; //RGBA 1274 int numRequestedPixels = btMin(rgbaBufferSizeInPixels, numRemainingPixels); 1275 if (numRequestedPixels) 1276 { 1277 if (startPixelIndex == 0) 1278 { 1279 CommonCameraInterface* oldCam = getRenderInterface()->getActiveCamera(); 1280 SimpleCamera tempCam; 1281 getRenderInterface()->setActiveCamera(&tempCam); 1282 getRenderInterface()->getActiveCamera()->setVRCamera(viewMatrix, projectionMatrix); 1283 { 1284 BT_PROFILE("renderScene"); 1285 getRenderInterface()->renderScene(); 1286 } 1287 1288 { 1289 BT_PROFILE("copy pixels"); 1290 btAlignedObjectArray<unsigned char> sourceRgbaPixelBuffer; 1291 btAlignedObjectArray<float> sourceDepthBuffer; 1292 //copy the image into our local cache 1293 sourceRgbaPixelBuffer.resize(sourceWidth * sourceHeight * numBytesPerPixel); 1294 sourceDepthBuffer.resize(sourceWidth * sourceHeight); 1295 { 1296 BT_PROFILE("getScreenPixels"); 1297 m_data->m_glApp->getScreenPixels(&(sourceRgbaPixelBuffer[0]), sourceRgbaPixelBuffer.size(), &sourceDepthBuffer[0], sizeof(float) * sourceDepthBuffer.size()); 1298 } 1299 1300 m_data->m_rgbaPixelBuffer1.resize(destinationWidth * destinationHeight * numBytesPerPixel); 1301 m_data->m_depthBuffer1.resize(destinationWidth * destinationHeight); 1302 //rescale and flip 1303 { 1304 BT_PROFILE("resize and flip"); 1305 for (int j = 0; j < destinationHeight; j++) 1306 { 1307 for (int i = 0; i < destinationWidth; i++) 1308 { 1309 int xIndex = int(float(i) * (float(sourceWidth) / float(destinationWidth))); 1310 int yIndex = int(float(destinationHeight - 1 - j) * (float(sourceHeight) / float(destinationHeight))); 1311 btClamp(xIndex, 0, sourceWidth); 1312 btClamp(yIndex, 0, sourceHeight); 1313 int bytesPerPixel = 4; //RGBA 1314 1315 int sourcePixelIndex = (xIndex + yIndex * sourceWidth) * bytesPerPixel; 1316 int sourceDepthIndex = xIndex + yIndex * sourceWidth; 1317 #define COPY4PIXELS 1 1318 #ifdef COPY4PIXELS 1319 int* dst = (int*)&m_data->m_rgbaPixelBuffer1[(i + j * destinationWidth) * 4 + 0]; 1320 int* src = (int*)&sourceRgbaPixelBuffer[sourcePixelIndex + 0]; 1321 *dst = *src; 1322 1323 #else 1324 m_data->m_rgbaPixelBuffer1[(i + j * destinationWidth) * 4 + 0] = sourceRgbaPixelBuffer[sourcePixelIndex + 0]; 1325 m_data->m_rgbaPixelBuffer1[(i + j * destinationWidth) * 4 + 1] = sourceRgbaPixelBuffer[sourcePixelIndex + 1]; 1326 m_data->m_rgbaPixelBuffer1[(i + j * destinationWidth) * 4 + 2] = sourceRgbaPixelBuffer[sourcePixelIndex + 2]; 1327 m_data->m_rgbaPixelBuffer1[(i + j * destinationWidth) * 4 + 3] = 255; 1328 #endif 1329 if (depthBuffer) 1330 { 1331 m_data->m_depthBuffer1[i + j * destinationWidth] = sourceDepthBuffer[sourceDepthIndex]; 1332 } 1333 } 1334 } 1335 } 1336 } 1337 1338 //segmentation mask 1339 1340 if (segmentationMaskBuffer) 1341 { 1342 { 1343 m_data->m_glApp->m_window->startRendering(); 1344 m_data->m_glApp->setViewport(sourceWidth, sourceHeight); 1345 BT_PROFILE("renderScene"); 1346 getRenderInterface()->renderSceneInternal(B3_SEGMENTATION_MASK_RENDERMODE); 1347 } 1348 1349 { 1350 BT_PROFILE("copy pixels"); 1351 btAlignedObjectArray<unsigned char> sourceRgbaPixelBuffer; 1352 btAlignedObjectArray<float> sourceDepthBuffer; 1353 //copy the image into our local cache 1354 sourceRgbaPixelBuffer.resize(sourceWidth * sourceHeight * numBytesPerPixel); 1355 sourceDepthBuffer.resize(sourceWidth * sourceHeight); 1356 { 1357 BT_PROFILE("getScreenPixelsSegmentationMask"); 1358 m_data->m_glApp->getScreenPixels(&(sourceRgbaPixelBuffer[0]), sourceRgbaPixelBuffer.size(), &sourceDepthBuffer[0], sizeof(float) * sourceDepthBuffer.size()); 1359 } 1360 m_data->m_segmentationMaskBuffer.resize(destinationWidth * destinationHeight, -1); 1361 1362 //rescale and flip 1363 { 1364 BT_PROFILE("resize and flip segmentation mask"); 1365 for (int j = 0; j < destinationHeight; j++) 1366 { 1367 for (int i = 0; i < destinationWidth; i++) 1368 { 1369 int xIndex = int(float(i) * (float(sourceWidth) / float(destinationWidth))); 1370 int yIndex = int(float(destinationHeight - 1 - j) * (float(sourceHeight) / float(destinationHeight))); 1371 btClamp(xIndex, 0, sourceWidth); 1372 btClamp(yIndex, 0, sourceHeight); 1373 int bytesPerPixel = 4; //RGBA 1374 int sourcePixelIndex = (xIndex + yIndex * sourceWidth) * bytesPerPixel; 1375 int sourceDepthIndex = xIndex + yIndex * sourceWidth; 1376 1377 if (segmentationMaskBuffer) 1378 { 1379 float depth = sourceDepthBuffer[sourceDepthIndex]; 1380 if (depth < 1) 1381 { 1382 int segMask = sourceRgbaPixelBuffer[sourcePixelIndex + 0] + 256 * (sourceRgbaPixelBuffer[sourcePixelIndex + 1]) + 256 * 256 * (sourceRgbaPixelBuffer[sourcePixelIndex + 2]); 1383 m_data->m_segmentationMaskBuffer[i + j * destinationWidth] = segMask; 1384 } 1385 else 1386 { 1387 m_data->m_segmentationMaskBuffer[i + j * destinationWidth] = -1; 1388 } 1389 } 1390 } 1391 } 1392 } 1393 } 1394 } 1395 1396 getRenderInterface()->setActiveCamera(oldCam); 1397 1398 if (1) 1399 { 1400 getRenderInterface()->getActiveCamera()->disableVRCamera(); 1401 DrawGridData dg; 1402 dg.upAxis = m_data->m_glApp->getUpAxis(); 1403 getRenderInterface()->updateCamera(dg.upAxis); 1404 m_data->m_glApp->m_window->startRendering(); 1405 } 1406 } 1407 if (pixelsRGBA) 1408 { 1409 BT_PROFILE("copy rgba pixels"); 1410 1411 for (int i = 0; i < numRequestedPixels * numBytesPerPixel; i++) 1412 { 1413 pixelsRGBA[i] = m_data->m_rgbaPixelBuffer1[i + startPixelIndex * numBytesPerPixel]; 1414 } 1415 } 1416 if (depthBuffer) 1417 { 1418 BT_PROFILE("copy depth buffer pixels"); 1419 1420 for (int i = 0; i < numRequestedPixels; i++) 1421 { 1422 depthBuffer[i] = m_data->m_depthBuffer1[i + startPixelIndex]; 1423 } 1424 } 1425 if (segmentationMaskBuffer) 1426 { 1427 BT_PROFILE("copy segmentation mask pixels"); 1428 for (int i = 0; i < numRequestedPixels; i++) 1429 { 1430 segmentationMaskBuffer[i] = m_data->m_segmentationMaskBuffer[i + startPixelIndex]; 1431 } 1432 } 1433 if (numPixelsCopied) 1434 *numPixelsCopied = numRequestedPixels; 1435 } 1436 1437 m_data->m_glApp->setViewport(-1, -1); 1438 } 1439 1440 struct MyConvertPointerSizeT 1441 { 1442 union { 1443 const void* m_ptr; 1444 size_t m_int; 1445 }; 1446 }; 1447 bool shapePointerCompareFunc(const btCollisionObject* colA, const btCollisionObject* colB) 1448 { 1449 MyConvertPointerSizeT a, b; 1450 a.m_ptr = colA->getCollisionShape(); 1451 b.m_ptr = colB->getCollisionShape(); 1452 return (a.m_int < b.m_int); 1453 } 1454 1455 void OpenGLGuiHelper::autogenerateGraphicsObjects(btDiscreteDynamicsWorld* rbWorld) 1456 { 1457 //sort the collision objects based on collision shape, the gfx library requires instances that re-use a shape to be added after eachother 1458 1459 btAlignedObjectArray<btCollisionObject*> sortedObjects; 1460 sortedObjects.reserve(rbWorld->getNumCollisionObjects()); 1461 for (int i = 0; i < rbWorld->getNumCollisionObjects(); i++) 1462 { 1463 btCollisionObject* colObj = rbWorld->getCollisionObjectArray()[i]; 1464 sortedObjects.push_back(colObj); 1465 } 1466 sortedObjects.quickSort(shapePointerCompareFunc); 1467 for (int i = 0; i < sortedObjects.size(); i++) 1468 { 1469 btCollisionObject* colObj = sortedObjects[i]; 1470 //btRigidBody* body = btRigidBody::upcast(colObj); 1471 //does this also work for btMultiBody/btMultiBodyLinkCollider? 1472 btSoftBody* sb = btSoftBody::upcast(colObj); 1473 if (sb) 1474 { 1475 colObj->getCollisionShape()->setUserPointer(sb); 1476 } 1477 createCollisionShapeGraphicsObject(colObj->getCollisionShape()); 1478 int colorIndex = colObj->getBroadphaseHandle()->getUid() & 3; 1479 1480 btVector4 color; 1481 color = sColors[colorIndex]; 1482 if (colObj->getCollisionShape()->getShapeType() == STATIC_PLANE_PROXYTYPE) 1483 { 1484 color.setValue(1, 1, 1, 1); 1485 } 1486 createCollisionObjectGraphicsObject(colObj, color); 1487 1488 } 1489 } 1490 1491 void OpenGLGuiHelper::drawText3D(const char* txt, float position[3], float orientation[4], float color[4], float size, int optionFlags) 1492 { 1493 B3_PROFILE("OpenGLGuiHelper::drawText3D"); 1494 1495 btAssert(m_data->m_glApp); 1496 m_data->m_glApp->drawText3D(txt, position, orientation, color, size, optionFlags); 1497 } 1498 1499 void OpenGLGuiHelper::drawText3D(const char* txt, float posX, float posY, float posZ, float size) 1500 { 1501 B3_PROFILE("OpenGLGuiHelper::drawText3D"); 1502 1503 btAssert(m_data->m_glApp); 1504 m_data->m_glApp->drawText3D(txt, posX, posY, posZ, size); 1505 } 1506 1507 struct CommonGraphicsApp* OpenGLGuiHelper::getAppInterface() 1508 { 1509 return m_data->m_glApp; 1510 } 1511 1512 void OpenGLGuiHelper::dumpFramesToVideo(const char* mp4FileName) 1513 { 1514 if (m_data->m_glApp) 1515 { 1516 m_data->m_glApp->dumpFramesToVideo(mp4FileName); 1517 } 1518 } 1519 1520 void OpenGLGuiHelper::computeSoftBodyVertices(btCollisionShape* collisionShape, 1521 btAlignedObjectArray<GLInstanceVertex>& gfxVertices, 1522 btAlignedObjectArray<int>& indices) 1523 { 1524 if (collisionShape->getUserPointer() == 0) 1525 return; 1526 b3Assert(collisionShape->getUserPointer()); 1527 btSoftBody* psb = (btSoftBody*)collisionShape->getUserPointer(); 1528 gfxVertices.resize(psb->m_faces.size() * 3); 1529 1530 for (int i = 0; i < psb->m_faces.size(); i++) // Foreach face 1531 { 1532 for (int k = 0; k < 3; k++) // Foreach vertex on a face 1533 { 1534 int currentIndex = i * 3 + k; 1535 for (int j = 0; j < 3; j++) 1536 { 1537 gfxVertices[currentIndex].xyzw[j] = psb->m_faces[i].m_n[k]->m_x[j]; 1538 } 1539 for (int j = 0; j < 3; j++) 1540 { 1541 gfxVertices[currentIndex].normal[j] = psb->m_faces[i].m_n[k]->m_n[j]; 1542 } 1543 for (int j = 0; j < 2; j++) 1544 { 1545 gfxVertices[currentIndex].uv[j] = psb->m_faces[i].m_n[k]->m_x[j]; 1546 } 1547 indices.push_back(currentIndex); 1548 } 1549 } 1550 } 1551 1552 void OpenGLGuiHelper::updateShape(int shapeIndex, float* vertices, int numVertices) 1553 { 1554 m_data->m_glApp->m_renderer->updateShape(shapeIndex, vertices, numVertices); 1555 }