DebugDraw without Cocos2D, nothing shows

Discuss issues specific to using Box2D on the iPhone
Reactivo
Posts: 3
Joined: Sun Jun 23, 2013 3:28 pm

DebugDraw without Cocos2D, nothing shows

Postby Reactivo » Sun Jun 23, 2013 3:58 pm

Hi,

First of all thank you for this amazing library. I'm starting to learn it's basics as well as taking my first steps with Xcode and iPhone development, so excuse me if my questions looks silly.

Everything was going great with the Testbed, but now I'm trying to create a project in XCode 4.6.2 for OpenGL ES 2.0 with Box2D 2.2.1 library. I was able to add the files and start with a basic setup that gives me no errors, but i can't see anything on screen although I struggled for hours with the Debug Draw settings and classes.

I managed to get some GLESRender files updated to work with the last version of Box2D (or at least it gives no errors in console), but still no luck. I don't want to use Cocos2D or any framework for now, and understanding OpenGL ES 2.0 seems pretty hard, so I don't know if I'm missing something here.

This is my ViewController.mm file:

Code: Select all

#import "ViewController.h"

GLESDebugDraw debugDraw;

@interface ViewController ()
@end

@implementation ViewController

@synthesize context = _context;
@synthesize effect = _effect;

// OpenGL ES 2.0 context config
- (void)setupGL
{
   
    [EAGLContext setCurrentContext:self.context];
   
    self.effect = [[GLKBaseEffect alloc] init];
   
    glViewport(0, 0, 320, 480);
    glOrthof(-16, 16, -8, 40, -1, 1);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    // OpenGL ES 2.0 context
    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
   
    if (!self.context) {
        NSLog(@"Failed to create ES context");
    }
   
    [EAGLContext setCurrentContext:self.context];
    [self setupGL];
   
    // Initialize view
    GLKView *view = (GLKView *)self.view;
    view.context = self.context;
   
    self.preferredFramesPerSecond = 60;
   
    // Box2D World
   
    b2Vec2 gravity(0, 0);
    b2dWorld = new b2World(gravity);
   
    // Debug draw
    debugDraw.SetFlags(b2Draw::e_shapeBit);
    b2dWorld->SetDebugDraw( &debugDraw );
   
    //body definition
    b2BodyDef myBodyDef;
    myBodyDef.type = b2_dynamicBody;
   
    //shape definition
    b2PolygonShape polygonShape;
    polygonShape.SetAsBox(1, 1); //a 2x2 rectangle
   
    //fixture definition
    b2FixtureDef myFixtureDef;
    myFixtureDef.shape = &polygonShape;
    myFixtureDef.density = 1;
   
    //create dynamic bodies
    for (int i = 0; i < 10; i++)
    {
        myBodyDef.position.Set(0, 10);
        b2dWorld->CreateBody(&myBodyDef)->CreateFixture(&myFixtureDef);
    }
   
    //a static body
    myBodyDef.type = b2_staticBody;
    myBodyDef.position.Set(0, 0);
    b2Body* staticBody = b2dWorld->CreateBody(&myBodyDef);
   
    //add a fixture to the static body
    polygonShape.SetAsBox( 10, 1, b2Vec2(0, 0), 0);
    staticBody->CreateFixture(&myFixtureDef);

}

- (void)viewDidUnload
{
    [super viewDidUnload];
 
    if ([EAGLContext currentContext] == self.context) {
        [EAGLContext setCurrentContext:nil];
    }
    self.context = nil;
}


- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    // Clear screen each time
    glClear(GL_COLOR_BUFFER_BIT);
   
    // show FPS in debug
    #ifdef DEBUG
        static int framecount = 0;
        framecount ++;
        if (framecount > 30)
        {
            float ft = self.timeSinceLastDraw;
            NSString * debugText = [NSString stringWithFormat:@"%2.1f, %0.3f", 1.0/ft, ft];
            [self.debugLabel setText:debugText];
            framecount = 0;
        }
    #endif
 
    // Step properties
    float32 timeStep = 1/60.0;      //the length of time passed to simulate (seconds)
    int32 velocityIterations = 8;   //how strongly to correct velocity
    int32 positionIterations = 3;   //how strongly to correct position
   
    b2dWorld->Step( timeStep, velocityIterations, positionIterations);   
   
    b2dWorld->DrawDebugData();
}

