Hello!
For Sparrow 2.0 i have adapted the debug draw class:
GLES-Render.h
/*
* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
*
* Sparrow 2.0 Port by Wolion Games (c) 2013
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 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.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef GLES_RENDER_H
#define GLES_RENDER_H
#define KEY_COLORLINE @"colorline"
#define KEY_COLORFILL @"colorfill"
#define KEY_POINTS @"points"
#import <Availability.h>
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
#import <OpenGLES/EAGL.h>
#elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
#import <OpenGL/OpenGL.h>
#endif
#include "Box2D.h"
struct b2AABB;
// This class implements debug drawing callbacks that are invoked
// inside b2World::Step.
class GLESDebugDraw : public b2Draw
{
float32 mRatio;
SPVertexData* _verticesLines;
SPVertexData* _verticesFilled;
void AddToDebugLines(const b2Vec2 old_vertice, const b2Color color);
void AddToDebugFilled(const b2Vec2 old_vertice, const b2Color color);
SPBaseEffect *_baseEffect;
SPRenderSupport* _support;
public:
GLESDebugDraw();
GLESDebugDraw( 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);
void prepare(SPRenderSupport* support, SPBaseEffect* baseEffect);
};
#endif // GLES_RENDER_H
and the GLES-Render.mm file
/*
* Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
*
* Sparrow 2.0 Port by Wolion Games (c) 2013
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 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.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include "GLES-Render.h"
#include "Sparrow.h"
GLESDebugDraw::GLESDebugDraw()
: mRatio( 1.0f )
{
}
GLESDebugDraw::GLESDebugDraw( float32 ratio )
: mRatio( ratio )
{
_verticesLines = [[SPVertexData alloc]init];
_verticesFilled = [[SPVertexData alloc]init];
_baseEffect = [[SPBaseEffect alloc]init];
}
void GLESDebugDraw::prepare(SPRenderSupport* support, SPBaseEffect* baseEffect)
{
_verticesLines.numVertices=0;
_verticesFilled.numVertices=0;
_support = support;
_baseEffect = baseEffect;
}
void GLESDebugDraw::AddToDebugLines(const b2Vec2 old_vertice, const b2Color color)
{
SPVertex vertex;
b2Vec2 tmp = old_vertice;
tmp *= mRatio;
vertex.color.r = color.r*255;
vertex.color.g = color.g*255;
vertex.color.b = color.b*255;
vertex.color.a = 255;
vertex.position = GLKVector2Make(tmp.x,tmp.y);
[_verticesLines appendVertex:vertex];
}
void GLESDebugDraw::AddToDebugFilled(const b2Vec2 old_vertice, const b2Color color)
{
SPVertex vertex;
b2Vec2 tmp = old_vertice;
tmp *= mRatio;
vertex.color.r = color.r*255;
vertex.color.g = color.g*255;
vertex.color.b = color.b*255;
vertex.color.a = 255;
vertex.position = GLKVector2Make(tmp.x,tmp.y);
[_verticesFilled appendVertex:vertex];
}
void GLESDebugDraw::DrawPolygon(const b2Vec2* old_vertices, int32 vertexCount, const b2Color& color)
{
_verticesLines.numVertices = 0;
for( int i=0;i<vertexCount;i++)
{
AddToDebugLines(old_vertices[i], color);
}
int attribPosition = _baseEffect.attribPosition;
int attribColor = _baseEffect.attribColor;
glEnableVertexAttribArray(attribPosition);
glEnableVertexAttribArray(attribColor);
glVertexAttribPointer(attribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(SPVertex),
(void *)(&_verticesLines.vertices[0].position));
glVertexAttribPointer(attribColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(SPVertex),
(void *)(&_verticesLines.vertices[0].color));
glDrawArrays(GL_LINE_LOOP, 0, _verticesLines.numVertices);
}
void GLESDebugDraw::DrawSolidPolygon(const b2Vec2* old_vertices, int32 vertexCount, const b2Color& color)
{
b2Color fillColor = b2Color(color.r*0.5f,color.g*0.5f,color.b*0.5f);
_verticesFilled.numVertices = 0;
for( int i=0;i<vertexCount;i++)
{
AddToDebugFilled(old_vertices[i], fillColor);
}
int attribPosition = _baseEffect.attribPosition;
int attribColor = _baseEffect.attribColor;
glEnableVertexAttribArray(attribPosition);
glEnableVertexAttribArray(attribColor);
glVertexAttribPointer(attribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(SPVertex),
(void *)(&_verticesFilled.vertices[0].position));
glVertexAttribPointer(attribColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(SPVertex),
(void *)(&_verticesFilled.vertices[0].color));
glDrawArrays(GL_TRIANGLE_FAN, 0, _verticesFilled.numVertices);
DrawPolygon(old_vertices, vertexCount, color);
}
void GLESDebugDraw::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
{
const float32 k_segments = 16.0f;
const float32 k_increment = 2.0f * b2_pi / k_segments;
float32 theta = 0.0f;
b2Vec2 points[(int)k_segments];
for (int32 i = 0; i < k_segments; ++i)
{
points[i] = center + radius * b2Vec2(cosf(theta), sinf(theta));
theta += k_increment;
}
DrawPolygon(points, k_segments, color);
}
void GLESDebugDraw::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
{
const float32 k_segments = 16.0f;
const float32 k_increment = 2.0f * b2_pi / k_segments;
float32 theta = 0.0f;
b2Vec2 points[(int)k_segments];
for (int32 i = 0; i < k_segments; ++i)
{
points[i] = center + radius * b2Vec2(cosf(theta), sinf(theta));
theta += k_increment;
}
DrawSolidPolygon(points, k_segments, color);
DrawSegment(center,center+radius*axis,color);
}
void GLESDebugDraw::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
{
b2Vec2 points[2];
points[0]=p1;
points[1]=p2;
DrawPolygon(points, 2, color);
}
void GLESDebugDraw::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));
}
void GLESDebugDraw::DrawPoint(const b2Vec2& p, float32 size, const b2Color& color)
{
SPVertex point;
point.position.x=p.x* mRatio;
point.position.y=p.y* mRatio;
point.color.r = color.r*255;
point.color.g = color.g*255;
point.color.b = color.b*255;
point.color.a = 255;
int attribPosition = _baseEffect.attribPosition;
int attribColor = _baseEffect.attribColor;
glEnableVertexAttribArray(attribPosition);
glEnableVertexAttribArray(attribColor);
glVertexAttribPointer(attribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(SPVertex),
(void *)(&point.position));
glVertexAttribPointer(attribColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(SPVertex),
(void *)(&point.color));
glDrawArrays(GL_POINTS, 0, 1);
}
void GLESDebugDraw::DrawString(int x, int y, const char *string, ...)
{
// NSLog(@"DrawString: unsupported: %s", string);
//printf(string);
/* Unsupported as yet. Could replace with bitmap font renderer at a later date */
}
void GLESDebugDraw::DrawAABB(b2AABB* aabb, const b2Color& c)
{
b2Vec2 glVertices[] = {
{aabb->lowerBound.x * mRatio, aabb->lowerBound.y * mRatio},
{aabb->upperBound.x * mRatio, aabb->lowerBound.y * mRatio},
{aabb->upperBound.x * mRatio, aabb->upperBound.y * mRatio},
{aabb->lowerBound.x * mRatio, aabb->upperBound.y * mRatio}
};
DrawPolygon(glVertices, 4, c);
}
to use it:
in your world class
// implementation
#define PTM_RATIO 64;
GLESDebugDraw* _debugDraw;
b2World *_world;
_baseEffect = [[SPBaseEffect alloc]init];
// this comes in your init function
float debugDrawScaleFactor = 1.0f;
debugDrawScaleFactor *= PTM_RATIO;
_debugDraw = new GLESDebugDraw(debugDrawScaleFactor);
UInt32 debugDrawFlags = 0;
debugDrawFlags += b2Draw::e_shapeBit;
debugDrawFlags += b2Draw::e_jointBit;
debugDrawFlags += b2Draw::e_aabbBit;
// debugDrawFlags += b2Draw::e_pairBit;
// debugDrawFlags += b2Draw::e_centerOfMassBit;
_debugDraw->SetFlags(debugDrawFlags);
_world->SetDebugDraw(_debugDraw);
overwrite the render method in your class with your world definition
-(void) render:(SPRenderSupport *)support
{
[support finishQuadBatch]; // finish previously batched quads
[support addDrawCalls:1]; // update stats display
_baseEffect.mvpMatrix = support.mvpMatrix;
_baseEffect.alpha = support.alpha;
[_baseEffect prepareToDraw];
[SPBlendMode applyBlendFactorsForBlendMode:support.blendMode premultipliedAlpha:NO];
_debugDraw->prepare(support, _baseEffect); // prepare the drawing
_world->DrawDebugData();
}
i hope that i have correctly implemented the handling of the new opengl things.