我試圖用gluOrtho2D與glutBitmapCharacter,以便我可以在屏幕上呈現文本以及我的3D對象。但是,當我使用glOrtho2D時,我的3D對象消失了;我認爲這是因爲我沒有將投影設置回OpenGL/GLUT默認值,但我不確定那是什麼。使用OpenGL,我怎樣才能在默認投影中正確使用gluOrtho2D?
無論如何,這是我使用呈現文本的功能:
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();
}
而且,渲染功能類似於此:
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...");