- (void)update
{
   
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

@end


This is GLES-Render.h

Code: Select all

#ifndef RENDER_H
#define RENDER_H

#import <UIKit/UIKit.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>

#include <Box2D/Box2D.h>

struct b2AABB;

// This class implements debug drawing callbacks that are invoked
// inside b2World::Step.
class GLESDebugDraw : public b2Draw
{
public:
   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);
};


#endif


And this is the GLES-Render.mm, supposedly updated to work with Box2D 2.2.1

Code: Select all

#include "GLES-Render.h"

#include <cstdio>
#include <cstdarg>
#include <cstring>

void GLESDebugDraw::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
   glColor4f(color.r, color.g, color.b,1);
   glVertexPointer(2, GL_FLOAT, 0, vertices);
   glDrawArrays(GL_LINE_LOOP, 0, vertexCount);
}

void GLESDebugDraw::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
   glVertexPointer(2, GL_FLOAT, 0, vertices);
   
   glColor4f(color.r, color.g, color.b,0.5f);
   glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount);
   
   glColor4f(color.r, color.g, color.b,1);
   glDrawArrays(GL_LINE_LOOP, 0, vertexCount);
}

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;
   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;
      glVertices[i*2+1]=v.y;
      theta += k_increment;
   }
   
   glColor4f(color.r, color.g, color.b,1);
   glVertexPointer(2, GL_FLOAT, 0, glVertices);
   
   glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount);
}

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;
   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;
      glVertices[i*2+1]=v.y;
      theta += k_increment;
   }
   
   glColor4f(color.r, color.g, color.b,0.5f);
   glVertexPointer(2, GL_FLOAT, 0, glVertices);
   glDrawArrays(GL_TRIANGLE_FAN, 0, vertexCount);
   glColor4f(color.r, color.g, color.b,1);
   glDrawArrays(GL_LINE_LOOP, 0, vertexCount);
   
   // Draw the axis line
   DrawSegment(center,center+radius*axis,color);
}

void GLESDebugDraw::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
{
   glColor4f(color.r, color.g, color.b,1);
   GLfloat            glVertices[] = {
      p1.x,p1.y,p2.x,p2.y
   };
   glVertexPointer(2, GL_FLOAT, 0, glVertices);
   glDrawArrays(GL_LINES, 0, 2);
}

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)
{
   glColor4f(color.r, color.g, color.b,1);
   glPointSize(size);
   GLfloat glVertices[] = {
      p.x,p.y
   };
   glVertexPointer(2, GL_FLOAT, 0, glVertices);
   glDrawArrays(GL_POINTS, 0, 1);
   glPointSize(1.0f);
}

void GLESDebugDraw::DrawString(int x, int y, const char *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
   };
   glVertexPointer(2, GL_FLOAT, 0, glVertices);
   glDrawArrays(GL_LINE_LOOP, 0, 8);
   
}


As I said before it gives no errors of any kind but don't show anything on screen although If I trace some properties, it seems they are there. Maybe you can see where the error is or point me in the right direction, because I don't know where else to look. Everything seems about integration with Cocos2D.

Thank you.

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: DebugDraw without Cocos2D, nothing shows

Postby irresistible force » Sun Jun 23, 2013 4:42 pm

Try setting the viewport and glOrtho every frame, before the DrawDebugData.

Reactivo
Posts: 3
Joined: Sun Jun 23, 2013 3:28 pm

Re: DebugDraw without Cocos2D, nothing shows

Postby Reactivo » Mon Jun 24, 2013 3:58 am

No luck, still showing an empty screen :(

Reactivo
Posts: 3
Joined: Sun Jun 23, 2013 3:28 pm

Re: DebugDraw without Cocos2D, nothing shows

Postby Reactivo » Mon Jun 24, 2013 4:09 am

Maybe it can be related to this in GLES-render.h?

Code: Select all

#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>


Seems like I'm importing OpenGL ES 1 files. If I change ES1 for ES2, it gives me lots of errors in GLES-Render.mm in things like "glColor4f" wich I see aren't supported in Open GL ES 2. I think the drawing code is not really adapted to the 2.0 version...


Return to “iPhone”



Who is online

Users browsing this forum: No registered users and 1 guest