This is a small patch for Build/iPhone/Box2d/Classes/GLES-Render.*
The patch adds support for "pixels 2 meter" ratio.
This patch is useful when:
- your GL view has a different aspect ratio of 1 pixel == 1 meter (which is very probable)
- and if you want to draw the shapes with the new ratio
Patch against svn box2d r228:
The patch adds support for "pixels 2 meter" ratio.
This patch is useful when:
- your GL view has a different aspect ratio of 1 pixel == 1 meter (which is very probable)
- and if you want to draw the shapes with the new ratio
Code:
// How to use it:
// if 1 meter == 32 pixels, then:
m_debugDraw = new GLESDebugDraw( 32.0f ); // <-- pixel to meters ratio
world->SetDebugDraw(m_debugDraw);
// if 1 meter == 32 pixels, then:
m_debugDraw = new GLESDebugDraw( 32.0f ); // <-- pixel to meters ratio
world->SetDebugDraw(m_debugDraw);
Patch against svn box2d r228:
Code:
Index: GLES-Render.mm
===================================================================
--- GLES-Render.mm (revision 1263)
+++ GLES-Render.mm (working copy)
@@ -26,15 +26,37 @@
#include <cstring>
-void GLESDebugDraw::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
+GLESDebugDraw::GLESDebugDraw()
+ : mRatio( 1.0f )
{
+}
+GLESDebugDraw::GLESDebugDraw( float32 ratio )
+ : mRatio( ratio )
+{
+}
+
+
+void GLESDebugDraw::DrawPolygon(const b2Vec2* old_vertices, int32 vertexCount, const b2Color& color)
+{
+ b2Vec2 vertices[vertexCount];
+ for( int i=0;i<vertexCount;i++) {
+ vertices[i] = old_vertices[i];
+ vertices[i] *= mRatio;
+ }
+
glColor4f(color.r, color.g, color.b,1);
glVertexPointer(2, GL_FLOAT, 0, vertices);
- glDrawArrays(GL_LINE_LOOP, 0, vertexCount);
+ glDrawArrays(GL_LINE_LOOP, 0, vertexCount);
}
-void GLESDebugDraw::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
+void GLESDebugDraw::DrawSolidPolygon(const b2Vec2* old_vertices, int32 vertexCount, const b2Color& color)
{
+ b2Vec2 vertices[vertexCount];
+ for( int i=0;i<vertexCount;i++) {
+ vertices[i] = old_vertices[i];
+ vertices[i] *= mRatio;
+ }
+
glVertexPointer(2, GL_FLOAT, 0, vertices);
glColor4f(color.r, color.g, color.b,0.5f);
@@ -46,6 +68,7 @@
void GLESDebugDraw::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
{
+
const float32 k_segments = 16.0f;
int vertexCount=16;
const float32 k_increment = 2.0f * b2_pi / k_segments;
@@ -55,8 +78,8 @@
for (int32 i = 0; i < k_segments; ++i)
{
b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
- glVertices[i*2]=v.x;
- glVertices[i*2+1]=v.y;
+ glVertices[i*2]=v.x * mRatio;
+ glVertices[i*2+1]=v.y * mRatio;
theta += k_increment;
}
@@ -68,6 +91,7 @@
void GLESDebugDraw::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
{
+
const float32 k_segments = 16.0f;
int vertexCount=16;
const float32 k_increment = 2.0f * b2_pi / k_segments;
@@ -77,8 +101,8 @@
for (int32 i = 0; i < k_segments; ++i)
{
b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
- glVertices[i*2]=v.x;
- glVertices[i*2+1]=v.y;
+ glVertices[i*2]=v.x * mRatio;
+ glVertices[i*2+1]=v.y * mRatio;
theta += k_increment;
}
@@ -96,7 +120,8 @@
{
glColor4f(color.r, color.g, color.b,1);
GLfloat glVertices[] = {
- p1.x,p1.y,p2.x,p2.y
+ p1.x * mRatio, p1.y * mRatio,
+ p2.x * mRatio, p2.y * mRatio
};
glVertexPointer(2, GL_FLOAT, 0, glVertices);
glDrawArrays(GL_LINES, 0, 2);
@@ -119,7 +144,7 @@
glColor4f(color.r, color.g, color.b,1);
glPointSize(size);
GLfloat glVertices[] = {
- p.x,p.y
+ p.x * mRatio, p.y * mRatio
};
glVertexPointer(2, GL_FLOAT, 0, glVertices);
glDrawArrays(GL_POINTS, 0, 1);
@@ -129,19 +154,19 @@
void GLESDebugDraw::DrawString(int x, int y, const char *string, ...)
{
+ //printf(string);
/* Unsupported as yet. Could replace with bitmap font renderer at a later date */
}
void GLESDebugDraw::DrawAABB(b2AABB* aabb, const b2Color& c)
{
-
glColor4f(c.r, c.g, c.b,1);
GLfloat glVertices[] = {
- aabb->lowerBound.x, aabb->lowerBound.y,
- aabb->upperBound.x, aabb->lowerBound.y,
- aabb->upperBound.x, aabb->upperBound.y,
- aabb->lowerBound.x, aabb->upperBound.y
+ 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
};
glVertexPointer(2, GL_FLOAT, 0, glVertices);
glDrawArrays(GL_LINE_LOOP, 0, 8);
Index: GLES-Render.h
===================================================================
--- GLES-Render.h (revision 1263)
+++ GLES-Render.h (working copy)
@@ -34,7 +34,12 @@
// inside b2World::Step.
class GLESDebugDraw : public b2DebugDraw
{
+ float32 mRatio;
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);
===================================================================
--- GLES-Render.mm (revision 1263)
+++ GLES-Render.mm (working copy)
@@ -26,15 +26,37 @@
#include <cstring>
-void GLESDebugDraw::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
+GLESDebugDraw::GLESDebugDraw()
+ : mRatio( 1.0f )
{
+}
+GLESDebugDraw::GLESDebugDraw( float32 ratio )
+ : mRatio( ratio )
+{
+}
+
+
+void GLESDebugDraw::DrawPolygon(const b2Vec2* old_vertices, int32 vertexCount, const b2Color& color)
+{
+ b2Vec2 vertices[vertexCount];
+ for( int i=0;i<vertexCount;i++) {
+ vertices[i] = old_vertices[i];
+ vertices[i] *= mRatio;
+ }
+
glColor4f(color.r, color.g, color.b,1);
glVertexPointer(2, GL_FLOAT, 0, vertices);
- glDrawArrays(GL_LINE_LOOP, 0, vertexCount);
+ glDrawArrays(GL_LINE_LOOP, 0, vertexCount);
}
-void GLESDebugDraw::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
+void GLESDebugDraw::DrawSolidPolygon(const b2Vec2* old_vertices, int32 vertexCount, const b2Color& color)
{
+ b2Vec2 vertices[vertexCount];
+ for( int i=0;i<vertexCount;i++) {
+ vertices[i] = old_vertices[i];
+ vertices[i] *= mRatio;
+ }
+
glVertexPointer(2, GL_FLOAT, 0, vertices);
glColor4f(color.r, color.g, color.b,0.5f);
@@ -46,6 +68,7 @@
void GLESDebugDraw::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
{
+
const float32 k_segments = 16.0f;
int vertexCount=16;
const float32 k_increment = 2.0f * b2_pi / k_segments;
@@ -55,8 +78,8 @@
for (int32 i = 0; i < k_segments; ++i)
{
b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
- glVertices[i*2]=v.x;
- glVertices[i*2+1]=v.y;
+ glVertices[i*2]=v.x * mRatio;
+ glVertices[i*2+1]=v.y * mRatio;
theta += k_increment;
}
@@ -68,6 +91,7 @@
void GLESDebugDraw::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
{
+
const float32 k_segments = 16.0f;
int vertexCount=16;
const float32 k_increment = 2.0f * b2_pi / k_segments;
@@ -77,8 +101,8 @@
for (int32 i = 0; i < k_segments; ++i)
{
b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
- glVertices[i*2]=v.x;
- glVertices[i*2+1]=v.y;
+ glVertices[i*2]=v.x * mRatio;
+ glVertices[i*2+1]=v.y * mRatio;
theta += k_increment;
}
@@ -96,7 +120,8 @@
{
glColor4f(color.r, color.g, color.b,1);
GLfloat glVertices[] = {
- p1.x,p1.y,p2.x,p2.y
+ p1.x * mRatio, p1.y * mRatio,
+ p2.x * mRatio, p2.y * mRatio
};
glVertexPointer(2, GL_FLOAT, 0, glVertices);
glDrawArrays(GL_LINES, 0, 2);
@@ -119,7 +144,7 @@
glColor4f(color.r, color.g, color.b,1);
glPointSize(size);
GLfloat glVertices[] = {
- p.x,p.y
+ p.x * mRatio, p.y * mRatio
};
glVertexPointer(2, GL_FLOAT, 0, glVertices);
glDrawArrays(GL_POINTS, 0, 1);
@@ -129,19 +154,19 @@
void GLESDebugDraw::DrawString(int x, int y, const char *string, ...)
{
+ //printf(string);
/* Unsupported as yet. Could replace with bitmap font renderer at a later date */
}
void GLESDebugDraw::DrawAABB(b2AABB* aabb, const b2Color& c)
{
-
glColor4f(c.r, c.g, c.b,1);
GLfloat glVertices[] = {
- aabb->lowerBound.x, aabb->lowerBound.y,
- aabb->upperBound.x, aabb->lowerBound.y,
- aabb->upperBound.x, aabb->upperBound.y,
- aabb->lowerBound.x, aabb->upperBound.y
+ 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
};
glVertexPointer(2, GL_FLOAT, 0, glVertices);
glDrawArrays(GL_LINE_LOOP, 0, 8);
Index: GLES-Render.h
===================================================================
--- GLES-Render.h (revision 1263)
+++ GLES-Render.h (working copy)
@@ -34,7 +34,12 @@
// inside b2World::Step.
class GLESDebugDraw : public b2DebugDraw
{
+ float32 mRatio;
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);