Enabling Bullet Debug Draw (Code included)

View previous topic View next topic Go down

Enabling Bullet Debug Draw (Code included)

Post  Francescu on Fri May 08, 2009 7:29 pm

Here is the code and information on how to enable wireframe drawing of your world / scene meshes in Bullet and in your iDevice.

Bullet needs an implementation of the btIDebugDraw interface and then you have to register the implementation and instance of the class with bullet.
You then have to tell bullet to debug draw during rendering pass...(before swapbuffers)...

Just add GLDebugDrawer.h and GLDebugDrawer.mm to your "Other Sources" section in XCode for your project and then add the following in your template.mm

Here I'm using Tutorial06_1 as an example:

Code:

#include "template.h"

#include "../src/sio2/sio2.h"

/*********************/
#include "GLDebugDrawer.h"

GLDebugDrawer debugDrawer;
/*********************/


then in templateLoading() after sio2PhysicPlay() call

Code:


      // Tell our physic simulation to start.
      sio2PhysicPlay( sio2->_SIO2physic );
      
/*********************/
      debugDrawer.setDebugMode(btIDebugDraw::DBG_DrawWireframe);
      sio2->_SIO2physic->_btSoftRigidDynamicsWorld->setDebugDrawer(&debugDrawer);
/*********************/

then in templateRender() before sio2WindowLeaveLandscape3D() call

Code:

      // Reset back all OpenGL states required to draw properly
      // the previous objects.
      sio2ObjectReset();
      
      sio2MaterialReset();
      
/*********************/
      sio2->_SIO2physic->_btSoftRigidDynamicsWorld->debugDrawWorld();
/*********************/
   }
   sio2WindowLeaveLandscape3D();

I'm pasting the code for GLDebugDrawer.h and GLDebugDrawer.mm as I don't know where to host / attach these files...if anyone knows let me know...

GLDebugDrawer.h

Code:

#ifndef GL_DEBUG_DRAWER_H
#define GL_DEBUG_DRAWER_H

#include "../src/bullet/btIDebugDraw.h"

extern "C"
{
#include <OpenGLES/EAGL.h>
#include <OpenGLES/ES1/gl.h>
#include <OpenGLES/ES1/glext.h>
}

class GLDebugDrawer : public btIDebugDraw
{
        int m_debugMode;
      
   public:
      
        GLDebugDrawer();
            
        virtual void   drawLine(const btVector3& from,const btVector3& to,const btVector3& color);
      
        virtual void   drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color);
      
        virtual void   reportErrorWarning(const char* warningString);
      
        virtual void   draw3dText(const btVector3& location,const char* textString);
      
        virtual void   setDebugMode(int debugMode);
      
        virtual int      getDebugMode() const { return m_debugMode;}
      
};
#endif

GLDebugDrawer.mm
Code:

#include "GLDebugDrawer.h"

#include <stdio.h> //printf debugging

GLDebugDrawer::GLDebugDrawer()
:m_debugMode(0)
{
   
}

void    GLDebugDrawer::drawLine(const btVector3& from,const btVector3& to,const btVector3& color)
{
   //      if (m_debugMode > 0)
   {
      float tmp[ 6 ] = { from.getX(), from.getY(), from.getZ(),
                    to.getX(), to.getY(), to.getZ() };
      
      glPushMatrix();
      {         
         glColor4f(color.getX(), color.getY(), color.getZ(), 1.0f);         
         glVertexPointer( 3,
                     GL_FLOAT,
                     0,
                     &tmp );
         
         glPointSize( 5.0f );
         glDrawArrays( GL_POINTS, 0, 2 );
         glDrawArrays( GL_LINES, 0, 2 );
      }
      glPopMatrix();      
   }
}

void    GLDebugDrawer::setDebugMode(int debugMode)
{
   m_debugMode = debugMode;
}

void    GLDebugDrawer::draw3dText(const btVector3& location,const char* textString)
{
   //glRasterPos3f(location.x(),  location.y(),  location.z());
   //BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),textString);
}

void    GLDebugDrawer::reportErrorWarning(const char* warningString)
{
   printf(warningString);
}

void    GLDebugDrawer::drawContactPoint(const btVector3& pointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color)
{
   {
      //btVector3 to=pointOnB+normalOnB*distance;
      //const btVector3&from = pointOnB;
      //glColor4f(color.getX(), color.getY(), color.getZ(), 1.0f);   
      
      //GLDebugDrawer::drawLine(from, to, color);
      
      //glRasterPos3f(from.x(),  from.y(),  from.z());
      //char buf[12];
      //sprintf(buf," %d",lifeTime);
      //BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf);
   }
}

The end result on the iDevice and hope it is useful for your bullet / OGL debugging purposes:


Thanks to SIO2 (ROm) for telling me Bullet has such debug draw support...

Francescu

Posts: 136
Join date: 2009-03-18

View user profile

Back to top Go down

Re: Enabling Bullet Debug Draw (Code included)

Post  sio2interactive on Fri May 08, 2009 7:46 pm

tks for sharing really helpful for alot of people Im sure!

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com

sio2interactive

Posts: 1526
Join date: 2008-08-26
Age: 35
Location: Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: Enabling Bullet Debug Draw (Code included)

Post  ColorDelta on Sat May 09, 2009 4:41 am

This is very useful, Francescu - thanks for taking the time to create this example.

Would it be worth adding this to the Wiki?

ColorDelta

Posts: 16
Join date: 2009-03-25
Location: London, UK

View user profile

Back to top Go down

Re: Enabling Bullet Debug Draw (Code included)

Post  sio2interactive on Sat May 09, 2009 4:57 am

Well do you guys want to have this feature included In the next version? I can always hook it up with sio2physic...

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com

sio2interactive

Posts: 1526
Join date: 2008-08-26
Age: 35
Location: Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: Enabling Bullet Debug Draw (Code included)

Post  Francescu on Sat May 09, 2009 12:49 pm

It would be great to add this to the WIKI - if someone wants to do it, fine - otherwise I'll do it as soon as I have some cycles...

2 other methods need to be implemented - I did comment out the code where some OpenGL calls were not available in GL ES...

For instance, being able to draw the collisions' contact points would be great - method to be implemented is GLDebugDrawer::drawContactPoint()
One just needs to implement 3D text drawing as these calls/libraries below:

Code:

   //glRasterPos3f(location.x(),  location.y(),  location.z());
   //BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),textString);

are not supported in GL ES...

Cheers

Francescu

Posts: 136
Join date: 2009-03-18

View user profile

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

Permissions in this forum:
You cannot reply to topics in this forum