PDA

View Full Version : Using textures with box2D



kcazyz
04-14-2009, 07:57 PM
Ok I know this relates to Box2D, but I asked there, and they said it has to do more with textures, and GLUT, so here I am :p.

Anyways, it really doesn't have much to do with Box2D, just how it's set up. My fist plan was to just try and get a texture on the screen. I got this to work. Here are my glut callback functions:


void display(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();

glTranslatef (0, 0, -20);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 1);

glBegin(GL_QUADS);

glTexCoord2f(0.0f, 0.0f);
glVertex3f(-10.0f, -10.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(10.0f, -10.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(10.0f, 10.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-10.0f, 10.0f, 0.0f);

glEnd();

glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}

void reshape(int w, int h) {

if(h == 0)
h = 1;

float ratio = 1.0* w / h;

glViewport (0, 0, w, h);

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();

gluPerspective (90, ratio, 1, 1000);

glutPostRedisplay ();
}

Then in my main function:


loadTGA("texture.tga", 1);

This all works fine (the loadTGA function is elsewhere, but don't worry, it works fine).

Now in Box2D, in order to display the bodies correcty, my callback functions are:


void display(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

float32 timeStep = 1.0f / 30.0f;
int32 vIterations = 10;
int32 pIterations = 8;

world->Step(timeStep, vIterations, pIterations);

glutSwapBuffers();
}

void reshape(int w, int h) {

GLUI_Master.get_viewport_area(&tx, &ty, &tw, &th);
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float32 ratio = float32(tw) / float32(th);

b2Vec2 extents(ratio * 25.0f, 25.0f);
extents *= viewZoom;

b2Vec2 lower = viewCenter - extents;
b2Vec2 upper = viewCenter + extents;

// L/R/B/T
gluOrtho2D(lower.x, upper.x, lower.y, upper.y);
}

Some info is:
viewZoom = 1.0.
a b2Vec2 is a simple vector (x, y).

Anyway this works fine. So in order to combine the two, first I add the loadTGA command to my main function.
Then I change the display function to:


void display(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

float32 timeStep = 1.0f / 30.0f;
int32 vIterations = 10;
int32 pIterations = 8;

world->Step(timeStep, vIterations, pIterations);

glTranslatef (0, 0, -20);

glEnable (GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, 13);

/*glBegin (GL_QUADS);
glTexCoord2f (0.0f,0.0f);
glVertex3f (-10.0f, -10.0f, 0.0f);
glTexCoord2f (1.0f, 0.0f);
glVertex3f (10.0f, -10.0f, 0.0f);
glTexCoord2f (1.0f, 1.0f);
glVertex3f (10.0f, 10.0f, 0.0f);
glTexCoord2f (0.0f, 1.0f);
glVertex3f (-10.0f, 10.0f, 0.0f);
glEnd ();

glDisable (GL_TEXTURE_2D);

glutSwapBuffers();
}

Now for my reshape function, I have absolutely no idea what to do :p. So...what do I have to change in order for texture to appear on the screen?


This is important so I can then track the bodies with the texture, thus adding the texture to the body!

Thanks in advance,
Kcaz

kcazyz
04-15-2009, 11:09 AM
So, can anyone help?