Search code examples
openglbox2dgluttexture-mappingtexture2d

Texture showing Inverted Image


I have a Physics body that is a PolygonShape. The problem is when I am applying texture on it it shows inverted image! I am unable to figure out why? I guess I am giving the right pixel coordinates but still it doesn't work. Here is the opengl rendering:

void drawSquare2(b2Vec2* points,b2Vec2 center,float angle) 
{

    glColor3f(1,1,1);
    glPushMatrix();

        pix.readBMPFile("marioStanding.bmp",1);
    pix.setChromaKey(255, 255, 255);        
    pix.setTexture(11);
            glTranslatef(center.x*M2P,center.y*M2P,0);
            glRotatef(angle*180.0/3.141,0,0,1);



    glEnable (GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,11);


    glBegin(GL_POLYGON);

    int i=0;
            glVertex2f(points[i].x*M2P,points[i].y*M2P);
    glTexCoord2f(0,0);

    i++;
             glVertex2f(points[i].x*M2P,points[i].y*M2P);
    glTexCoord2f(0,1);


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

        i++;
            glVertex2f(points[i].x*M2P,points[i].y*M2P);
        glTexCoord2f(1,0);



            glEnd();

    glDisable(GL_TEXTURE_2D);
    glPopMatrix();
}

And this is how I am calling it from my Display Function

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

  drawSquare2(points,tom_char->GetWorldCenter(),tom_char->GetAngle());

addRect Logic:

b2Body* addRect2(int x,int y,int w,int h,bool dyn=true)
{
    b2BodyDef bodydef;  
    bodydef.position.Set(x*P2M,y*P2M);   //Setting body position
    if(dyn)
    {
            bodydef.type=b2_dynamicBody;  // dynamic body means body will move

    }

    b2Body* body=world->CreateBody(&bodydef);        //Creating box2D body

    b2PolygonShape shape;            //Creating shape object
    shape.SetAsBox(P2M*w,P2M*h);

    ////////////// Adding Fixtures(mass, density etc) //////////////

    b2FixtureDef fixturedef;
    fixturedef.shape=&shape;
    fixturedef.density=0.0;
    fixturedef.restitution = 0.7;
    body->CreateFixture(&fixturedef);
    return body;
} 

and these are the parameters given to addRect:

tom_char=addRect(500,460,50,70,true);

Cannot spot where am I going wrong!


Solution

  • [PROBLEM SOLVED]

    Yes the vertices of the polygon are wounded counterclockwise! And swapping the coordinates solved the problem i.e the order should be (0,1),(0,0),(1,0),(1,1)