source: proiecte/SIMEO/Simeo/src/SimeoGui/GL_ShapeDrawer.cpp @ 167

Last change on this file since 167 was 167, checked in by (none), 14 years ago

Simeo: added final project and also older proof of concept code.

We used Git for version control, so look at the Git repo
in SIMEO/Simeo/ for more info.

File size: 26.7 KB
Line 
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16#ifdef WIN32 //needed for glut.h
17#include <windows.h>
18#endif
19#include "GLDebugFont.h"
20
21
22
23#include "GlutStuff.h"
24#include "GL_ShapeDrawer.h"
25#include "BulletCollision/CollisionShapes/btPolyhedralConvexShape.h"
26#include "BulletCollision/CollisionShapes/btTriangleMeshShape.h"
27#include "BulletCollision/CollisionShapes/btBoxShape.h"
28#include "BulletCollision/CollisionShapes/btSphereShape.h"
29#include "BulletCollision/CollisionShapes/btConeShape.h"
30#include "BulletCollision/CollisionShapes/btCylinderShape.h"
31#include "BulletCollision/CollisionShapes/btTetrahedronShape.h"
32#include "BulletCollision/CollisionShapes/btCompoundShape.h"
33#include "BulletCollision/CollisionShapes/btCapsuleShape.h"
34#include "BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h"
35#include "BulletCollision/CollisionShapes/btUniformScalingShape.h"
36#include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
37#include "BulletCollision/CollisionShapes/btMultiSphereShape.h"
38
39
40///
41#include "BulletCollision/CollisionShapes/btShapeHull.h"
42
43#include "LinearMath/btTransformUtil.h"
44
45
46#include "LinearMath/btIDebugDraw.h"
47//for debugmodes
48
49#include <stdio.h> //printf debugging
50
51//#define USE_DISPLAY_LISTS 1
52#ifdef USE_DISPLAY_LISTS
53
54#include <map>
55
56using namespace std;
57
58//Set for storing Display list per trimesh
59struct TRIMESH_KEY
60{
61        btCollisionShape* m_shape;
62        GLuint m_dlist;//OpenGL display list
63};
64
65typedef map<unsigned long,TRIMESH_KEY> TRIMESH_KEY_MAP;
66
67typedef pair<unsigned long,TRIMESH_KEY> TRIMESH_KEY_PAIR;
68
69TRIMESH_KEY_MAP g_display_lists;
70
71class GlDisplaylistDrawcallback : public btTriangleCallback
72{
73public:
74
75        virtual void processTriangle(btVector3* triangle,int partId, int triangleIndex)
76        {
77
78                btVector3 diff1 = triangle[1] - triangle[0];
79                btVector3 diff2 = triangle[2] - triangle[0];
80                btVector3 normal = diff1.cross(diff2);
81
82                normal.normalize();
83
84                glBegin(GL_TRIANGLES);
85                glColor3f(1, 1, 1);
86                glNormal3d(normal.getX(),normal.getY(),normal.getZ());
87                glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
88
89                //glColor3f(0, 1, 0);
90                glNormal3d(normal.getX(),normal.getY(),normal.getZ());
91                glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
92
93                //glColor3f(0, 1, 0);
94                glNormal3d(normal.getX(),normal.getY(),normal.getZ());
95                glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
96                glEnd();
97
98                /*glBegin(GL_LINES);
99                glColor3f(1, 1, 0);
100                glNormal3d(normal.getX(),normal.getY(),normal.getZ());
101                glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
102                glNormal3d(normal.getX(),normal.getY(),normal.getZ());
103                glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
104                glColor3f(1, 1, 0);
105                glNormal3d(normal.getX(),normal.getY(),normal.getZ());
106                glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
107                glNormal3d(normal.getX(),normal.getY(),normal.getZ());
108                glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
109                glColor3f(1, 1, 0);
110                glNormal3d(normal.getX(),normal.getY(),normal.getZ());
111                glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
112                glNormal3d(normal.getX(),normal.getY(),normal.getZ());
113                glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
114                glEnd();*/
115
116
117        }
118};
119
120GLuint  OGL_get_displaylist_for_shape(btCollisionShape * shape)
121{
122        TRIMESH_KEY_MAP::iterator map_iter;
123
124        unsigned long key = (unsigned long)shape;
125        map_iter = g_display_lists.find(key);
126        if(map_iter!=g_display_lists.end())
127        {
128                return map_iter->second.m_dlist;
129        }
130
131        return 0;
132}
133
134void OGL_displaylist_clean()
135{
136        TRIMESH_KEY_MAP::iterator map_iter,map_itend;
137
138        map_iter = g_display_lists.begin();
139
140        while(map_iter!=map_itend)
141        {
142                glDeleteLists(map_iter->second.m_dlist,1);
143                map_iter++;
144        }
145
146        g_display_lists.clear();
147}
148
149
150void OGL_displaylist_register_shape(btCollisionShape * shape)
151{
152        btVector3 aabbMax(btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT));
153        btVector3 aabbMin(-btScalar(BT_LARGE_FLOAT),-btScalar(BT_LARGE_FLOAT),-btScalar(BT_LARGE_FLOAT));
154        GlDisplaylistDrawcallback drawCallback;
155        TRIMESH_KEY dlist;
156
157        dlist.m_dlist = glGenLists(1);
158        dlist.m_shape = shape;
159
160        unsigned long key = (unsigned long)shape;
161
162        g_display_lists.insert(TRIMESH_KEY_PAIR(key,dlist));
163
164        glNewList(dlist.m_dlist,GL_COMPILE);
165
166//      glEnable(GL_CULL_FACE);
167
168        glCullFace(GL_BACK);
169
170        if (shape->isConcave())
171        {
172                btConcaveShape* concaveMesh = (btConcaveShape*) shape;
173                //todo pass camera, for some culling
174                concaveMesh->processAllTriangles(&drawCallback,aabbMin,aabbMax);
175        }
176
177//      glDisable(GL_CULL_FACE);
178
179        glEndList();
180}
181#endif //USE_DISPLAY_LISTS
182
183void GL_ShapeDrawer::drawCoordSystem()  {
184        glBegin(GL_LINES);
185        glColor3f(1, 0, 0);
186        glVertex3d(0, 0, 0);
187        glVertex3d(1, 0, 0);
188        glColor3f(0, 1, 0);
189        glVertex3d(0, 0, 0);
190        glVertex3d(0, 1, 0);
191        glColor3f(0, 0, 1);
192        glVertex3d(0, 0, 0);
193        glVertex3d(0, 0, 1);
194        glEnd();
195
196}
197
198
199
200
201
202class GlDrawcallback : public btTriangleCallback
203{
204
205public:
206
207        bool    m_wireframe;
208
209        GlDrawcallback()
210                :m_wireframe(false)
211        {
212        }
213
214        virtual void processTriangle(btVector3* triangle,int partId, int triangleIndex)
215        {
216
217                (void)triangleIndex;
218                (void)partId;
219
220
221                if (m_wireframe)
222                {
223                        glBegin(GL_LINES);
224                        glColor3f(1, 0, 0);
225                        glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
226                        glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
227                        glColor3f(0, 1, 0);
228                        glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
229                        glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
230                        glColor3f(0, 0, 1);
231                        glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
232                        glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
233                        glEnd();
234                } else
235                {
236                        glBegin(GL_TRIANGLES);
237                        //glColor3f(1, 1, 1);
238
239
240                        glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
241                        glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
242                        glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
243
244                        glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
245                        glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
246                        glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
247                        glEnd();
248                }
249        }
250};
251
252class TriangleGlDrawcallback : public btInternalTriangleIndexCallback
253{
254public:
255        virtual void internalProcessTriangleIndex(btVector3* triangle,int partId,int  triangleIndex)
256        {
257                (void)triangleIndex;
258                (void)partId;
259
260
261                glBegin(GL_TRIANGLES);//LINES);
262                glColor3f(1, 0, 0);
263                glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
264                glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
265                glColor3f(0, 1, 0);
266                glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
267                glVertex3d(triangle[1].getX(), triangle[1].getY(), triangle[1].getZ());
268                glColor3f(0, 0, 1);
269                glVertex3d(triangle[2].getX(), triangle[2].getY(), triangle[2].getZ());
270                glVertex3d(triangle[0].getX(), triangle[0].getY(), triangle[0].getZ());
271                glEnd();
272        }
273};
274
275
276void GL_ShapeDrawer::drawSphere(btScalar radius, int lats, int longs)
277{
278        int i, j;
279        for(i = 0; i <= lats; i++) {
280                btScalar lat0 = SIMD_PI * (-btScalar(0.5) + (btScalar) (i - 1) / lats);
281                btScalar z0  = radius*sin(lat0);
282                btScalar zr0 =  radius*cos(lat0);
283
284                btScalar lat1 = SIMD_PI * (-btScalar(0.5) + (btScalar) i / lats);
285                btScalar z1 = radius*sin(lat1);
286                btScalar zr1 = radius*cos(lat1);
287
288                glBegin(GL_QUAD_STRIP);
289                for(j = 0; j <= longs; j++) {
290                        btScalar lng = 2 * SIMD_PI * (btScalar) (j - 1) / longs;
291                        btScalar x = cos(lng);
292                        btScalar y = sin(lng);
293                        glNormal3f(x * zr1, y * zr1, z1);
294                        glVertex3f(x * zr1, y * zr1, z1);
295                        glNormal3f(x * zr0, y * zr0, z0);
296                        glVertex3f(x * zr0, y * zr0, z0);
297                }
298                glEnd();
299        }
300}
301
302void GL_ShapeDrawer::drawCylinder(float radius,float halfHeight, int upAxis)
303{
304
305
306        glPushMatrix();
307        switch (upAxis)
308        {
309        case 0:
310                glRotatef(-90.0, 0.0, 1.0, 0.0);
311                glTranslatef(0.0, 0.0, -halfHeight);
312                break;
313        case 1:
314                glRotatef(-90.0, 1.0, 0.0, 0.0);
315                glTranslatef(0.0, 0.0, -halfHeight);
316                break;
317        case 2:
318
319                glTranslatef(0.0, 0.0, -halfHeight);
320                break;
321        default:
322                {
323                        btAssert(0);
324                }
325
326        }
327
328        GLUquadricObj *quadObj = gluNewQuadric();
329
330        //The gluCylinder subroutine draws a cylinder that is oriented along the z axis.
331        //The base of the cylinder is placed at z = 0; the top of the cylinder is placed at z=height.
332        //Like a sphere, the cylinder is subdivided around the z axis into slices and along the z axis into stacks.
333
334        gluQuadricDrawStyle(quadObj, (GLenum)GLU_FILL);
335        gluQuadricNormals(quadObj, (GLenum)GLU_SMOOTH);
336
337        gluDisk(quadObj,0,radius,15, 10);
338
339        gluCylinder(quadObj, radius, radius, 2.f*halfHeight, 15, 10);
340        glTranslatef(0.0, 0.0, 2.*halfHeight);
341        glRotatef(-180.0, 0.0, 1.0, 0.0);
342        gluDisk(quadObj,0,radius,15, 10);
343
344        glPopMatrix();
345        gluDeleteQuadric(quadObj);
346}
347
348GL_ShapeDrawer::ShapeCache*             GL_ShapeDrawer::cache(btConvexShape* shape)
349{
350        ShapeCache*             sc=(ShapeCache*)shape->getUserPointer();
351        if(!sc)
352        {
353                sc=new(btAlignedAlloc(sizeof(ShapeCache),16)) ShapeCache(shape);
354                sc->m_shapehull.buildHull(shape->getMargin());
355                m_shapecaches.push_back(sc);
356                shape->setUserPointer(sc);
357                /* Build edges  */
358                const int                       ni=sc->m_shapehull.numIndices();
359                const int                       nv=sc->m_shapehull.numVertices();
360                const unsigned int*     pi=sc->m_shapehull.getIndexPointer();
361                const btVector3*        pv=sc->m_shapehull.getVertexPointer();
362                btAlignedObjectArray<ShapeCache::Edge*> edges;
363                sc->m_edges.reserve(ni);
364                edges.resize(nv*nv,0);
365                for(int i=0;i<ni;i+=3)
366                {
367                        const unsigned int* ti=pi+i;
368                        const btVector3         nrm=btCross(pv[ti[1]]-pv[ti[0]],pv[ti[2]]-pv[ti[0]]).normalized();
369                        for(int j=2,k=0;k<3;j=k++)
370                        {
371                                const unsigned int      a=ti[j];
372                                const unsigned int      b=ti[k];
373                                ShapeCache::Edge*&      e=edges[btMin(a,b)*nv+btMax(a,b)];
374                                if(!e)
375                                {
376                                        sc->m_edges.push_back(ShapeCache::Edge());
377                                        e=&sc->m_edges[sc->m_edges.size()-1];
378                                        e->n[0]=nrm;e->n[1]=-nrm;
379                                        e->v[0]=a;e->v[1]=b;
380                                }
381                                else
382                                {
383                                        e->n[1]=nrm;
384                                }
385                        }
386                }
387        }
388        return(sc);
389}
390
391void renderSquareA(float x, float y, float z)
392{
393        glBegin(GL_LINE_LOOP);
394        glVertex3f(x, y, z);
395        glVertex3f(x + 10., y, z);
396        glVertex3f(x + 10., y + 10., z);
397        glVertex3f(x, y + 10., z);
398        glEnd();
399}
400
401inline void glDrawVector(const btVector3& v) { glVertex3d(v[0], v[1], v[2]); }
402
403
404void GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, const btVector3& color,int  debugMode,const btVector3& worldBoundsMin,const btVector3& worldBoundsMax)
405{
406        if (shape->getShapeType() == CUSTOM_CONVEX_SHAPE_TYPE)
407        {
408                btVector3 org(m[12], m[13], m[14]);
409                btVector3 dx(m[0], m[1], m[2]);
410                btVector3 dy(m[4], m[5], m[6]);
411//              btVector3 dz(m[8], m[9], m[10]);
412                const btBoxShape* boxShape = static_cast<const btBoxShape*>(shape);
413                btVector3 halfExtent = boxShape->getHalfExtentsWithMargin();
414                dx *= halfExtent[0];
415                dy *= halfExtent[1];
416//              dz *= halfExtent[2];
417                glColor3f(1,1,1);
418                glDisable(GL_LIGHTING);
419                glLineWidth(2);
420
421                glBegin(GL_LINE_LOOP);
422                glDrawVector(org - dx - dy);
423                glDrawVector(org - dx + dy);
424                glDrawVector(org + dx + dy);
425                glDrawVector(org + dx - dy);
426                glEnd();
427                return;
428        }
429        else if((shape->getShapeType() == BOX_SHAPE_PROXYTYPE) && (debugMode & btIDebugDraw::DBG_FastWireframe))
430        {
431                btVector3 org(m[12], m[13], m[14]);
432                btVector3 dx(m[0], m[1], m[2]);
433                btVector3 dy(m[4], m[5], m[6]);
434                btVector3 dz(m[8], m[9], m[10]);
435                const btBoxShape* boxShape = static_cast<const btBoxShape*>(shape);
436                btVector3 halfExtent = boxShape->getHalfExtentsWithMargin();
437                dx *= halfExtent[0];
438                dy *= halfExtent[1];
439                dz *= halfExtent[2];
440                glBegin(GL_LINE_LOOP);
441                glDrawVector(org - dx - dy - dz);
442                glDrawVector(org + dx - dy - dz);
443                glDrawVector(org + dx + dy - dz);
444                glDrawVector(org - dx + dy - dz);
445                glDrawVector(org - dx + dy + dz);
446                glDrawVector(org + dx + dy + dz);
447                glDrawVector(org + dx - dy + dz);
448                glDrawVector(org - dx - dy + dz);
449                glEnd();
450                glBegin(GL_LINES);
451                glDrawVector(org + dx - dy - dz);
452                glDrawVector(org + dx - dy + dz);
453                glDrawVector(org + dx + dy - dz);
454                glDrawVector(org + dx + dy + dz);
455                glDrawVector(org - dx - dy - dz);
456                glDrawVector(org - dx + dy - dz);
457                glDrawVector(org - dx - dy + dz);
458                glDrawVector(org - dx + dy + dz);
459                glEnd();
460                return;
461        }
462
463        glPushMatrix();
464        btglMultMatrix(m);
465
466
467        if (shape->getShapeType() == UNIFORM_SCALING_SHAPE_PROXYTYPE)
468        {
469                const btUniformScalingShape* scalingShape = static_cast<const btUniformScalingShape*>(shape);
470                const btConvexShape* convexShape = scalingShape->getChildShape();
471                float   scalingFactor = (float)scalingShape->getUniformScalingFactor();
472                {
473                        btScalar tmpScaling[4][4]={{scalingFactor,0,0,0},
474                        {0,scalingFactor,0,0},
475                        {0,0,scalingFactor,0},
476                        {0,0,0,1}};
477
478                        drawOpenGL( (btScalar*)tmpScaling,convexShape,color,debugMode,worldBoundsMin,worldBoundsMax);
479                }
480                glPopMatrix();
481                return;
482        }
483
484        if (shape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE)
485        {
486                const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(shape);
487                for (int i=compoundShape->getNumChildShapes()-1;i>=0;i--)
488                {
489                        btTransform childTrans = compoundShape->getChildTransform(i);
490                        const btCollisionShape* colShape = compoundShape->getChildShape(i);
491                        btScalar childMat[16];
492                        childTrans.getOpenGLMatrix(childMat);
493                        drawOpenGL(childMat,colShape,color,debugMode,worldBoundsMin,worldBoundsMax);
494                }
495
496        } else
497        {
498                if(m_textureenabled&&(!m_textureinitialized))
499                {
500                        GLubyte*        image=new GLubyte[256*256*3];
501                        for(int y=0;y<256;++y)
502                        {
503                                const int       t=y>>4;
504                                GLubyte*        pi=image+y*256*3;
505                                for(int x=0;x<256;++x)
506                                {
507                                        const int               s=x>>4;
508                                        const GLubyte   b=180;
509                                        GLubyte                 c=b+(((s+t)&1)&1)*(255-b);
510                                        pi[0]=pi[1]=pi[2]=c;pi+=3;
511                                }
512                        }
513
514                        glGenTextures(1,(GLuint*)&m_texturehandle);
515                        glBindTexture(GL_TEXTURE_2D,m_texturehandle);
516                        glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
517                        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
518                        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
519                        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
520                        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
521                        gluBuild2DMipmaps(GL_TEXTURE_2D,3,256,256,GL_RGB,GL_UNSIGNED_BYTE,image);
522                        delete[] image;
523
524                        glMatrixMode(GL_TEXTURE);
525                        glLoadIdentity();
526                        glScalef(0.025,0.025,0.025);
527
528
529                }
530
531
532
533                static const GLfloat    planex[]={1,0,0,0};
534//                      static const GLfloat    planey[]={0,1,0,0};
535                        static const GLfloat    planez[]={0,0,1,0};
536                        glTexGenfv(GL_S,GL_OBJECT_PLANE,planex);
537                        glTexGenfv(GL_T,GL_OBJECT_PLANE,planez);
538                        glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEAR);
539                        glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEAR);
540                        glEnable(GL_TEXTURE_GEN_S);
541                        glEnable(GL_TEXTURE_GEN_T);
542                        glEnable(GL_TEXTURE_GEN_R);
543                        m_textureinitialized=true;
544
545
546
547
548                //drawCoordSystem();
549
550                //glPushMatrix();
551                glEnable(GL_COLOR_MATERIAL);
552                if(m_textureenabled)
553                {
554                        glEnable(GL_TEXTURE_2D);
555                        glBindTexture(GL_TEXTURE_2D,m_texturehandle);
556                } else
557                {
558                        glDisable(GL_TEXTURE_2D);
559                }
560
561
562                glColor3f(color.x(),color.y(), color.z());
563
564                bool useWireframeFallback = true;
565
566                if (!(debugMode & btIDebugDraw::DBG_DrawWireframe))
567                {
568                        ///you can comment out any of the specific cases, and use the default
569
570                        ///the benefit of 'default' is that it approximates the actual collision shape including collision margin
571                        //int shapetype=m_textureenabled?MAX_BROADPHASE_COLLISION_TYPES:shape->getShapeType();
572                        int shapetype=shape->getShapeType();
573                        switch (shapetype)
574                        {
575
576                                case SPHERE_SHAPE_PROXYTYPE:
577                                {
578                                        const btSphereShape* sphereShape = static_cast<const btSphereShape*>(shape);
579                                        float radius = sphereShape->getMargin();//radius doesn't include the margin, so draw with margin
580                                        drawSphere(radius,10,10);
581                                        useWireframeFallback = false;
582                                        break;
583                                }
584
585                                case BOX_SHAPE_PROXYTYPE:
586                                {
587                                        const btBoxShape* boxShape = static_cast<const btBoxShape*>(shape);
588                                        btVector3 halfExtent = boxShape->getHalfExtentsWithMargin();
589
590                                        static int indices[36] = {
591                                                0,1,2,
592                                                3,2,1,
593                                                4,0,6,
594                                                6,0,2,
595                                                5,1,4,
596                                                4,1,0,
597                                                7,3,1,
598                                                7,1,5,
599                                                5,4,7,
600                                                7,4,6,
601                                                7,2,3,
602                                                7,6,2};
603
604                                        static btVector3 vertices[8]={  btVector3(1,1,1),btVector3(-1,1,1),     btVector3(1,-1,1),      btVector3(-1,-1,1),     btVector3(1,1,-1),      btVector3(-1,1,-1),     btVector3(1,-1,-1),     btVector3(-1,-1,-1)};
605                                        glBegin (GL_TRIANGLES);
606                                        int si=36;
607                                        for (int i=0;i<si;i+=3)
608                                        {
609                                                btVector3 v1 = vertices[indices[i]]*halfExtent;
610                                                btVector3 v2 = vertices[indices[i+1]]*halfExtent;
611                                                btVector3 v3 = vertices[indices[i+2]]*halfExtent;
612                                                btVector3 normal = (v3-v1).cross(v2-v1);
613                                                normal.normalize ();
614
615                                                glNormal3f(normal.getX(),normal.getY(),normal.getZ());
616                                                glVertex3f (v1.x(), v1.y(), v1.z());
617                                                glVertex3f (v2.x(), v2.y(), v2.z());
618                                                glVertex3f (v3.x(), v3.y(), v3.z());
619
620                                        }
621                                        glEnd();
622
623                                        useWireframeFallback = false;
624                                        break;
625                                }
626
627
628
629#if 0
630
631                        case CONE_SHAPE_PROXYTYPE:
632                                {
633                                        const btConeShape* coneShape = static_cast<const btConeShape*>(shape);
634                                        int upIndex = coneShape->getConeUpIndex();
635                                        float radius = coneShape->getRadius();//+coneShape->getMargin();
636                                        float height = coneShape->getHeight();//+coneShape->getMargin();
637                                        switch (upIndex)
638                                        {
639                                        case 0:
640                                                glRotatef(90.0, 0.0, 1.0, 0.0);
641                                                break;
642                                        case 1:
643                                                glRotatef(-90.0, 1.0, 0.0, 0.0);
644                                                break;
645                                        case 2:
646                                                break;
647                                        default:
648                                                {
649                                                }
650                                        };
651
652                                        glTranslatef(0.0, 0.0, -0.5*height);
653                                        glutSolidCone(radius,height,10,10);
654                                        useWireframeFallback = false;
655                                        break;
656
657                                }
658#endif
659
660                        case STATIC_PLANE_PROXYTYPE:
661                                {
662                                        const btStaticPlaneShape* staticPlaneShape = static_cast<const btStaticPlaneShape*>(shape);
663                                        btScalar planeConst = staticPlaneShape->getPlaneConstant();
664                                        const btVector3& planeNormal = staticPlaneShape->getPlaneNormal();
665                                        btVector3 planeOrigin = planeNormal * planeConst;
666                                        btVector3 vec0,vec1;
667                                        btPlaneSpace1(planeNormal,vec0,vec1);
668                                        btScalar vecLen = 100.f;
669                                        btVector3 pt0 = planeOrigin + vec0*vecLen;
670                                        btVector3 pt1 = planeOrigin - vec0*vecLen;
671                                        btVector3 pt2 = planeOrigin + vec1*vecLen;
672                                        btVector3 pt3 = planeOrigin - vec1*vecLen;
673                                        glBegin(GL_LINES);
674                                        glVertex3f(pt0.getX(),pt0.getY(),pt0.getZ());
675                                        glVertex3f(pt1.getX(),pt1.getY(),pt1.getZ());
676                                        glVertex3f(pt2.getX(),pt2.getY(),pt2.getZ());
677                                        glVertex3f(pt3.getX(),pt3.getY(),pt3.getZ());
678                                        glEnd();
679
680
681                                        break;
682
683                                }
684
685/*
686                        case CYLINDER_SHAPE_PROXYTYPE:
687                                {
688                                        const btCylinderShape* cylinder = static_cast<const btCylinderShape*>(shape);
689                                        int upAxis = cylinder->getUpAxis();
690
691
692                                        float radius = cylinder->getRadius();
693                                        float halfHeight = cylinder->getHalfExtentsWithMargin()[upAxis];
694
695                                        drawCylinder(radius,halfHeight,upAxis);
696
697                                        break;
698                                }
699*/
700
701                        case MULTI_SPHERE_SHAPE_PROXYTYPE:
702                        {
703                                const btMultiSphereShape* multiSphereShape = static_cast<const btMultiSphereShape*>(shape);
704
705                                btTransform childTransform;
706                                childTransform.setIdentity();
707
708
709                                for (int i = multiSphereShape->getSphereCount()-1; i>=0;i--)
710                                {
711                                        btSphereShape sc(multiSphereShape->getSphereRadius(i));
712                                        childTransform.setOrigin(multiSphereShape->getSpherePosition(i));
713                                        btScalar childMat[16];
714                                        childTransform.getOpenGLMatrix(childMat);
715                                        drawOpenGL(childMat,&sc,color,debugMode,worldBoundsMin,worldBoundsMax);
716                                }
717
718                                break;
719                        }
720
721                        default:
722                                {
723
724
725                                        if (shape->isConvex())
726                                        {
727                                                ShapeCache*     sc=cache((btConvexShape*)shape);
728#if 0
729                                                btConvexShape* convexShape = (btConvexShape*)shape;
730                                                if (!shape->getUserPointer())
731                                                {
732                                                        //create a hull approximation
733                                                        void* mem = btAlignedAlloc(sizeof(btShapeHull),16);
734                                                        btShapeHull* hull = new(mem) btShapeHull(convexShape);
735
736                                                        ///cleanup memory
737                                                        m_shapeHulls.push_back(hull);
738
739                                                        btScalar margin = shape->getMargin();
740                                                        hull->buildHull(margin);
741                                                        convexShape->setUserPointer(hull);
742
743
744                                                        //      printf("numTriangles = %d\n", hull->numTriangles ());
745                                                        //      printf("numIndices = %d\n", hull->numIndices ());
746                                                        //      printf("numVertices = %d\n", hull->numVertices ());
747
748
749                                                }
750#endif
751
752
753
754                                                //if (shape->getUserPointer())
755                                                {
756                                                        //glutSolidCube(1.0);
757                                                        btShapeHull* hull = &sc->m_shapehull/*(btShapeHull*)shape->getUserPointer()*/;
758
759
760                                                        if (hull->numTriangles () > 0)
761                                                        {
762                                                                int index = 0;
763                                                                const unsigned int* idx = hull->getIndexPointer();
764                                                                const btVector3* vtx = hull->getVertexPointer();
765
766                                                                glBegin (GL_TRIANGLES);
767
768                                                                for (int i = 0; i < hull->numTriangles (); i++)
769                                                                {
770                                                                        int i1 = index++;
771                                                                        int i2 = index++;
772                                                                        int i3 = index++;
773                                                                        btAssert(i1 < hull->numIndices () &&
774                                                                                i2 < hull->numIndices () &&
775                                                                                i3 < hull->numIndices ());
776
777                                                                        int index1 = idx[i1];
778                                                                        int index2 = idx[i2];
779                                                                        int index3 = idx[i3];
780                                                                        btAssert(index1 < hull->numVertices () &&
781                                                                                index2 < hull->numVertices () &&
782                                                                                index3 < hull->numVertices ());
783
784                                                                        btVector3 v1 = vtx[index1];
785                                                                        btVector3 v2 = vtx[index2];
786                                                                        btVector3 v3 = vtx[index3];
787                                                                        btVector3 normal = (v3-v1).cross(v2-v1);
788                                                                        normal.normalize ();
789
790                                                                        glNormal3f(normal.getX(),normal.getY(),normal.getZ());
791                                                                        glVertex3f (v1.x(), v1.y(), v1.z());
792                                                                        glVertex3f (v2.x(), v2.y(), v2.z());
793                                                                        glVertex3f (v3.x(), v3.y(), v3.z());
794
795                                                                }
796                                                                glEnd ();
797
798                                                        }
799                                                }
800                                        }
801                                }
802                        }
803
804                }
805
806
807
808
809                /// for polyhedral shapes
810                if (debugMode==btIDebugDraw::DBG_DrawFeaturesText && (shape->isPolyhedral()))
811                {
812                        btPolyhedralConvexShape* polyshape = (btPolyhedralConvexShape*) shape;
813
814                        {
815                                glRasterPos3f(0.0,  0.0,  0.0);
816                                //btDrawString(BMF_GetFont(BMF_kHelvetica10),polyshape->getExtraDebugInfo());
817
818                                glColor3f(1.f, 1.f, 1.f);
819                                int i;
820                                for (i=0;i<polyshape->getNumVertices();i++)
821                                {
822                                        btVector3 vtx;
823                                        polyshape->getVertex(i,vtx);
824                                        glRasterPos3f(vtx.x(),  vtx.y(),  vtx.z());
825                                        char buf[12];
826                                        sprintf(buf," %d",i);
827                                        //btDrawString(BMF_GetFont(BMF_kHelvetica10),buf);
828                                }
829
830                                for (i=0;i<polyshape->getNumPlanes();i++)
831                                {
832                                        btVector3 normal;
833                                        btVector3 vtx;
834                                        polyshape->getPlane(normal,vtx,i);
835                                        btScalar d = vtx.dot(normal);
836
837                                        glRasterPos3f(normal.x()*d,  normal.y()*d, normal.z()*d);
838                                        char buf[12];
839                                        sprintf(buf," plane %d",i);
840                                        //btDrawString(BMF_GetFont(BMF_kHelvetica10),buf);
841
842                                }
843                        }
844
845                }
846
847
848#ifdef USE_DISPLAY_LISTS
849
850                if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE||shape->getShapeType() == GIMPACT_SHAPE_PROXYTYPE)
851                {
852                        GLuint dlist =   OGL_get_displaylist_for_shape((btCollisionShape * )shape);
853                        if (dlist)
854                        {
855                                glCallList(dlist);
856                        }
857                        else
858                        {
859#else
860                if (shape->isConcave() && !shape->isInfinite())
861                {
862                        btConcaveShape* concaveMesh = (btConcaveShape*) shape;
863
864                        GlDrawcallback drawCallback;
865                        drawCallback.m_wireframe = (debugMode & btIDebugDraw::DBG_DrawWireframe)!=0;
866
867                        concaveMesh->processAllTriangles(&drawCallback,worldBoundsMin,worldBoundsMax);
868
869                }
870#endif
871
872#ifdef USE_DISPLAY_LISTS
873        }
874}
875#endif
876
877
878
879
880
881glDisable(GL_DEPTH_TEST);
882glRasterPos3f(0,0,0);//mvtx.x(),  vtx.y(),  vtx.z());
883if (debugMode&btIDebugDraw::DBG_DrawText)
884{
885        GLDebugDrawString(0,0,shape->getName());
886}
887
888if (debugMode& btIDebugDraw::DBG_DrawFeaturesText)
889{
890        //btDrawString(BMF_GetFont(BMF_kHelvetica10),shape->getExtraDebugInfo());
891}
892glEnable(GL_DEPTH_TEST);
893
894//      glPopMatrix();
895if(m_textureenabled) glDisable(GL_TEXTURE_2D);
896        }
897        glPopMatrix();
898
899}
900
901//
902void            GL_ShapeDrawer::drawShadow(btScalar* m,const btVector3& extrusion,const btCollisionShape* shape,const btVector3& worldBoundsMin,const btVector3& worldBoundsMax)
903{
904        glPushMatrix();
905        btglMultMatrix(m);
906        if(shape->getShapeType() == UNIFORM_SCALING_SHAPE_PROXYTYPE)
907        {
908                const btUniformScalingShape* scalingShape = static_cast<const btUniformScalingShape*>(shape);
909                const btConvexShape* convexShape = scalingShape->getChildShape();
910                float   scalingFactor = (float)scalingShape->getUniformScalingFactor();
911                btScalar tmpScaling[4][4]={     {scalingFactor,0,0,0},
912                {0,scalingFactor,0,0},
913                {0,0,scalingFactor,0},
914                {0,0,0,1}};
915                drawShadow((btScalar*)tmpScaling,extrusion,convexShape,worldBoundsMin,worldBoundsMax);
916                glPopMatrix();
917                return;
918        }
919        else if(shape->getShapeType()==COMPOUND_SHAPE_PROXYTYPE)
920        {
921                const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(shape);
922                for (int i=compoundShape->getNumChildShapes()-1;i>=0;i--)
923                {
924                        btTransform childTrans = compoundShape->getChildTransform(i);
925                        const btCollisionShape* colShape = compoundShape->getChildShape(i);
926                        btScalar childMat[16];
927                        childTrans.getOpenGLMatrix(childMat);
928                        drawShadow(childMat,extrusion*childTrans.getBasis(),colShape,worldBoundsMin,worldBoundsMax);
929                }
930        }
931        else
932        {
933                if (shape->isConvex())
934                {
935                        ShapeCache*     sc=cache((btConvexShape*)shape);
936                        btShapeHull* hull =&sc->m_shapehull;
937                        glBegin(GL_QUADS);
938                        for(int i=0;i<sc->m_edges.size();++i)
939                        {
940                                const btScalar          d=btDot(sc->m_edges[i].n[0],extrusion);
941                                if((d*btDot(sc->m_edges[i].n[1],extrusion))<0)
942                                {
943                                        const int                       q=      d<0?1:0;
944                                        const btVector3&        a=      hull->getVertexPointer()[sc->m_edges[i].v[q]];
945                                        const btVector3&        b=      hull->getVertexPointer()[sc->m_edges[i].v[1-q]];
946                                        glVertex3f(a[0],a[1],a[2]);
947                                        glVertex3f(b[0],b[1],b[2]);
948                                        glVertex3f(b[0]+extrusion[0],b[1]+extrusion[1],b[2]+extrusion[2]);
949                                        glVertex3f(a[0]+extrusion[0],a[1]+extrusion[1],a[2]+extrusion[2]);
950                                }
951                        }
952                        glEnd();
953                }
954
955        }
956
957
958
959
960        if (shape->isConcave())//>getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE||shape->getShapeType() == GIMPACT_SHAPE_PROXYTYPE)
961                //              if (shape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
962        {
963                btConcaveShape* concaveMesh = (btConcaveShape*) shape;
964
965                GlDrawcallback drawCallback;
966                drawCallback.m_wireframe = false;
967
968                concaveMesh->processAllTriangles(&drawCallback,worldBoundsMin,worldBoundsMax);
969
970        }
971        glPopMatrix();
972
973}
974
975//
976GL_ShapeDrawer::GL_ShapeDrawer()
977{
978        m_texturehandle                 =       0;
979        m_textureenabled                =       false;
980        m_textureinitialized    =       false;
981}
982
983GL_ShapeDrawer::~GL_ShapeDrawer()
984{
985        int i;
986        for (i=0;i<m_shapecaches.size();i++)
987        {
988                m_shapecaches[i]->~ShapeCache();
989                btAlignedFree(m_shapecaches[i]);
990        }
991        m_shapecaches.clear();
992        if(m_textureinitialized)
993        {
994                glDeleteTextures(1,(const GLuint*) &m_texturehandle);
995        }
996}
997
998
Note: See TracBrowser for help on using the repository browser.