hi, I've been having trouble with the hello world example and combining it with glut. ive managed to set up all of the world and objects but they wont run within the main glut render loop. I know exactly what is wrong, but im not sure how to create the world in such a way that it is global. if anybody has and advice on how to create the world object, It would be great if you could tell me, or any other hints about how to get box2d working with glut.
here is my code...
here is my code...
Code:
#include <stdlib.h>
#include <stdio.h>
#include<GL/glut.h>
#include "Box2D.h"
#include <cstdio>
b2AABB worldAABB;
bool doSleep;
b2BodyDef groundBodyDef;
b2Body* groundBody;
b2PolygonDef groundShapeDef;
b2BodyDef bodyDef;
b2PolygonDef shapeDef;
float32 timeStep = 1.0f / 60.0f;
int32 iterations = 10;
b2Body* body;
void changeSize(int w, int h) {
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
float ratio = 1.0* w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0,0.0,0.0,-1.0, 0.0f,1.0f,0.0f);
}
void renderScene(void)
{
//angle+=anglePlus;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
//glRotatef(angle,0.0f,1.0f,0.0f);
//draw here
glBegin(GL_POINTS);
// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
world.Step(timeStep, iterations);
// Now print the position and angle of the body.
b2Vec2 position = body->GetPosition();
float32 angle = body->GetAngle();
printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
glVertex3f( position.x, position.y,0.0f);
glEnd();
//end draw
glPopMatrix();
glutSwapBuffers();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA);
glutInitWindowPosition(1000,1000);
glutInitWindowSize(800,500);
glutCreateWindow("ShockAndAwe");
glutFullScreen();
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
glEnable(GL_DEPTH_TEST);
printf("%s \n", "A string");
/*B2_NOT_USED(argc);
B2_NOT_USED(argv);*/
worldAABB.lowerBound.Set(-100.0f, -100.0f);
worldAABB.upperBound.Set(100.0f, 100.0f);
b2Vec2 gravity(0.0f, -10.0f);
doSleep = true;
b2World world(worldAABB, gravity, doSleep);
groundBodyDef.position.Set(0.0f, -10.0f);
b2Body* groundBody = world.CreateBody(&groundBodyDef);
groundShapeDef.SetAsBox(50.0f, 10.0f);
groundBody->CreateShape(&groundShapeDef);
bodyDef.position.Set(0.0f, 4.0f);
body= world.CreateBody(&bodyDef);
shapeDef.SetAsBox(1.0f, 1.0f);
shapeDef.density = 1.0f;
shapeDef.friction = 0.3f;
body->CreateShape(&shapeDef);
body->SetMassFromShapes();
glutMainLoop();
}
#include <stdio.h>
#include<GL/glut.h>
#include "Box2D.h"
#include <cstdio>
b2AABB worldAABB;
bool doSleep;
b2BodyDef groundBodyDef;
b2Body* groundBody;
b2PolygonDef groundShapeDef;
b2BodyDef bodyDef;
b2PolygonDef shapeDef;
float32 timeStep = 1.0f / 60.0f;
int32 iterations = 10;
b2Body* body;
void changeSize(int w, int h) {
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
float ratio = 1.0* w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0,0.0,0.0,-1.0, 0.0f,1.0f,0.0f);
}
void renderScene(void)
{
//angle+=anglePlus;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
//glRotatef(angle,0.0f,1.0f,0.0f);
//draw here
glBegin(GL_POINTS);
// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
world.Step(timeStep, iterations);
// Now print the position and angle of the body.
b2Vec2 position = body->GetPosition();
float32 angle = body->GetAngle();
printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
glVertex3f( position.x, position.y,0.0f);
glEnd();
//end draw
glPopMatrix();
glutSwapBuffers();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA);
glutInitWindowPosition(1000,1000);
glutInitWindowSize(800,500);
glutCreateWindow("ShockAndAwe");
glutFullScreen();
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
glEnable(GL_DEPTH_TEST);
printf("%s \n", "A string");
/*B2_NOT_USED(argc);
B2_NOT_USED(argv);*/
worldAABB.lowerBound.Set(-100.0f, -100.0f);
worldAABB.upperBound.Set(100.0f, 100.0f);
b2Vec2 gravity(0.0f, -10.0f);
doSleep = true;
b2World world(worldAABB, gravity, doSleep);
groundBodyDef.position.Set(0.0f, -10.0f);
b2Body* groundBody = world.CreateBody(&groundBodyDef);
groundShapeDef.SetAsBox(50.0f, 10.0f);
groundBody->CreateShape(&groundShapeDef);
bodyDef.position.Set(0.0f, 4.0f);
body= world.CreateBody(&bodyDef);
shapeDef.SetAsBox(1.0f, 1.0f);
shapeDef.density = 1.0f;
shapeDef.friction = 0.3f;
body->CreateShape(&shapeDef);
body->SetMassFromShapes();
glutMainLoop();
}