无纹理的四边形显得较暗

发布于 2024-12-14 01:05:32 字数 1548 浏览 0 评论 0原文

我刚刚开始使用 OpenGL,但在实现字体系统后遇到了问题。

我的计划是简单地可视化几种寻路算法。

目前OpenGL的设置如下(OnSize在手动创建窗口时被调用一次):

void GLWindow::OnSize(GLsizei width, GLsizei height)
{
    // set size
    glViewport(0,0,width,height);

    // orthographic projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0,width,height,0.0,-1.0,1.0);
    glMatrixMode(GL_MODELVIEW);

    m_uiWidth = width;
    m_uiHeight = height;
}

void GLWindow::InitGL()
{
    // enable 2D texturing
    glEnable(GL_TEXTURE_2D);
    // choose a smooth shading model
    glShadeModel(GL_SMOOTH);
    // set the clear color to black
    glClearColor(0.0, 0.0, 0.0, 0.0);

    glEnable(GL_ALPHA_TEST);
    glAlphaFunc(GL_GREATER, 0.0f);  
}

理论上我不需要混合,因为我只会使用无纹理的Quads来可视化障碍物和线条等来绘制路径...所以一切都会是无纹理,除了字体...

字体类有一个推送和弹出功能,看起来像这样(如果我没记错的话,我的字体系统是基于我很久以前关注的 NeHe 教程):

inline void GLFont::pushScreenMatrix()
{
    glPushAttrib(GL_TRANSFORM_BIT);
    GLint   viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(viewport[0],viewport[2],viewport[1],viewport[3], -1.0, 1.0);
    glPopAttrib();
}

inline void GLFont::popProjectionMatrix()
{
    glPushAttrib(GL_TRANSFORM_BIT);
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glPopAttrib();
}

所以问题: 如果我不绘制文本,我可以看到我想要绘制的四边形,但它们很暗,所以我的常规 OpenGL 矩阵属性一定有问题。 如果我绘制文本(因此调用与字体相关的推送和弹出函数),我看不到任何四边形。

问题: 我如何解决这个问题以及为什么会发生这种情况的一些背景信息也很好,因为我仍然是一个刚刚开始的初学者/学生。

I just started working with OpenGL, but I ran into a problem after implementing a Font system.

My plan is to simply visualize several Pathfinding Algorithms.

Currently OpenGL gets set up like this (OnSize gets called once on window creation manually):

void GLWindow::OnSize(GLsizei width, GLsizei height)
{
    // set size
    glViewport(0,0,width,height);

    // orthographic projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0,width,height,0.0,-1.0,1.0);
    glMatrixMode(GL_MODELVIEW);

    m_uiWidth = width;
    m_uiHeight = height;
}

void GLWindow::InitGL()
{
    // enable 2D texturing
    glEnable(GL_TEXTURE_2D);
    // choose a smooth shading model
    glShadeModel(GL_SMOOTH);
    // set the clear color to black
    glClearColor(0.0, 0.0, 0.0, 0.0);

    glEnable(GL_ALPHA_TEST);
    glAlphaFunc(GL_GREATER, 0.0f);  
}

In theory I don't need blending, because I will only use untextured Quads to visualize obstacles and line etc to draw paths... So everything will be untextured, except the fonts...

The Font Class has a push and pop function, that look like this (if I remember right my Font system is based on a NeHe Tutorial that I was following quite a while ago):

inline void GLFont::pushScreenMatrix()
{
    glPushAttrib(GL_TRANSFORM_BIT);
    GLint   viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(viewport[0],viewport[2],viewport[1],viewport[3], -1.0, 1.0);
    glPopAttrib();
}

inline void GLFont::popProjectionMatrix()
{
    glPushAttrib(GL_TRANSFORM_BIT);
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glPopAttrib();
}

So the Problem:
If I don't draw a Text I can see the Quads I want to draw, but they are quite dark, so there must be something wrong with my general OpenGL Matrix Properties.
If I draw Text (so the font related push and pop functions get called) I can't see any Quads.

The question:
How do I solve this problem and some background information why this happened would also be nice, because I am still a beginner/student, who just started.

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

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

发布评论

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

评论(1

南风起 2024-12-21 01:05:32

如果您的四边形没有纹理,您将遇到未定义的行为。可能发生的情况是,将使用任何先前的纹理,并且将使用点 (0,0) 处的颜色,这可能是导致它们不可见的原因。

实际上,您需要在尝试使用 glDisable(GL_TEXTURE_2D) 绘制无纹理四边形之前禁用纹理。再说一次,如果你不这样做,它只会使用以前的纹理和纹理坐标,而不会看到你的 draw() 循环,我假设它是未定义的。

If your quads are untextured, you will run into undefined behaviour. What will probably happen is that any previous texture will be used, and the colour at point (0,0) will be used, which could be what is causing them to be invisible.

Really, you need to disable texturing before trying to draw untextured quads using glDisable(GL_TEXTURE_2D). Again, if you don't, it'll just use the previous texture and texture co-ordinates, which without seeing your draw() loop, I'm assuming to be undefined.

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