使用OpenGL,如何正确使用gluOrtho2D默认投影? [英] With OpenGL, how can I use gluOrtho2D properly with default projection?

查看:1252
本文介绍了使用OpenGL,如何正确使用gluOrtho2D默认投影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用gluOrtho2D与glutBitmapCharacter,使我可以在屏幕上渲染文本以​​及我的3D对象。然而,当我使用glOrtho2D,我的3D对象消失;我假设这是因为我不设置投影回到OpenGL / GLUT默认,但我不是很确定是什么。

I'm trying to use gluOrtho2D with glutBitmapCharacter so that I can render text on the screen as well as my 3D objects. However, when I use glOrtho2D, my 3D objects dissapear; I assume this is because I'm not setting the projection back to the OpenGL/GLUT default, but I'm not really sure what that is.

无论如何,这是我用来渲染文本的函数:

Anyway, this is the function I'm using to render text:

void GlutApplication::RenderString(Point2f point, void* font, string s)
{
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(0.0, WindowWidth, 0.0, WindowHeight);

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    glDisable(GL_TEXTURE);
    glDisable(GL_TEXTURE_2D);

    glRasterPos2f(point.X, point.Y);
    for (string::iterator i = s.begin(); i != s.end(); ++i)
    {
    	glutBitmapCharacter(font, *i);
    }

    glEnable(GL_TEXTURE);
    glEnable(GL_TEXTURE_2D);

    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
}

并且,render函数类似于:

And, the render function is similar to this:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glLoadIdentity();

// Do some translation here.

// Draw some 3D objects.

glPopMatrix();

// For some reason, this stops the above from being rendered,
// where the camera is facing (I assume they are still being rendered).
Point2f statusPoint(10, 10);
RenderString(statusPoint, GLUT_BITMAP_9_BY_15, "Loading...");


推荐答案

我建议你检查你是否忘了一个 glPopMatrix 某处。为此,您可以通过 glGet(GL_MODELVIEW_STACK_DEPTH)获取堆栈深度。

I suggest that you check if you forgot a glPopMatrix somewhere. To do so you can get the stack depth via glGet(GL_MODELVIEW_STACK_DEPTH). Getters for the other matrix-stacks are available as well.

此外,您还可以查看当前矩阵。调用 glGetFloatv(GL_MODELVIEW_MATRIX,Pointer_To_Some_Floats)得到它。每次设置模型视图或投影矩阵时,都可以打印出浮点数。这样,你应该能够找出哪个矩阵不规则地作为当前矩阵。

Also you can take a look at the current matrix. Call glGetFloatv(GL_MODELVIEW_MATRIX, Pointer_To_Some_Floats) to get it. You can print out the floats each time you've set up a modelview or projection matrix. That way you should be able to to find out which matrix erratically ends up as the current matrix.

这应该给你足够的线索来找到这个bug。

That should give you enough clues to find the bug.

这篇关于使用OpenGL,如何正确使用gluOrtho2D默认投影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