GL_ShapeDrawer.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifdef _WIN32 //needed for glut.h
  14. #include <windows.h>
  15. #endif
  16. #include "../OpenGLWindow/OpenGL2Include.h"
  17. #include "GL_ShapeDrawer.h"
  18. #include "BulletCollision/CollisionShapes/btPolyhedralConvexShape.h"
  19. #include "BulletCollision/CollisionShapes/btTriangleMeshShape.h"
  20. #include "BulletCollision/CollisionShapes/btBoxShape.h"
  21. #include "BulletCollision/CollisionShapes/btSphereShape.h"
  22. #include "BulletCollision/CollisionShapes/btConeShape.h"
  23. #include "BulletCollision/CollisionShapes/btCylinderShape.h"
  24. #include "BulletCollision/CollisionShapes/btTetrahedronShape.h"
  25. #include "BulletCollision/CollisionShapes/btCompoundShape.h"
  26. #include "BulletCollision/CollisionShapes/btCapsuleShape.h"
  27. #include "BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h"
  28. #include "BulletCollision/CollisionShapes/btUniformScalingShape.h"
  29. #include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
  30. #include "BulletCollision/CollisionShapes/btMultiSphereShape.h"
  31. #include "BulletCollision/CollisionShapes/btConvexPolyhedron.h"
  32. #include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h"
  33. #include "BulletDynamics/Dynamics/btRigidBody.h"
  34. #include "LinearMath/btDefaultMotionState.h"
  35. ///
  36. #include "BulletCollision/CollisionShapes/btShapeHull.h"
  37. #include "LinearMath/btTransformUtil.h"
  38. #include "LinearMath/btIDebugDraw.h"
  39. //for debugmodes
  40. #include <stdio.h> //printf debugging
  41. #include <cmath>
  42. #if defined(BT_USE_DOUBLE_PRECISION)
  43. #define btglLoadMatrix glLoadMatrixd
  44. #define btglMultMatrix glMultMatrixd
  45. #define btglColor3 glColor3d
  46. #define btglVertex3 glVertex3d
  47. #else
  48. #define btglLoadMatrix glLoadMatrixf
  49. #define btglMultMatrix glMultMatrixf
  50. #define btglColor3 glColor3f
  51. #define btglVertex3 glVertex3d
  52. #endif
  53. void GL_ShapeDrawer::drawCoordSystem()
  54. {
  55. glBegin(GL_LINES);
  56. glColor3f(1, 0, 0);
  57. glVertex3d(0, 0, 0);
  58. glVertex3d(1, 0, 0);
  59. glColor3f(0, 1, 0);
  60. glVertex3d(0, 0, 0);
  61. glVertex3d(0, 1, 0);
  62. glColor3f(0, 0, 1);
  63. glVertex3d(0, 0, 0);
  64. glVertex3d(0, 0, 1);
  65. glEnd();
  66. }
  67. class GlDrawcallback : public btTriangleCallback
  68. {
  69. public:
  70. bool m_wireframe;
  71. GlDrawcallback()
  72. : m_wireframe(false)
  73. {
  74. }
  75. virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex)
  76. {
  77. (void)triangleIndex;
  78. (void)partId;
  79. if (m_wireframe)
  80. {
  81. glBegin(GL_LINES);
  82. glColor3f(1, 0, 0);
  83. glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
  84. glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
  85. glColor3f(0, 1, 0);
  86. glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
  87. glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
  88. glColor3f(0, 0, 1);
  89. glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
  90. glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
  91. glEnd();
  92. }
  93. else
  94. {
  95. glBegin(GL_TRIANGLES);
  96. //glColor3f(1, 1, 1);
  97. glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
  98. glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
  99. glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
  100. glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
  101. glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
  102. glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
  103. glEnd();
  104. }
  105. }
  106. };
  107. class TriangleGlDrawcallback : public btInternalTriangleIndexCallback
  108. {
  109. public:
  110. virtual void internalProcessTriangleIndex(btVector3* triangle, int partId, int triangleIndex)
  111. {
  112. (void)triangleIndex;
  113. (void)partId;
  114. glBegin(GL_TRIANGLES); //LINES);
  115. glColor3f(1, 0, 0);
  116. glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
  117. glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
  118. glColor3f(0, 1, 0);
  119. glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
  120. glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
  121. glColor3f(0, 0, 1);
  122. glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
  123. glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
  124. glEnd();
  125. }
  126. };
  127. void GL_ShapeDrawer::drawSphere(btScalar radius, int lats, int longs)
  128. {
  129. int i, j;
  130. for (i = 0; i <= lats; i++)
  131. {
  132. btScalar lat0 = SIMD_PI * (-btScalar(0.5) + (btScalar)(i - 1) / lats);
  133. btScalar z0 = radius * std::sin(lat0);
  134. btScalar zr0 = radius * std::cos(lat0);
  135. btScalar lat1 = SIMD_PI * (-btScalar(0.5) + (btScalar)i / lats);
  136. btScalar z1 = radius * std::sin(lat1);
  137. btScalar zr1 = radius * std::cos(lat1);
  138. glBegin(GL_QUAD_STRIP);
  139. for (j = 0; j <= longs; j++)
  140. {
  141. btScalar lng = 2 * SIMD_PI * (btScalar)(j - 1) / longs;
  142. btScalar x = std::cos(lng);
  143. btScalar y = std::sin(lng);
  144. glNormal3f(x * zr1, y * zr1, z1);
  145. glVertex3f(x * zr1, y * zr1, z1);
  146. glNormal3f(x * zr0, y * zr0, z0);
  147. glVertex3f(x * zr0, y * zr0, z0);
  148. }
  149. glEnd();
  150. }
  151. }
  152. GL_ShapeDrawer::ShapeCache* GL_ShapeDrawer::cache(btConvexShape* shape)
  153. {
  154. ShapeCache* sc = (ShapeCache*)shape->getUserPointer();
  155. if (!sc)
  156. {
  157. sc = new (btAlignedAlloc(sizeof(ShapeCache), 16)) ShapeCache(shape);
  158. sc->m_shapehull.buildHull(shape->getMargin());
  159. m_shapecaches.push_back(sc);
  160. shape->setUserPointer(sc);
  161. /* Build edges */
  162. const int ni = sc->m_shapehull.numIndices();
  163. const int nv = sc->m_shapehull.numVertices();
  164. const unsigned int* pi = sc->m_shapehull.getIndexPointer();
  165. const btVector3* pv = sc->m_shapehull.getVertexPointer();
  166. btAlignedObjectArray<ShapeCache::Edge*> edges;
  167. sc->m_edges.reserve(ni);
  168. edges.resize(nv * nv, 0);
  169. for (int i = 0; i < ni; i += 3)
  170. {
  171. const unsigned int* ti = pi + i;
  172. const btVector3 nrm = btCross(pv[ti[1]] - pv[ti[0]], pv[ti[2]] - pv[ti[0]]).normalized();
  173. for (int j = 2, k = 0; k < 3; j = k++)
  174. {
  175. const unsigned int a = ti[j];
  176. const unsigned int b = ti[k];
  177. ShapeCache::Edge*& e = edges[btMin(a, b) * nv + btMax(a, b)];
  178. if (!e)
  179. {
  180. sc->m_edges.push_back(ShapeCache::Edge());
  181. e = &sc->m_edges[sc->m_edges.size() - 1];
  182. e->n[0] = nrm;
  183. e->n[1] = -nrm;
  184. e->v[0] = a;
  185. e->v[1] = b;
  186. }
  187. else
  188. {
  189. e->n[1] = nrm;
  190. }
  191. }
  192. }
  193. }
  194. return (sc);
  195. }
  196. void renderSquareA(float x, float y, float z)
  197. {
  198. glBegin(GL_LINE_LOOP);
  199. glVertex3f(x, y, z);
  200. glVertex3f(x + 10.f, y, z);
  201. glVertex3f(x + 10.f, y + 10.f, z);
  202. glVertex3f(x, y + 10.f, z);
  203. glEnd();
  204. }
  205. inline void glDrawVector(const btVector3& v) { glVertex3d(v[0], v[1], v[2]); }
  206. void GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, const btVector3& color, int debugMode, const btVector3& worldBoundsMin, const btVector3& worldBoundsMax)
  207. {
  208. if (shape->getShapeType() == CUSTOM_CONVEX_SHAPE_TYPE)
  209. {
  210. btVector3 org(m[12], m[13], m[14]);
  211. btVector3 dx(m[0], m[1], m[2]);
  212. btVector3 dy(m[4], m[5], m[6]);
  213. // btVector3 dz(m[8], m[9], m[10]);
  214. const btBoxShape* boxShape = static_cast<const btBoxShape*>(shape);
  215. btVector3 halfExtent = boxShape->getHalfExtentsWithMargin();
  216. dx *= halfExtent[0];
  217. dy *= halfExtent[1];
  218. // dz *= halfExtent[2];
  219. glColor3f(1, 1, 1);
  220. glDisable(GL_LIGHTING);
  221. glLineWidth(2);
  222. glBegin(GL_LINE_LOOP);
  223. glDrawVector(org - dx - dy);
  224. glDrawVector(org - dx + dy);
  225. glDrawVector(org + dx + dy);
  226. glDrawVector(org + dx - dy);
  227. glEnd();
  228. return;
  229. }
  230. else if ((shape->getShapeType() == BOX_SHAPE_PROXYTYPE) && (debugMode & btIDebugDraw::DBG_FastWireframe))
  231. {
  232. btVector3 org(m[12], m[13], m[14]);
  233. btVector3 dx(m[0], m[1], m[2]);
  234. btVector3 dy(m[4], m[5], m[6]);
  235. btVector3 dz(m[8], m[9], m[10]);
  236. const btBoxShape* boxShape = static_cast<const btBoxShape*>(shape);
  237. btVector3 halfExtent = boxShape->getHalfExtentsWithMargin();
  238. dx *= halfExtent[0];
  239. dy *= halfExtent[1];
  240. dz *= halfExtent[2];
  241. glBegin(GL_LINE_LOOP);
  242. glDrawVector(org - dx - dy - dz);
  243. glDrawVector(org + dx - dy - dz);
  244. glDrawVector(org + dx + dy - dz);
  245. glDrawVector(org - dx + dy - dz);
  246. glDrawVector(org - dx + dy + dz);
  247. glDrawVector(org + dx + dy + dz);
  248. glDrawVector(org + dx - dy + dz);
  249. glDrawVector(org - dx - dy + dz);
  250. glEnd();
  251. glBegin(GL_LINES);
  252. glDrawVector(org + dx - dy - dz);
  253. glDrawVector(org + dx - dy + dz);
  254. glDrawVector(org + dx + dy - dz);
  255. glDrawVector(org + dx + dy + dz);
  256. glDrawVector(org - dx - dy - dz);
  257. glDrawVector(org - dx + dy - dz);
  258. glDrawVector(org - dx - dy + dz);
  259. glDrawVector(org - dx + dy + dz);
  260. glEnd();
  261. return;
  262. }
  263. glPushMatrix();
  264. btglMultMatrix(m);
  265. if (shape->getShapeType() == UNIFORM_SCALING_SHAPE_PROXYTYPE)
  266. {
  267. const btUniformScalingShape* scalingShape = static_cast<const btUniformScalingShape*>(shape);
  268. const btConvexShape* convexShape = scalingShape->getChildShape();
  269. float scalingFactor = (float)scalingShape->getUniformScalingFactor();
  270. {
  271. btScalar tmpScaling[4][4] = {{scalingFactor, 0, 0, 0},
  272. {0, scalingFactor, 0, 0},
  273. {0, 0, scalingFactor, 0},
  274. {0, 0, 0, 1}};
  275. drawOpenGL((btScalar*)tmpScaling, convexShape, color, debugMode, worldBoundsMin, worldBoundsMax);
  276. }
  277. glPopMatrix();
  278. return;
  279. }
  280. if (shape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE)
  281. {
  282. const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(shape);
  283. for (int i = compoundShape->getNumChildShapes() - 1; i >= 0; i--)
  284. {
  285. btTransform childTrans = compoundShape->getChildTransform(i);
  286. const btCollisionShape* colShape = compoundShape->getChildShape(i);
  287. ATTRIBUTE_ALIGNED16(btScalar)
  288. childMat[16];
  289. childTrans.getOpenGLMatrix(childMat);
  290. drawOpenGL(childMat, colShape, color, debugMode, worldBoundsMin, worldBoundsMax);
  291. }
  292. }
  293. else
  294. {
  295. if (m_textureenabled && (!m_textureinitialized))
  296. {
  297. GLubyte* image = new GLubyte[256 * 256 * 4];
  298. for (int y = 0; y < 256; ++y)
  299. {
  300. const int t = y >> 4;
  301. GLubyte* pi = image + y * 256 * 3;
  302. for (int x = 0; x < 256; ++x)
  303. {
  304. const int s = x >> 4;
  305. const GLubyte b = 180;
  306. GLubyte c = b + ((s + (t & 1)) & 1) * (255 - b);
  307. pi[0] = pi[1] = pi[2] = pi[3] = c;
  308. pi += 3;
  309. }
  310. }
  311. glGenTextures(1, (GLuint*)&m_texturehandle);
  312. glBindTexture(GL_TEXTURE_2D, m_texturehandle);
  313. glGenTextures(1, (GLuint*)&m_texturehandle);
  314. glBindTexture(GL_TEXTURE_2D, m_texturehandle);
  315. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  316. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  317. glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  318. //glGenerateMipmap(GL_TEXTURE_2D);
  319. delete[] image;
  320. }
  321. glMatrixMode(GL_TEXTURE);
  322. glLoadIdentity();
  323. glScalef(0.025f, 0.025f, 0.025f);
  324. glMatrixMode(GL_MODELVIEW);
  325. static const GLfloat planex[] = {1, 0, 0, 0};
  326. // static const GLfloat planey[]={0,1,0,0};
  327. static const GLfloat planez[] = {0, 0, 1, 0};
  328. glTexGenfv(GL_S, GL_OBJECT_PLANE, planex);
  329. glTexGenfv(GL_T, GL_OBJECT_PLANE, planez);
  330. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  331. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  332. glEnable(GL_TEXTURE_GEN_S);
  333. glEnable(GL_TEXTURE_GEN_T);
  334. glEnable(GL_TEXTURE_GEN_R);
  335. m_textureinitialized = true;
  336. //drawCoordSystem();
  337. //glPushMatrix();
  338. glEnable(GL_COLOR_MATERIAL);
  339. if (m_textureenabled)
  340. {
  341. glEnable(GL_TEXTURE_2D);
  342. glBindTexture(GL_TEXTURE_2D, m_texturehandle);
  343. }
  344. else
  345. {
  346. glDisable(GL_TEXTURE_2D);
  347. }
  348. glColor3f(color.x(), color.y(), color.z());
  349. //bool useWireframeFallback = true;
  350. if (!(debugMode & btIDebugDraw::DBG_DrawWireframe))
  351. {
  352. ///you can comment out any of the specific cases, and use the default
  353. ///the benefit of 'default' is that it approximates the actual collision shape including collision margin
  354. //int shapetype=m_textureenabled?MAX_BROADPHASE_COLLISION_TYPES:shape->getShapeType();
  355. int shapetype = shape->getShapeType();
  356. switch (shapetype)
  357. {
  358. case SPHERE_SHAPE_PROXYTYPE:
  359. {
  360. const btSphereShape* sphereShape = static_cast<const btSphereShape*>(shape);
  361. float radius = sphereShape->getMargin(); //radius doesn't include the margin, so draw with margin
  362. drawSphere(radius, 10, 10);
  363. //useWireframeFallback = false;
  364. break;
  365. }
  366. case BOX_SHAPE_PROXYTYPE:
  367. {
  368. const btBoxShape* boxShape = static_cast<const btBoxShape*>(shape);
  369. btVector3 halfExtent = boxShape->getHalfExtentsWithMargin();
  370. static int indices[36] = {
  371. 0, 1, 2,
  372. 3, 2, 1,
  373. 4, 0, 6,
  374. 6, 0, 2,
  375. 5, 1, 4,
  376. 4, 1, 0,
  377. 7, 3, 1,
  378. 7, 1, 5,
  379. 5, 4, 7,
  380. 7, 4, 6,
  381. 7, 2, 3,
  382. 7, 6, 2};
  383. btVector3 vertices[8] = {
  384. btVector3(halfExtent[0], halfExtent[1], halfExtent[2]),
  385. btVector3(-halfExtent[0], halfExtent[1], halfExtent[2]),
  386. btVector3(halfExtent[0], -halfExtent[1], halfExtent[2]),
  387. btVector3(-halfExtent[0], -halfExtent[1], halfExtent[2]),
  388. btVector3(halfExtent[0], halfExtent[1], -halfExtent[2]),
  389. btVector3(-halfExtent[0], halfExtent[1], -halfExtent[2]),
  390. btVector3(halfExtent[0], -halfExtent[1], -halfExtent[2]),
  391. btVector3(-halfExtent[0], -halfExtent[1], -halfExtent[2])};
  392. #if 1
  393. glBegin(GL_TRIANGLES);
  394. int si = 36;
  395. for (int i = 0; i < si; i += 3)
  396. {
  397. const btVector3& v1 = vertices[indices[i]];
  398. ;
  399. const btVector3& v2 = vertices[indices[i + 1]];
  400. const btVector3& v3 = vertices[indices[i + 2]];
  401. btVector3 normal = (v3 - v1).cross(v2 - v1);
  402. normal.normalize();
  403. glNormal3f(normal.getX(), normal.getY(), normal.getZ());
  404. glVertex3f(v1.x(), v1.y(), v1.z());
  405. glVertex3f(v2.x(), v2.y(), v2.z());
  406. glVertex3f(v3.x(), v3.y(), v3.z());
  407. }
  408. glEnd();
  409. #endif
  410. //useWireframeFallback = false;
  411. break;
  412. }
  413. #if 0
  414. case CONE_SHAPE_PROXYTYPE:
  415. {
  416. const btConeShape* coneShape = static_cast<const btConeShape*>(shape);
  417. int upIndex = coneShape->getConeUpIndex();
  418. float radius = coneShape->getRadius();//+coneShape->getMargin();
  419. float height = coneShape->getHeight();//+coneShape->getMargin();
  420. switch (upIndex)
  421. {
  422. case 0:
  423. glRotatef(90.0, 0.0, 1.0, 0.0);
  424. break;
  425. case 1:
  426. glRotatef(-90.0, 1.0, 0.0, 0.0);
  427. break;
  428. case 2:
  429. break;
  430. default:
  431. {
  432. }
  433. };
  434. glTranslatef(0.0, 0.0, -0.5*height);
  435. glutSolidCone(radius,height,10,10);
  436. //useWireframeFallback = false;
  437. break;
  438. }
  439. #endif
  440. case STATIC_PLANE_PROXYTYPE:
  441. {
  442. const btStaticPlaneShape* staticPlaneShape = static_cast<const btStaticPlaneShape*>(shape);
  443. btScalar planeConst = staticPlaneShape->getPlaneConstant();
  444. const btVector3& planeNormal = staticPlaneShape->getPlaneNormal();
  445. btVector3 planeOrigin = planeNormal * planeConst;
  446. btVector3 vec0, vec1;
  447. btPlaneSpace1(planeNormal, vec0, vec1);
  448. btScalar vecLen = 100.f;
  449. btVector3 pt0 = planeOrigin + vec0 * vecLen;
  450. btVector3 pt1 = planeOrigin - vec0 * vecLen;
  451. btVector3 pt2 = planeOrigin + vec1 * vecLen;
  452. btVector3 pt3 = planeOrigin - vec1 * vecLen;
  453. glBegin(GL_LINES);
  454. glVertex3f(pt0.getX(), pt0.getY(), pt0.getZ());
  455. glVertex3f(pt1.getX(), pt1.getY(), pt1.getZ());
  456. glVertex3f(pt2.getX(), pt2.getY(), pt2.getZ());
  457. glVertex3f(pt3.getX(), pt3.getY(), pt3.getZ());
  458. glEnd();
  459. break;
  460. }
  461. case MULTI_SPHERE_SHAPE_PROXYTYPE:
  462. {
  463. const btMultiSphereShape* multiSphereShape = static_cast<const btMultiSphereShape*>(shape);
  464. btTransform childTransform;
  465. childTransform.setIdentity();
  466. for (int i = multiSphereShape->getSphereCount() - 1; i >= 0; i--)
  467. {
  468. btSphereShape sc(multiSphereShape->getSphereRadius(i));
  469. childTransform.setOrigin(multiSphereShape->getSpherePosition(i));
  470. ATTRIBUTE_ALIGNED16(btScalar)
  471. childMat[16];
  472. childTransform.getOpenGLMatrix(childMat);
  473. drawOpenGL(childMat, &sc, color, debugMode, worldBoundsMin, worldBoundsMax);
  474. }
  475. break;
  476. }
  477. default:
  478. {
  479. if (shape->isConvex())
  480. {
  481. const btConvexPolyhedron* poly = shape->isPolyhedral() ? ((btPolyhedralConvexShape*)shape)->getConvexPolyhedron() : 0;
  482. if (poly)
  483. {
  484. int i;
  485. glBegin(GL_TRIANGLES);
  486. for (i = 0; i < poly->m_faces.size(); i++)
  487. {
  488. btVector3 centroid(0, 0, 0);
  489. int numVerts = poly->m_faces[i].m_indices.size();
  490. if (numVerts > 2)
  491. {
  492. btVector3 v1 = poly->m_vertices[poly->m_faces[i].m_indices[0]];
  493. for (int v = 0; v < poly->m_faces[i].m_indices.size() - 2; v++)
  494. {
  495. btVector3 v2 = poly->m_vertices[poly->m_faces[i].m_indices[v + 1]];
  496. btVector3 v3 = poly->m_vertices[poly->m_faces[i].m_indices[v + 2]];
  497. btVector3 normal = (v3 - v1).cross(v2 - v1);
  498. normal.normalize();
  499. glNormal3f(normal.getX(), normal.getY(), normal.getZ());
  500. glVertex3f(v1.x(), v1.y(), v1.z());
  501. glVertex3f(v2.x(), v2.y(), v2.z());
  502. glVertex3f(v3.x(), v3.y(), v3.z());
  503. }
  504. }
  505. }
  506. glEnd();
  507. }
  508. else
  509. {
  510. ShapeCache* sc = cache((btConvexShape*)shape);
  511. //glutSolidCube(1.0);
  512. btShapeHull* hull = &sc->m_shapehull /*(btShapeHull*)shape->getUserPointer()*/;
  513. if (hull->numTriangles() > 0)
  514. {
  515. int index = 0;
  516. const unsigned int* idx = hull->getIndexPointer();
  517. const btVector3* vtx = hull->getVertexPointer();
  518. glBegin(GL_TRIANGLES);
  519. for (int i = 0; i < hull->numTriangles(); i++)
  520. {
  521. int i1 = index++;
  522. int i2 = index++;
  523. int i3 = index++;
  524. btAssert(i1 < hull->numIndices() &&
  525. i2 < hull->numIndices() &&
  526. i3 < hull->numIndices());
  527. int index1 = idx[i1];
  528. int index2 = idx[i2];
  529. int index3 = idx[i3];
  530. btAssert(index1 < hull->numVertices() &&
  531. index2 < hull->numVertices() &&
  532. index3 < hull->numVertices());
  533. btVector3 v1 = vtx[index1];
  534. btVector3 v2 = vtx[index2];
  535. btVector3 v3 = vtx[index3];
  536. btVector3 normal = (v3 - v1).cross(v2 - v1);
  537. normal.normalize();
  538. glNormal3f(normal.getX(), normal.getY(), normal.getZ());
  539. glVertex3f(v1.x(), v1.y(), v1.z());
  540. glVertex3f(v2.x(), v2.y(), v2.z());
  541. glVertex3f(v3.x(), v3.y(), v3.z());
  542. }
  543. glEnd();
  544. }
  545. }
  546. }
  547. }
  548. }
  549. }
  550. glNormal3f(0, 1, 0);
  551. /// for polyhedral shapes
  552. if (debugMode == btIDebugDraw::DBG_DrawFeaturesText && (shape->isPolyhedral()))
  553. {
  554. btPolyhedralConvexShape* polyshape = (btPolyhedralConvexShape*)shape;
  555. {
  556. glColor3f(1.f, 1.f, 1.f);
  557. int i;
  558. for (i = 0; i < polyshape->getNumVertices(); i++)
  559. {
  560. btVector3 vtx;
  561. polyshape->getVertex(i, vtx);
  562. char buf[12];
  563. sprintf(buf, " %d", i);
  564. //btDrawString(BMF_GetFont(BMF_kHelvetica10),buf);
  565. }
  566. for (i = 0; i < polyshape->getNumPlanes(); i++)
  567. {
  568. btVector3 normal;
  569. btVector3 vtx;
  570. polyshape->getPlane(normal, vtx, i);
  571. //btScalar d = vtx.dot(normal);
  572. //char buf[12];
  573. //sprintf(buf," plane %d",i);
  574. //btDrawString(BMF_GetFont(BMF_kHelvetica10),buf);
  575. }
  576. }
  577. }
  578. }
  579. glPopMatrix();
  580. glDisable(GL_TEXTURE_2D);
  581. }
  582. //
  583. void GL_ShapeDrawer::drawShadow(btScalar* m, const btVector3& extrusion, const btCollisionShape* shape, const btVector3& worldBoundsMin, const btVector3& worldBoundsMax)
  584. {
  585. glPushMatrix();
  586. btglMultMatrix(m);
  587. if (shape->getShapeType() == UNIFORM_SCALING_SHAPE_PROXYTYPE)
  588. {
  589. const btUniformScalingShape* scalingShape = static_cast<const btUniformScalingShape*>(shape);
  590. const btConvexShape* convexShape = scalingShape->getChildShape();
  591. float scalingFactor = (float)scalingShape->getUniformScalingFactor();
  592. btScalar tmpScaling[4][4] = {{scalingFactor, 0, 0, 0},
  593. {0, scalingFactor, 0, 0},
  594. {0, 0, scalingFactor, 0},
  595. {0, 0, 0, 1}};
  596. drawShadow((btScalar*)tmpScaling, extrusion, convexShape, worldBoundsMin, worldBoundsMax);
  597. glPopMatrix();
  598. return;
  599. }
  600. else if (shape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE)
  601. {
  602. const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(shape);
  603. for (int i = compoundShape->getNumChildShapes() - 1; i >= 0; i--)
  604. {
  605. btTransform childTrans = compoundShape->getChildTransform(i);
  606. const btCollisionShape* colShape = compoundShape->getChildShape(i);
  607. ATTRIBUTE_ALIGNED16(btScalar)
  608. childMat[16];
  609. childTrans.getOpenGLMatrix(childMat);
  610. drawShadow(childMat, extrusion * childTrans.getBasis(), colShape, worldBoundsMin, worldBoundsMax);
  611. }
  612. }
  613. else
  614. {
  615. // bool useWireframeFallback = true;
  616. if (shape->isConvex())
  617. {
  618. ShapeCache* sc = cache((btConvexShape*)shape);
  619. btShapeHull* hull = &sc->m_shapehull;
  620. glBegin(GL_QUADS);
  621. for (int i = 0; i < sc->m_edges.size(); ++i)
  622. {
  623. const btScalar d = btDot(sc->m_edges[i].n[0], extrusion);
  624. if ((d * btDot(sc->m_edges[i].n[1], extrusion)) < 0)
  625. {
  626. const int q = d < 0 ? 1 : 0;
  627. const btVector3& a = hull->getVertexPointer()[sc->m_edges[i].v[q]];
  628. const btVector3& b = hull->getVertexPointer()[sc->m_edges[i].v[1 - q]];
  629. glVertex3f(a[0], a[1], a[2]);
  630. glVertex3f(b[0], b[1], b[2]);
  631. glVertex3f(b[0] + extrusion[0], b[1] + extrusion[1], b[2] + extrusion[2]);
  632. glVertex3f(a[0] + extrusion[0], a[1] + extrusion[1], a[2] + extrusion[2]);
  633. }
  634. }
  635. glEnd();
  636. }
  637. }
  638. if (shape->isConcave()) //>getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE||shape->getShapeType() == GIMPACT_SHAPE_PROXYTYPE)
  639. // if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
  640. {
  641. btConcaveShape* concaveMesh = (btConcaveShape*)shape;
  642. GlDrawcallback drawCallback;
  643. drawCallback.m_wireframe = false;
  644. concaveMesh->processAllTriangles(&drawCallback, worldBoundsMin, worldBoundsMax);
  645. }
  646. glPopMatrix();
  647. }
  648. //
  649. GL_ShapeDrawer::GL_ShapeDrawer()
  650. {
  651. m_texturehandle = 0;
  652. m_textureenabled = false;
  653. m_textureinitialized = false;
  654. }
  655. GL_ShapeDrawer::~GL_ShapeDrawer()
  656. {
  657. int i;
  658. for (i = 0; i < m_shapecaches.size(); i++)
  659. {
  660. m_shapecaches[i]->~ShapeCache();
  661. btAlignedFree(m_shapecaches[i]);
  662. }
  663. m_shapecaches.clear();
  664. if (m_textureinitialized)
  665. {
  666. glDeleteTextures(1, (const GLuint*)&m_texturehandle);
  667. }
  668. }
  669. void GL_ShapeDrawer::drawSceneInternal(const btDiscreteDynamicsWorld* dynamicsWorld, int pass, int cameraUpAxis)
  670. {
  671. btAssert(dynamicsWorld);
  672. btScalar m[16];
  673. btMatrix3x3 rot;
  674. rot.setIdentity();
  675. const int numObjects = dynamicsWorld->getNumCollisionObjects();
  676. btVector3 wireColor(1, 0, 0);
  677. //glDisable(GL_CULL_FACE);
  678. for (int i = 0; i < numObjects; i++)
  679. {
  680. const btCollisionObject* colObj = dynamicsWorld->getCollisionObjectArray()[i];
  681. const btRigidBody* body = btRigidBody::upcast(colObj);
  682. if (body && body->getMotionState())
  683. {
  684. btDefaultMotionState* myMotionState = (btDefaultMotionState*)body->getMotionState();
  685. myMotionState->m_graphicsWorldTrans.getOpenGLMatrix(m);
  686. rot = myMotionState->m_graphicsWorldTrans.getBasis();
  687. }
  688. else
  689. {
  690. colObj->getWorldTransform().getOpenGLMatrix(m);
  691. rot = colObj->getWorldTransform().getBasis();
  692. }
  693. btVector3 wireColor(1.f, 1.0f, 0.5f); //wants deactivation
  694. if (i & 1) wireColor = btVector3(0.f, 0.0f, 1.f);
  695. ///color differently for active, sleeping, wantsdeactivation states
  696. if (colObj->getActivationState() == 1) //active
  697. {
  698. if (i & 1)
  699. {
  700. wireColor += btVector3(1.f, 0.f, 0.f);
  701. }
  702. else
  703. {
  704. wireColor += btVector3(.5f, 0.f, 0.f);
  705. }
  706. }
  707. if (colObj->getActivationState() == 2) //ISLAND_SLEEPING
  708. {
  709. if (i & 1)
  710. {
  711. wireColor += btVector3(0.f, 1.f, 0.f);
  712. }
  713. else
  714. {
  715. wireColor += btVector3(0.f, 0.5f, 0.f);
  716. }
  717. }
  718. btVector3 aabbMin(0, 0, 0), aabbMax(0, 0, 0);
  719. //m_dynamicsWorld->getBroadphase()->getBroadphaseAabb(aabbMin,aabbMax);
  720. aabbMin -= btVector3(BT_LARGE_FLOAT, BT_LARGE_FLOAT, BT_LARGE_FLOAT);
  721. aabbMax += btVector3(BT_LARGE_FLOAT, BT_LARGE_FLOAT, BT_LARGE_FLOAT);
  722. // printf("aabbMin=(%f,%f,%f)\n",aabbMin.getX(),aabbMin.getY(),aabbMin.getZ());
  723. // printf("aabbMax=(%f,%f,%f)\n",aabbMax.getX(),aabbMax.getY(),aabbMax.getZ());
  724. // m_dynamicsWorld->getDebugDrawer()->drawAabb(aabbMin,aabbMax,btVector3(1,1,1));
  725. //switch(pass)
  726. //if (!(getDebugMode()& btIDebugDraw::DBG_DrawWireframe))
  727. int debugMode = 0; //getDebugMode()
  728. //btVector3 m_sundirection(-1,-1,-1);
  729. btVector3 m_sundirection(btVector3(1, -2, 1) * 1000);
  730. if (cameraUpAxis == 2)
  731. {
  732. m_sundirection = btVector3(1, 1, -2) * 1000;
  733. }
  734. switch (pass)
  735. {
  736. case 0:
  737. drawOpenGL(m, colObj->getCollisionShape(), wireColor, debugMode, aabbMin, aabbMax);
  738. break;
  739. case 1:
  740. drawShadow(m, m_sundirection * rot, colObj->getCollisionShape(), aabbMin, aabbMax);
  741. break;
  742. case 2:
  743. drawOpenGL(m, colObj->getCollisionShape(), wireColor * btScalar(0.3), 0, aabbMin, aabbMax);
  744. break;
  745. }
  746. }
  747. }
  748. //this GL_ShapeDrawer will be removed, in the meanwhile directly access this global 'useShadoMaps'
  749. extern bool useShadowMap;
  750. void GL_ShapeDrawer::drawScene(const btDiscreteDynamicsWorld* dynamicsWorld, bool useShadows1, int cameraUpAxis)
  751. {
  752. bool useShadows = useShadowMap;
  753. GLfloat light_ambient[] = {btScalar(0.2), btScalar(0.2), btScalar(0.2), btScalar(1.0)};
  754. GLfloat light_diffuse[] = {btScalar(1.0), btScalar(1.0), btScalar(1.0), btScalar(1.0)};
  755. GLfloat light_specular[] = {btScalar(1.0), btScalar(1.0), btScalar(1.0), btScalar(1.0)};
  756. /* light_position is NOT default value */
  757. GLfloat light_position0[] = {btScalar(1.0), btScalar(10.0), btScalar(1.0), btScalar(0.0)};
  758. GLfloat light_position1[] = {btScalar(-1.0), btScalar(-10.0), btScalar(-1.0), btScalar(0.0)};
  759. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  760. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  761. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  762. glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
  763. glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
  764. glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
  765. glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
  766. glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
  767. glEnable(GL_LIGHTING);
  768. glEnable(GL_LIGHT0);
  769. glEnable(GL_LIGHT1);
  770. glShadeModel(GL_SMOOTH);
  771. glEnable(GL_DEPTH_TEST);
  772. glDepthFunc(GL_LESS);
  773. glClearColor(btScalar(0.7), btScalar(0.7), btScalar(0.7), btScalar(0));
  774. if (useShadows)
  775. {
  776. glClear(GL_STENCIL_BUFFER_BIT);
  777. glEnable(GL_CULL_FACE);
  778. drawSceneInternal(dynamicsWorld, 0, cameraUpAxis);
  779. glDisable(GL_LIGHTING);
  780. glDepthMask(GL_FALSE);
  781. glDepthFunc(GL_LEQUAL);
  782. glEnable(GL_STENCIL_TEST);
  783. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  784. glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFFL);
  785. glFrontFace(GL_CCW);
  786. glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
  787. drawSceneInternal(dynamicsWorld, 1, cameraUpAxis);
  788. glFrontFace(GL_CW);
  789. glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
  790. drawSceneInternal(dynamicsWorld, 1, cameraUpAxis);
  791. glFrontFace(GL_CCW);
  792. glPolygonMode(GL_FRONT, GL_FILL);
  793. glPolygonMode(GL_BACK, GL_FILL);
  794. glShadeModel(GL_SMOOTH);
  795. glEnable(GL_DEPTH_TEST);
  796. glDepthFunc(GL_LESS);
  797. glEnable(GL_LIGHTING);
  798. glDepthMask(GL_TRUE);
  799. glCullFace(GL_BACK);
  800. glFrontFace(GL_CCW);
  801. glEnable(GL_CULL_FACE);
  802. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  803. glDepthFunc(GL_LEQUAL);
  804. glStencilFunc(GL_NOTEQUAL, 0, 0xFFFFFFFFL);
  805. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  806. glDisable(GL_LIGHTING);
  807. drawSceneInternal(dynamicsWorld, 2, cameraUpAxis);
  808. glEnable(GL_LIGHTING);
  809. glDepthFunc(GL_LESS);
  810. glDisable(GL_STENCIL_TEST);
  811. glDisable(GL_CULL_FACE);
  812. }
  813. else
  814. {
  815. glDisable(GL_CULL_FACE);
  816. drawSceneInternal(dynamicsWorld, 0, cameraUpAxis);
  817. }
  818. }