OpenGL trying to Draw Multiple 2d Textures, only 1st one appears

五迷三道 提交于 2019-12-02 00:05:57

gluOrtho2D() doesn't issue a glLoadIdentity() like you seem to expect.

Generally you only set your projection matrix once per frame.

Try something like this:

void DrawFrame()
{
    ...

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluOrtho2D(0, this.Width, 0, this.Height);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    // "camera" transform(s)

    foreach object
    {
        glPushMatrix();
        // per-object matrix transform(s)

        // draw object

        glPopMatrix();
    }
}

Just had to deal with this problem so I thought I'd chip in as well.
I saw the accepted answer and in my case, I did reset everything correctly in the pipeline. In my pipeline, I need to render both 3D and 2D objects. For 3D objects I would switch to 3D projection/model view and for 2D objects would switch to orthographic projection.

However, when I wanted to render a couple of text blocks (using quads and textures), only the first texture was rendered. Depth testing was the root of my problem. So I've modified the code above to work for my situation.

void DrawFrame()
{
    foreach object
    {
        drawObject();   // Is an abstract method
    }
}


void _3DObject::drawObject() {
    switchTo3D();

    // Camera transform

    glPushMatrix();
    .
    . // Draw
    .
    .glPopMatrix();
}

void _2DObject::drawObject() {
    switchTo2D();

    // Having depth testing enabled was causing
    // problems when rendering textured quads.
    glDisable(GL_DEPTH_TEST);

    // Camera transform

    glPushMatrix();
    .
    . // Draw
    .
    .glPopMatrix();

    glEnable(GL_DEPTH_TEST);
}

PS: "Just" refers to exactly 1 month ago! Don't know why I forgot to post this answer sooner :(

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!