显示被倒置的图象的纹理

时间:2013-11-17 20:18:37

标签: opengl box2d glut texture-mapping texture2d

我有一个PolygonShape的物理实体。问题是当我在它上面应用纹理时它会显示倒像!我无法弄清楚为什么?我想我给的是正确的像素坐标,但它仍然不起作用。 这是opengl渲染:

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();
}

这就是我从Display函数

调用它的方式
  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逻辑:

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;
} 

这些是addRect的参数:

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

无法发现我哪里错了!

2 个答案:

答案 0 :(得分:1)

  

无法发现我哪里错了!

你什么都没做错。使用常规转换的OpenGL只是将图像的原点放在左下角而不是左上角。 BTW,DIB / .bmp文件支持订购。如果标题中的height字段为负值,则图片的原点位于左下方。

基本上可以归结为知道图像文件放置原点的位置,以及图像原点位于OpenGL端的位置。

完全有可能改变在OpenGL中处理图像的方式,将原点放在左上角。

答案 1 :(得分:0)

[问题已解决]

是多边形的顶点逆时针受伤了!并且交换坐标解决了问题,即顺序应为(0,1),(0,0),(1,0),(1,1)

相关问题