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

前端 未结 2 392
名媛妹妹
名媛妹妹 2021-01-20 18:45

I\'ve got a problem in that I\'m trying to draw a solar system using textures (one texture for each planet) and as I draw my textures, only the 1st one appears. None of the

相关标签:
2条回答
  • 2021-01-20 19:17

    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();
        }
    }
    
    0 讨论(0)
  • 2021-01-20 19:29

    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 :(

    0 讨论(0)
提交回复
热议问题