OpenGL 2D HUD没有显示

时间:2016-04-14 18:37:29

标签: c++ opengl

所以我有一个3D openGL应用程序,我想要显示2D HUD,我理解了经过大量谷歌搜索之后如何做到这一点的基本概念,但我尝试的任何东西都没有显示出来。在讨论了许多其他示例后,我目前的代码是:

    // HUD ///////////////////////////////////

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, (GLdouble)WIDTH, (GLdouble)HEIGHT, 0, -1, 1);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

    glColor3f(1, 1, 1);
    glPushMatrix();
    glBegin(GL_QUADS);
    glVertex3f(-5.0f, 5.0f, 0.0f);
    glVertex3f(-5.0f, -5.0f, 0.0f);
    glVertex3f(5.0f, -5.0f, 0.0f);
    glVertex3f(5.0f, 5.0f, 0.0f);
    glEnd();
    glPopMatrix();
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);

现在没有任何视觉上的变化,我可以让整个屏幕清晰地显示颜色,但即使这样我也似乎无法在其上绘制四边形。

编辑:

glDisable(GL_CULL_FACE);
    glDisable(GL_DEPTH_TEST);
    glDepthMask(GL_FALSE);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, WIDTH, HEIGHT, 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClear(GL_DEPTH_BUFFER_BIT);

    glColor3f(1, 1, 1);
    glPushMatrix();
    glBegin(GL_QUADS);
    glVertex3f(-10.0f, 10.0f, 0.0f);
    glVertex3f(-10.0f, -10.0f, 0.0f);
    glVertex3f(10.0f, -10.0f, 0.0f);
    glVertex3f(10.0f, 10.0f, 0.0f);
    glEnd();
    glPopMatrix();

    glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);

编辑2:SKYBOX CODE

glDisable(GL_CULL_FACE);
    glDepthFunc(GL_LEQUAL);  // Change depth function so depth test passes when values are equal to depth buffer's content
    skyShader.Bind();
    view = glm::mat4(glm::mat3(camera.GetViewProjection()));    // Remove any translation component of the view matrix
    glUniformMatrix4fv(glGetUniformLocation(skyShader.getProg(), "view"), 1, GL_FALSE, glm::value_ptr(view));
    glUniformMatrix4fv(glGetUniformLocation(skyShader.getProg(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
    // SKYBOX CUBE ///////////////////////////////// 
    glBindVertexArray(skyboxVAO);
    glActiveTexture(GL_TEXTURE0);
    glUniform1i(glGetUniformLocation(skyShader.getProg(), "skybox"), 0);
    glBindTexture(GL_TEXTURE_CUBE_MAP, skyboxTexture);
    glDrawArrays(GL_TRIANGLES, 0, 36);
    glBindVertexArray(0);
    glDepthFunc(GL_LESS); // Set depth function back to default

1 个答案:

答案 0 :(得分:3)

我不确定HEIGHTWIDTH的设置是什么,所以我只使用了100。当我运行您发布的代码时,我可以在屏幕的左上角看到您的四边形。

在你掌握了正在发生的事情之前,我建议你简单地使用你的HUD代码。

例如:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();
glBegin(GL_QUADS);
glVertex3f(-0.5f,  0.5f, 0f);
glVertex3f(-0.5f, -0.5f, 0f);
glVertex3f( 0.5f, -0.5f, 0f);
glVertex3f( 0.5f,  0.5f, 0f);
glEnd();
glPopMatrix();

鉴于你的GL状态的其余部分没有改变,那应该绘制一个填充屏幕50%的白色四边形,位于中心位置:

default view matrix

这是因为视图默认为glOrtho调用,如下所示:

glOrtho(-1f, 1f, -1f, 1f, -1f, 1f);

现在让我们将WIDTHHEIGHT设置为10f。这就是您的glOrtho电话:

glOrtho(0f, 10f, 10f, 0f, -1f, 1f);

这是呈现的内容:

modified view matrix

如果您的HEIGHTWIDTH更大,比如800x600,那么四边形会很小甚至不会出现。

编辑:使用glPushMatrixglPopMatrix隔离对模型视图和投影矩阵的修改

阅读完您的编辑和评论后,让我添加一些示例代码,我相信这些代码可以说明您的需求。它没有处理深度缓冲区,这可能是导致问题的原因,但我希望保持简单并以之前的方式构建:

int width = 500;   // window width
int height = 500;  // window height

// define our GL viewport
glViewport(0, 0, width, height);

// setup 3d projection
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glPerspective(60f, width/height, 1f, 50f);

// setup 3d modelview
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

// move the camera to put the tetrahedron behind the UI element
glRotatef(135f, 0f, 0f, 1f);
glTranslatef(0f, 5f, -30f);

// draw 3d object (tetrahedron)
glBegin(GL_TRIANGLES);
// triangle 1
glColor3f(1f, 0f, 0f);          // red
glVertex3f(0f, 10f, 0f);        // a
glColor3f(1f, 1f, 0f);          // yellow
glVertex3f(0f, -10f, -10f);     // b
glColor3f(0f, 1f, 0f);          // green
glVertex3f(-10f, -10f, 10f);    // c
// triangle 2
glColor3f(1f, 0f, 0f);          // red
glVertex3f(0f, 10f, 0f);        // a
glColor3f(1f, 0f, 1f);          // purple
glVertex3f(10f, -10f, 10f);     // d
glColor3f(1f, 1f, 0f);          // yellow
glVertex3f(0f, -10f, -10f);     // b
// triangle 3
glColor3f(1f, 0f, 0f);          // red
glVertex3f(0f, 10f, 0f);        // a
glColor3f(0f, 1f, 0f);          // green
glVertex3f(-10f, -10f, 10f);    // c
glColor3f(1f, 0f, 1f);          // purple
glVertex3f(10f, -10f, 10f);     // d
// triangle 4
glColor3f(1f, 1f, 0f);          // yellow
glVertex3f(0f, -10f, -10f);     // b
glColor3f(1f, 0f, 1f);          // purple
glVertex3f(10f, -10f, 10f);     // d
glColor3f(0f, 1f, 0f);          // green
glVertex3f(-10f, -10f, 10f);    // c
glEnd();

// revert to original modelview matrix
glPopMatrix();
// revert to original projection matrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();

// setup 2d projection
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0f, 1f, 1f, 0f, -1f, 1f);

// setup 2d modelview
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

// draw the 2d interface
glBegin(GL_QUADS);
glColor3f(0.5f, 0.5f, 0.5f);    // grey
glVertex3f(0.05f, 0.05f, 0f);   // top-left
glVertex3f(0.95f, 0.05f, 0f);   // top-right
glVertex3f(0.95f, 0.2f, 0f);    // bottom-right
glVertex3f(0.05f, 0.2f, 0f);    // bottom-left
glEnd();

// revert to original modelview matrix
glPopMatrix();
// revert to original projection matrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();

这将呈现以下内容:

interface over tetrahedron

当我们为2d界面设置ModelView矩阵时,我们会注意到我们使用它来定义视锥体:

glOrtho(0f, 1f, 1f, 0f, -1f, 1f);

这将我们的视图空间定义为左上角为0,0,右下角为1,1。这允许我们将内容放在屏幕上而不考虑视口大小。当我们绘制我们的2d界面时,我们只需要提供0.0到1.0范围内的值,它将适合视口。

重要: 你应该永远记住模型 - 视图和投影矩阵是堆栈。在对它们进行任何修改之前,请记住在堆栈上推送一个新层,然后在完成后将其弹回。通过这样做,您可以隔离您的更改。