OpenGLGuiHelper.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. #include "OpenGLGuiHelper.h"
  2. #include "btBulletDynamicsCommon.h"
  3. #include "../CommonInterfaces/CommonGraphicsAppInterface.h"
  4. #include "../CommonInterfaces/CommonRenderInterface.h"
  5. #include "Bullet3Common/b3Scalar.h"
  6. #include "CollisionShape2TriangleMesh.h"
  7. #include "../OpenGLWindow/SimpleCamera.h"
  8. #include "../OpenGLWindow/GLInstanceGraphicsShape.h"
  9. //backwards compatibility
  10. #include "GL_ShapeDrawer.h"
  11. #define BT_LINE_BATCH_SIZE 512
  12. struct MyDebugVec3
  13. {
  14. MyDebugVec3(const btVector3& org)
  15. :x(org.x()),
  16. y(org.y()),
  17. z(org.z())
  18. {
  19. }
  20. float x;
  21. float y;
  22. float z;
  23. };
  24. class MyDebugDrawer : public btIDebugDraw
  25. {
  26. CommonGraphicsApp* m_glApp;
  27. int m_debugMode;
  28. btAlignedObjectArray<MyDebugVec3> m_linePoints;
  29. btAlignedObjectArray<unsigned int> m_lineIndices;
  30. btVector3 m_currentLineColor;
  31. DefaultColors m_ourColors;
  32. public:
  33. MyDebugDrawer(CommonGraphicsApp* app)
  34. : m_glApp(app)
  35. ,m_debugMode(btIDebugDraw::DBG_DrawWireframe|btIDebugDraw::DBG_DrawAabb),
  36. m_currentLineColor(-1,-1,-1)
  37. {
  38. }
  39. virtual DefaultColors getDefaultColors() const
  40. {
  41. return m_ourColors;
  42. }
  43. ///the default implementation for setDefaultColors has no effect. A derived class can implement it and store the colors.
  44. virtual void setDefaultColors(const DefaultColors& colors)
  45. {
  46. m_ourColors = colors;
  47. }
  48. virtual void drawLine(const btVector3& from1,const btVector3& to1,const btVector3& color1)
  49. {
  50. //float from[4] = {from1[0],from1[1],from1[2],from1[3]};
  51. //float to[4] = {to1[0],to1[1],to1[2],to1[3]};
  52. //float color[4] = {color1[0],color1[1],color1[2],color1[3]};
  53. //m_glApp->m_instancingRenderer->drawLine(from,to,color);
  54. if (m_currentLineColor!=color1 || m_linePoints.size() >= BT_LINE_BATCH_SIZE)
  55. {
  56. flushLines();
  57. m_currentLineColor = color1;
  58. }
  59. MyDebugVec3 from(from1);
  60. MyDebugVec3 to(to1);
  61. m_linePoints.push_back(from);
  62. m_linePoints.push_back(to);
  63. m_lineIndices.push_back(m_lineIndices.size());
  64. m_lineIndices.push_back(m_lineIndices.size());
  65. }
  66. virtual void drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color)
  67. {
  68. drawLine(PointOnB,PointOnB+normalOnB*distance,color);
  69. btVector3 ncolor(0, 0, 0);
  70. drawLine(PointOnB, PointOnB + normalOnB*0.01, ncolor);
  71. }
  72. virtual void reportErrorWarning(const char* warningString)
  73. {
  74. }
  75. virtual void draw3dText(const btVector3& location,const char* textString)
  76. {
  77. }
  78. virtual void setDebugMode(int debugMode)
  79. {
  80. m_debugMode = debugMode;
  81. }
  82. virtual int getDebugMode() const
  83. {
  84. return m_debugMode;
  85. }
  86. virtual void flushLines()
  87. {
  88. int sz = m_linePoints.size();
  89. if (sz)
  90. {
  91. float debugColor[4];
  92. debugColor[0] = m_currentLineColor.x();
  93. debugColor[1] = m_currentLineColor.y();
  94. debugColor[2] = m_currentLineColor.z();
  95. debugColor[3] = 1.f;
  96. m_glApp->m_renderer->drawLines(&m_linePoints[0].x,debugColor,
  97. m_linePoints.size(),sizeof(MyDebugVec3),
  98. &m_lineIndices[0],
  99. m_lineIndices.size(),
  100. 1);
  101. m_linePoints.clear();
  102. m_lineIndices.clear();
  103. }
  104. }
  105. };
  106. static btVector4 sColors[4] =
  107. {
  108. btVector4(0.3,0.3,1,1),
  109. btVector4(0.6,0.6,1,1),
  110. btVector4(0,1,0,1),
  111. btVector4(0,1,1,1),
  112. //btVector4(1,1,0,1),
  113. };
  114. struct OpenGLGuiHelperInternalData
  115. {
  116. struct CommonGraphicsApp* m_glApp;
  117. class MyDebugDrawer* m_debugDraw;
  118. GL_ShapeDrawer* m_gl2ShapeDrawer;
  119. bool m_vrMode;
  120. int m_vrSkipShadowPass;
  121. btAlignedObjectArray<unsigned char> m_rgbaPixelBuffer1;
  122. btAlignedObjectArray<float> m_depthBuffer1;
  123. OpenGLGuiHelperInternalData()
  124. :m_vrMode(false),
  125. m_vrSkipShadowPass(0)
  126. {
  127. }
  128. };
  129. void OpenGLGuiHelper::setVRMode(bool vrMode)
  130. {
  131. m_data->m_vrMode = vrMode;
  132. m_data->m_vrSkipShadowPass = 0;
  133. }
  134. OpenGLGuiHelper::OpenGLGuiHelper(CommonGraphicsApp* glApp, bool useOpenGL2)
  135. {
  136. m_data = new OpenGLGuiHelperInternalData;
  137. m_data->m_glApp = glApp;
  138. m_data->m_debugDraw = 0;
  139. m_data->m_gl2ShapeDrawer = 0;
  140. if (useOpenGL2)
  141. {
  142. m_data->m_gl2ShapeDrawer = new GL_ShapeDrawer();
  143. }
  144. }
  145. OpenGLGuiHelper::~OpenGLGuiHelper()
  146. {
  147. delete m_data->m_debugDraw;
  148. delete m_data->m_gl2ShapeDrawer;
  149. delete m_data;
  150. }
  151. struct CommonRenderInterface* OpenGLGuiHelper::getRenderInterface()
  152. {
  153. return m_data->m_glApp->m_renderer;
  154. }
  155. void OpenGLGuiHelper::createRigidBodyGraphicsObject(btRigidBody* body, const btVector3& color)
  156. {
  157. createCollisionObjectGraphicsObject(body,color);
  158. }
  159. void OpenGLGuiHelper::createCollisionObjectGraphicsObject(btCollisionObject* body, const btVector3& color)
  160. {
  161. if (body->getUserIndex()<0)
  162. {
  163. btCollisionShape* shape = body->getCollisionShape();
  164. btTransform startTransform = body->getWorldTransform();
  165. int graphicsShapeId = shape->getUserIndex();
  166. if (graphicsShapeId>=0)
  167. {
  168. // btAssert(graphicsShapeId >= 0);
  169. //the graphics shape is already scaled
  170. btVector3 localScaling(1,1,1);
  171. int graphicsInstanceId = m_data->m_glApp->m_renderer->registerGraphicsInstance(graphicsShapeId, startTransform.getOrigin(), startTransform.getRotation(), color, localScaling);
  172. body->setUserIndex(graphicsInstanceId);
  173. }
  174. }
  175. }
  176. int OpenGLGuiHelper::registerTexture(const unsigned char* texels, int width, int height)
  177. {
  178. int textureId = m_data->m_glApp->m_renderer->registerTexture(texels,width,height);
  179. return textureId;
  180. }
  181. int OpenGLGuiHelper::registerGraphicsShape(const float* vertices, int numvertices, const int* indices, int numIndices,int primitiveType, int textureId)
  182. {
  183. int shapeId = m_data->m_glApp->m_renderer->registerShape(vertices, numvertices,indices,numIndices,primitiveType, textureId);
  184. return shapeId;
  185. }
  186. int OpenGLGuiHelper::registerGraphicsInstance(int shapeIndex, const float* position, const float* quaternion, const float* color, const float* scaling)
  187. {
  188. return m_data->m_glApp->m_renderer->registerGraphicsInstance(shapeIndex,position,quaternion,color,scaling);
  189. }
  190. void OpenGLGuiHelper::removeAllGraphicsInstances()
  191. {
  192. m_data->m_glApp->m_renderer->removeAllInstances();
  193. }
  194. void OpenGLGuiHelper::createCollisionShapeGraphicsObject(btCollisionShape* collisionShape)
  195. {
  196. //already has a graphics object?
  197. if (collisionShape->getUserIndex()>=0)
  198. return;
  199. btAlignedObjectArray<GLInstanceVertex> gfxVertices;
  200. btAlignedObjectArray<int> indices;
  201. btTransform startTrans;startTrans.setIdentity();
  202. {
  203. btAlignedObjectArray<btVector3> vertexPositions;
  204. btAlignedObjectArray<btVector3> vertexNormals;
  205. CollisionShape2TriangleMesh(collisionShape,startTrans,vertexPositions,vertexNormals,indices);
  206. gfxVertices.resize(vertexPositions.size());
  207. for (int i=0;i<vertexPositions.size();i++)
  208. {
  209. for (int j=0;j<4;j++)
  210. {
  211. gfxVertices[i].xyzw[j] = vertexPositions[i][j];
  212. }
  213. for (int j=0;j<3;j++)
  214. {
  215. gfxVertices[i].normal[j] = vertexNormals[i][j];
  216. }
  217. for (int j=0;j<2;j++)
  218. {
  219. gfxVertices[i].uv[j] = 0.5;//we don't have UV info...
  220. }
  221. }
  222. }
  223. if (gfxVertices.size() && indices.size())
  224. {
  225. int shapeId = registerGraphicsShape(&gfxVertices[0].xyzw[0],gfxVertices.size(),&indices[0],indices.size(),B3_GL_TRIANGLES,-1);
  226. collisionShape->setUserIndex(shapeId);
  227. }
  228. }
  229. void OpenGLGuiHelper::syncPhysicsToGraphics(const btDiscreteDynamicsWorld* rbWorld)
  230. {
  231. //in VR mode, we skip the synchronization for the second eye
  232. if (m_data->m_vrMode && m_data->m_vrSkipShadowPass==1)
  233. return;
  234. int numCollisionObjects = rbWorld->getNumCollisionObjects();
  235. {
  236. B3_PROFILE("write all InstanceTransformToCPU");
  237. for (int i = 0; i<numCollisionObjects; i++)
  238. {
  239. B3_PROFILE("writeSingleInstanceTransformToCPU");
  240. btCollisionObject* colObj = rbWorld->getCollisionObjectArray()[i];
  241. btVector3 pos = colObj->getWorldTransform().getOrigin();
  242. btQuaternion orn = colObj->getWorldTransform().getRotation();
  243. int index = colObj->getUserIndex();
  244. if (index >= 0)
  245. {
  246. m_data->m_glApp->m_renderer->writeSingleInstanceTransformToCPU(pos, orn, index);
  247. }
  248. }
  249. }
  250. {
  251. B3_PROFILE("writeTransforms");
  252. m_data->m_glApp->m_renderer->writeTransforms();
  253. }
  254. }
  255. void OpenGLGuiHelper::render(const btDiscreteDynamicsWorld* rbWorld)
  256. {
  257. if (m_data->m_vrMode)
  258. {
  259. //in VR, we skip the shadow generation for the second eye
  260. if (m_data->m_vrSkipShadowPass>=1)
  261. {
  262. m_data->m_glApp->m_renderer->renderSceneInternal(B3_USE_SHADOWMAP_RENDERMODE);
  263. m_data->m_vrSkipShadowPass=0;
  264. } else
  265. {
  266. m_data->m_glApp->m_renderer->renderScene();
  267. m_data->m_vrSkipShadowPass++;
  268. }
  269. } else
  270. {
  271. m_data->m_glApp->m_renderer->renderScene();
  272. }
  273. //backwards compatible OpenGL2 rendering
  274. if (m_data->m_gl2ShapeDrawer && rbWorld)
  275. {
  276. m_data->m_gl2ShapeDrawer->enableTexture(true);
  277. m_data->m_gl2ShapeDrawer->drawScene(rbWorld,true);
  278. }
  279. }
  280. void OpenGLGuiHelper::createPhysicsDebugDrawer(btDiscreteDynamicsWorld* rbWorld)
  281. {
  282. btAssert(rbWorld);
  283. m_data->m_debugDraw = new MyDebugDrawer(m_data->m_glApp);
  284. rbWorld->setDebugDrawer(m_data->m_debugDraw );
  285. m_data->m_debugDraw->setDebugMode(
  286. btIDebugDraw::DBG_DrawWireframe
  287. +btIDebugDraw::DBG_DrawAabb
  288. //btIDebugDraw::DBG_DrawContactPoints
  289. );
  290. }
  291. struct Common2dCanvasInterface* OpenGLGuiHelper::get2dCanvasInterface()
  292. {
  293. return m_data->m_glApp->m_2dCanvasInterface;
  294. }
  295. CommonParameterInterface* OpenGLGuiHelper::getParameterInterface()
  296. {
  297. return m_data->m_glApp->m_parameterInterface;
  298. }
  299. void OpenGLGuiHelper::setUpAxis(int axis)
  300. {
  301. m_data->m_glApp->setUpAxis(axis);
  302. }
  303. void OpenGLGuiHelper::resetCamera(float camDist, float pitch, float yaw, float camPosX,float camPosY, float camPosZ)
  304. {
  305. if (getRenderInterface() && getRenderInterface()->getActiveCamera())
  306. {
  307. getRenderInterface()->getActiveCamera()->setCameraDistance(camDist);
  308. getRenderInterface()->getActiveCamera()->setCameraPitch(pitch);
  309. getRenderInterface()->getActiveCamera()->setCameraYaw(yaw);
  310. getRenderInterface()->getActiveCamera()->setCameraTargetPosition(camPosX,camPosY,camPosZ);
  311. }
  312. }
  313. void OpenGLGuiHelper::copyCameraImageData(const float viewMatrix[16], const float projectionMatrix[16],
  314. unsigned char* pixelsRGBA, int rgbaBufferSizeInPixels,
  315. float* depthBuffer, int depthBufferSizeInPixels,
  316. int* segmentationMaskBuffer, int segmentationMaskBufferSizeInPixels,
  317. int startPixelIndex, int destinationWidth,
  318. int destinationHeight, int* numPixelsCopied)
  319. {
  320. int sourceWidth = m_data->m_glApp->m_window->getWidth()*m_data->m_glApp->m_window->getRetinaScale();
  321. int sourceHeight = m_data->m_glApp->m_window->getHeight()*m_data->m_glApp->m_window->getRetinaScale();
  322. if (numPixelsCopied)
  323. *numPixelsCopied = 0;
  324. int numTotalPixels = destinationWidth*destinationHeight;
  325. int numRemainingPixels = numTotalPixels - startPixelIndex;
  326. int numBytesPerPixel = 4;//RGBA
  327. int numRequestedPixels = btMin(rgbaBufferSizeInPixels,numRemainingPixels);
  328. if (numRequestedPixels)
  329. {
  330. if (startPixelIndex==0)
  331. {
  332. CommonCameraInterface* oldCam = getRenderInterface()->getActiveCamera();
  333. SimpleCamera tempCam;
  334. getRenderInterface()->setActiveCamera(&tempCam);
  335. getRenderInterface()->getActiveCamera()->setVRCamera(viewMatrix,projectionMatrix);
  336. getRenderInterface()->renderScene();
  337. getRenderInterface()->setActiveCamera(oldCam);
  338. {
  339. btAlignedObjectArray<unsigned char> sourceRgbaPixelBuffer;
  340. btAlignedObjectArray<float> sourceDepthBuffer;
  341. //copy the image into our local cache
  342. sourceRgbaPixelBuffer.resize(sourceWidth*sourceHeight*numBytesPerPixel);
  343. sourceDepthBuffer.resize(sourceWidth*sourceHeight);
  344. m_data->m_glApp->getScreenPixels(&(sourceRgbaPixelBuffer[0]),sourceRgbaPixelBuffer.size(), &sourceDepthBuffer[0],sizeof(float)*sourceDepthBuffer.size());
  345. m_data->m_rgbaPixelBuffer1.resize(destinationWidth*destinationHeight*numBytesPerPixel);
  346. m_data->m_depthBuffer1.resize(destinationWidth*destinationHeight);
  347. //rescale and flip
  348. for (int i=0;i<destinationWidth;i++)
  349. {
  350. for (int j=0;j<destinationHeight;j++)
  351. {
  352. int xIndex = int(float(i)*(float(sourceWidth)/float(destinationWidth)));
  353. int yIndex = int(float(destinationHeight-1-j)*(float(sourceHeight)/float(destinationHeight)));
  354. btClamp(xIndex,0,sourceWidth);
  355. btClamp(yIndex,0,sourceHeight);
  356. int bytesPerPixel = 4; //RGBA
  357. int sourcePixelIndex = (xIndex+yIndex*sourceWidth)*bytesPerPixel;
  358. m_data->m_rgbaPixelBuffer1[(i+j*destinationWidth)*4+0] = sourceRgbaPixelBuffer[sourcePixelIndex+0];
  359. m_data->m_rgbaPixelBuffer1[(i+j*destinationWidth)*4+1] = sourceRgbaPixelBuffer[sourcePixelIndex+1];
  360. m_data->m_rgbaPixelBuffer1[(i+j*destinationWidth)*4+2] = sourceRgbaPixelBuffer[sourcePixelIndex+2];
  361. m_data->m_rgbaPixelBuffer1[(i+j*destinationWidth)*4+3] = 255;
  362. }
  363. }
  364. }
  365. }
  366. if (pixelsRGBA)
  367. {
  368. for (int i=0;i<numRequestedPixels*numBytesPerPixel;i++)
  369. {
  370. pixelsRGBA[i] = m_data->m_rgbaPixelBuffer1[i+startPixelIndex*numBytesPerPixel];
  371. }
  372. }
  373. if (depthBuffer)
  374. {
  375. for (int i=0;i<numRequestedPixels;i++)
  376. {
  377. depthBuffer[i] = m_data->m_depthBuffer1[i];
  378. }
  379. }
  380. if (numPixelsCopied)
  381. *numPixelsCopied = numRequestedPixels;
  382. }
  383. }
  384. struct MyConvertPointerSizeT
  385. {
  386. union
  387. {
  388. const void* m_ptr;
  389. size_t m_int;
  390. };
  391. };
  392. bool shapePointerCompareFunc(const btCollisionObject* colA, const btCollisionObject* colB)
  393. {
  394. MyConvertPointerSizeT a,b;
  395. a.m_ptr = colA->getCollisionShape();
  396. b.m_ptr = colB->getCollisionShape();
  397. return (a.m_int<b.m_int);
  398. }
  399. void OpenGLGuiHelper::autogenerateGraphicsObjects(btDiscreteDynamicsWorld* rbWorld)
  400. {
  401. //sort the collision objects based on collision shape, the gfx library requires instances that re-use a shape to be added after eachother
  402. btAlignedObjectArray<btCollisionObject*> sortedObjects;
  403. sortedObjects.reserve(rbWorld->getNumCollisionObjects());
  404. for (int i=0;i<rbWorld->getNumCollisionObjects();i++)
  405. {
  406. btCollisionObject* colObj = rbWorld->getCollisionObjectArray()[i];
  407. sortedObjects.push_back(colObj);
  408. }
  409. sortedObjects.quickSort(shapePointerCompareFunc);
  410. for (int i=0;i<sortedObjects.size();i++)
  411. {
  412. btCollisionObject* colObj = sortedObjects[i];
  413. //btRigidBody* body = btRigidBody::upcast(colObj);
  414. //does this also work for btMultiBody/btMultiBodyLinkCollider?
  415. createCollisionShapeGraphicsObject(colObj->getCollisionShape());
  416. int colorIndex = colObj->getBroadphaseHandle()->getUid() & 3;
  417. btVector3 color= sColors[colorIndex];
  418. createCollisionObjectGraphicsObject(colObj,color);
  419. }
  420. }
  421. void OpenGLGuiHelper::drawText3D( const char* txt, float posX, float posY, float posZ, float size)
  422. {
  423. B3_PROFILE("OpenGLGuiHelper::drawText3D");
  424. btAssert(m_data->m_glApp);
  425. m_data->m_glApp->drawText3D(txt,posX,posY,posZ,size);
  426. }
  427. struct CommonGraphicsApp* OpenGLGuiHelper::getAppInterface()
  428. {
  429. return m_data->m_glApp;
  430. }