Cocos2d-x: 2.0.1 (2012-06-29)
Box2d: v2.2.0
Usage:
mWorld = new b2World(b2Vec2(0.0f, -15.0f));
b2DebugDraw *debugDraw = new b2DebugDraw(PTM_RATIO);
mWorld->SetDebugDraw(debugDraw);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
flags += b2Draw::e_jointBit;
flags += b2Draw::e_centerOfMassBit;
flags += b2Draw::e_aabbBit;
flags += b2Draw::e_pairBit;
debugDraw->SetFlags(flags);
Drawing:
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
kmGLPushMatrix();
mWorld->DrawDebugData();
kmGLPopMatrix();
Header:
#include "Box2D/Box2D.h"
class b2DebugDraw : public b2Draw
{
public:
b2DebugDraw( float32 ratio );
void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color);
void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color);
void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color);
void DrawTransform(const b2Transform& xf);
void DrawPoint(const b2Vec2& p, float32 size, const b2Color& color);
void DrawString(int x, int y, const char* string, ...);
void DrawAABB(b2AABB* aabb, const b2Color& color);
private:
cocos2d::CCGLProgram *mShaderProgram;
GLint mColorLocation;
float mRatio;
};
Source:
b2DebugDraw::b2DebugDraw( float32 ratio ) : mRatio( ratio )
{
mShaderProgram = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_Position_uColor);
mColorLocation = glGetUniformLocation( mShaderProgram->getProgram(), "u_color");
}
void b2DebugDraw::DrawPolygon(const b2Vec2* old_vertices, int32 vertexCount, const b2Color& color)
{
mShaderProgram->use();
mShaderProgram->setUniformForModelViewProjectionMatrix();
ccVertex2F *vertices = new ccVertex2F[vertexCount];
for( int i=0;isetUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1);
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glDrawArrays(GL_LINE_LOOP, 0, vertexCount);
delete []vertices;
CC_INCREMENT_GL_DRAWS(1);
CHECK_GL_ERROR_DEBUG();
}
void b2DebugDraw::DrawSolidPolygon(const b2Vec2* old_vertices, int32 vertexCount, const b2Color& color)
{
mShaderProgram->use();
mShaderProgram->setUniformForModelViewProjectionMatrix();
ccVertex2F *vertices = new ccVertex2F[vertexCount];
for( int i=0;isetUniformLocationWith4f(mColorLocation, color.r*0.5f, color.g*0.5f, color.b*0.5f, 0.5f);
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount);
mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1);
glDrawArrays(GL_LINE_LOOP, 0, vertexCount);
delete []vertices;
CC_INCREMENT_GL_DRAWS(2);
CHECK_GL_ERROR_DEBUG();
}
void b2DebugDraw::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
{
mShaderProgram->use();
mShaderProgram->setUniformForModelViewProjectionMatrix();
const float32 k_segments = 16.0f;
const int vertexCount=16;
const float32 k_increment = 2.0f * b2_pi / k_segments;
float32 theta = 0.0f;
GLfloat glVertices[vertexCount*2];
for (int32 i = 0; i < k_segments; ++i)
{
b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
glVertices[i*2]=v.x * mRatio;
glVertices[i*2+1]=v.y * mRatio;
theta += k_increment;
}
mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1);
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, glVertices);
glDrawArrays(GL_LINE_LOOP, 0, vertexCount);
CC_INCREMENT_GL_DRAWS(1);
CHECK_GL_ERROR_DEBUG();
}
void b2DebugDraw::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
{
mShaderProgram->use();
mShaderProgram->setUniformForModelViewProjectionMatrix();
const float32 k_segments = 16.0f;
const int vertexCount=16;
const float32 k_increment = 2.0f * b2_pi / k_segments;
float32 theta = 0.0f;
GLfloat glVertices[vertexCount*2];
for (int32 i = 0; i < k_segments; ++i)
{
b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
glVertices[i*2]=v.x * mRatio;
glVertices[i*2+1]=v.y * mRatio;
theta += k_increment;
}
mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r*0.5f, color.g*0.5f, color.b*0.5f, 0.5f);
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, glVertices);
glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount);
mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1);
glDrawArrays(GL_LINE_LOOP, 0, vertexCount);
DrawSegment(center,center+radius*axis,color);
CC_INCREMENT_GL_DRAWS(2);
CHECK_GL_ERROR_DEBUG();
}
void b2DebugDraw::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
{
mShaderProgram->use();
mShaderProgram->setUniformForModelViewProjectionMatrix();
mShaderProgram->setUniformLocationWith4f(mColorLocation, color.r, color.g, color.b, 1);
GLfloat glVertices[] = {
p1.x * mRatio, p1.y * mRatio,
p2.x * mRatio, p2.y * mRatio
};
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, glVertices);
glDrawArrays(GL_LINES, 0, 2);
CC_INCREMENT_GL_DRAWS(1);
CHECK_GL_ERROR_DEBUG();
}
void b2DebugDraw::DrawTransform(const b2Transform& xf)
{
b2Vec2 p1 = xf.p, p2;
const float32 k_axisScale = 0.4f;
p2 = p1 + k_axisScale * xf.q.GetXAxis();
DrawSegment(p1, p2, b2Color(1,0,0));
p2 = p1 + k_axisScale * xf.q.GetYAxis();
DrawSegment(p1,p2,b2Color(0,1,0));
}
Cheers.