简体   繁体   English

OpenGL绘制2D超过3D不起作用

[英]Opengl drawing 2D over 3D not working

I'm trying to draw a HUD over my game in OpenGL; 我正在尝试在OpenGL游戏中绘制HUD; I have decided to make it using some textures and applying them on a 2D scene. 我决定使用一些纹理并将其应用于2D场景。 I'm using Java and LWJGL. 我正在使用Java和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. 当我运行它时,将正确渲染3D零件,但是没有我的纹理的迹象。 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. 您可以尝试通过循环调用glError()直到返回成功,来清除错误。 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! 那只是一个hack,永远不要在发布代码时这样做!

But you must also try simplifying your code. 但是,您还必须尝试简化代码。 Just call glDrawLine() to start. 只需调用glDrawLine()即可启动。 If that works, then draw a single untextured triangle. 如果可行,则绘制一个无纹理的三角形。 That will confirm your perspective and modleview are correct. 那将确认您的观点和modleview是正确的。 Only then enable texturing. 只有这样才能启用纹理。

You might have out-of-order calls. 您可能会打乱电话。 EG what matrix are you pushing for GL_MODELVIEW? 例如,您要为GL_MODELVIEW推动什么矩阵?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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