简体   繁体   中英

Opengl drawing 2D over 3D not working

I'm trying to draw a HUD over my game in OpenGL; I have decided to make it using some textures and applying them on a 2D scene. I'm using Java and LWJGL. This is the code:

public void drawHUD1()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 800, 0, 600, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_LIGHTING);
    glPushMatrix();
    glClear(GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, arrows[0]);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f,0.0f);
    glVertex2f(60f,60f);
    glTexCoord2f(1.0f,0.0f);
    glVertex2f(90f,60f);
    glTexCoord2f(1.0f,1.0f);
    glVertex2f(90f,90f);
    glTexCoord2f(0.0f,1.0f);
    glVertex2f(60f,90f);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    glPopMatrix();
    glViewport(0, 0, width, height); 
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    float aspect = (float) width / height;
    GLU.gluPerspective(45.0f, aspect, 0.1f, 1000); 
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
}

When I run it, the 3D part is correctly rendered, but there are no signs of my texture. I remembered to call the method, so that is not a possible answer; also the texture is correctly loaded, I've checked it. Can anybody help me?

If you see an "impossible" error on the first function call, it may have been set by a previous call elsewhere - even in library code.

You could try clearing the error by calling glError() in a loop until it returns success. Then you will have a clean slate for checking every function you call. That is just a hack, don't ever do that in shipping code!

But you must also try simplifying your code. Just call glDrawLine() to start. If that works, then draw a single untextured triangle. That will confirm your perspective and modleview are correct. Only then enable texturing.

You might have out-of-order calls. EG what matrix are you pushing for GL_MODELVIEW?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM