GL_ShapeDrawer.cpp 27 KB

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