OpenGLGuiHelper.cpp 50 KB

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