OpenGL深度缓冲区问题

发布于 2024-10-10 16:12:24 字数 2000 浏览 0 评论 0原文

在我最近的几个项目中,我一直在使用一些在查看一些演示时发现的实用程序文件 在这里

即一个名为 opengl.h 的文件 - 主要用于管理着色器,有点像 glew 和另一个文件 gl_font。

gl_font 是他们用来使用顶点缓冲区对象在屏幕上渲染字体的类。

但是,当我使用它来渲染游戏中的帧速率时,它会正确绘制除天空盒之外的所有内容。由于某种原因,天空盒呈现白色,如此处所示,如果我不渲染它看起来的字体就像这个

以下是我认为最重要的 gl_font 类的一些部分:

void GLFont::begin()
{
    HWND hWnd = GetForegroundWindow();
    RECT rcClient;

    GetClientRect(hWnd, &rcClient);

    int w = rcClient.right - rcClient.left;
    int h = rcClient.bottom - rcClient.top;

    glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT);

    glDisable(GL_LIGHTING);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, m_fontTexture);

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0.0f, w, h, 0.0f, -1.0f, 1.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);

    drawTextBegin();
}

我尝试将 glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT); 更改为 glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT);背景纹理返回,但字体未渲染。

void GLFont::end()
{
    drawTextEnd();

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindTexture(GL_TEXTURE_2D, 0);
    glDisable(GL_TEXTURE_2D);

    glDisable(GL_BLEND);

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();

    glPopAttrib();
}

这是渲染字体时深度缓冲区的图像,就是实际情况的样子。

有人能解释一下这个问题吗?

任何帮助将不胜感激!

谢谢。

For my last few projects I have been using some of the utility files that I found whilst looking at a few demos here.

Namely a file called opengl.h - mainly used to manage shaders a bit like glew and another file gl_font.

gl_font is a class they use to render fonts on screen using vertex buffer objects.

However, when I use this to render the framerate in my game it draws everything but the skybox correctly. For some reason the skybox is rendered white as seen here, if I do not render the font it looks like this.

Here are some parts of the gl_font class that I think are most important:

void GLFont::begin()
{
    HWND hWnd = GetForegroundWindow();
    RECT rcClient;

    GetClientRect(hWnd, &rcClient);

    int w = rcClient.right - rcClient.left;
    int h = rcClient.bottom - rcClient.top;

    glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT);

    glDisable(GL_LIGHTING);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, m_fontTexture);

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0.0f, w, h, 0.0f, -1.0f, 1.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);

    drawTextBegin();
}

I have trie changing glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT); to glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT); and the background texture returns, but the font isn't rendered.

void GLFont::end()
{
    drawTextEnd();

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindTexture(GL_TEXTURE_2D, 0);
    glDisable(GL_TEXTURE_2D);

    glDisable(GL_BLEND);

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();

    glPopAttrib();
}

This is an image of the depth buffer when the font is rendered and this is what is looks like when it is not.

Could anyone shed some light on this problem please?

Any help would be much appreciated!

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清泪尽 2024-10-17 16:12:24

看起来 begin()glMatrixMode(GL_MODELVIEW) 之后缺少 glPushMatrix()。当某些文本也被渲染时,这可能会导致场景渲染不正确。

glGetError()不是报告了GL_STACK_UNDERFLOW错误吗?

Looks like begin() lacks a glPushMatrix() after glMatrixMode(GL_MODELVIEW). This might cause the scene to be rendered incorrectly when some text is also rendered.

Didn't glGetError() report a GL_STACK_UNDERFLOW error?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文