0
votes

I am stuck at Drawing Polygons as Box2D objects. With opengl, the drawing of a polygon is pretty simple i.e

glBegin(GL_POLYGON);

glColor3f(0,0,0);
glVertex2f(2.0,6.0);
glVertex2f(2.0,8.0);
glVertex2f(2.5,8.0);
glVertex2f(2.5,6.0);

glEnd();

And what I have seen in the tutorials, with box2D we do it like this:

void drawSquare(b2Vec2* points,b2Vec2 center,float angle)
{
  glColor3f(1,1,1);
glPushMatrix();
        glTranslatef(center.x*M2P,center.y*M2P,0);
        glRotatef(angle*180.0/3.141,0,0,1);

        glBegin(GL_QUADS);
                for(int i=0;i<4;i++)
                {

                        glVertex2f(points[i].x*M2P,points[i].y*M2P);


                }
        glEnd();
glPopMatrix();
}

And the display() code is as follows:

 void display()
 {
     glClear(GL_COLOR_BUFFER_BIT)   
     glEnable (GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,11);

//pix[0].mDraw();

glLoadIdentity();

b2Body* tmp=world->GetBodyList(); // Get no.of bodies

b2Vec2 points[4];

while(tmp)
{
        for(int i=0;i<4;i++)
        points[i]=((b2PolygonShape*)tmp->GetFixtureList()->GetShape())->GetVertex(i);

       drawSquare(points,tmp->GetWorldCenter(),tmp->GetAngle());

       tmp=tmp->GetNext();
}




glFlush();
}

I am stuck with this following code:

/*for(int i=0;i<4;i++)
points[i]=((b2PolygonShape*)tmp->GetFixtureList()->GetShape())->GetVertex(i);

       drawSquare(points,tmp->GetWorldCenter(),tmp->GetAngle());

       tmp=tmp->GetNext();*/

I dont really get what gets assigned into the points' array??

1
points is accumulating the vertices of the collision polygon, which are assumed to be squares. Those vertices are then passed to the drawing function to be handled to OpenGL for rendering. - Trillian
Got it! I have one more question! For texturing, we give textCords for every vertex. I tried to add the following command right after my glVertex2f() command in drawSquare() but I didn't get the desired result. I think this is not a correct approach for texture mapping. Can u spot whats wrong or suggest me the right way to map textures on my polygons 'glTexCoord2f(points[i].xM2P,points[i].yM2P);' - Vector

1 Answers

0
votes

points is accumulating the vertices of the collision polygon, which are assumed to be squares. Those vertices are then passed to the drawing function to be handled to OpenGL for rendering.

As for your second question in the comment, it makes no sense to use vertex coordinates as texture coordinates. Texture coordinates specify which pixels (or texels) in the texture should be used. The values are normalized such that (0,0) corresponds to the bottom-left of your image and (1,1) corresponds to the top-right of your image. Whether your object is at (0,0) or at (42,35) in your world does not change which pixels in the texture you want to use.

If you want to map the entirety of a texture to a quad, then you should pass (0,0), (0,1), (1,1) and (1,0) as the texture coordinates for each of your four vertices. You might have to play with the order of the coordinates if the texture appears rotated.