如何在OpenGL中一起绘制2D和3D材质?
答
关于这样:
void render_perspective_scene(void);
void render_ortho_scene(void);
void render_HUD();
void display()
{
float const aspect = (float)win_width/(float)win_height;
glViewport(0,0,win_width,win_height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-aspect*near/lens, aspect*near/lens, -near/lens, near/lens, near, far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
render_perspective_scene();
// just clear the depth buffer, so that everything that's
// drawn next will overlay the previously rendered scene.
glClear(GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-aspect*scale, aspect*scale, -scale, scale, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
render_ortho_scene();
// Same for the HUD, only that we render
// that one in pixel coordinates.
glClear(GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, win_width, 0, win_height, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
render_HUD();
}
答
你在正确的轨道上,但是您绘制2D图形时,还必须禁用深度测试:glDisable(GL_DEPTH_TEST);
否则可能会被你的3D图形变成隐藏。
+0
只有当我们在3D场景背后绘制东西时,diable depth test才有意义? – jondinham
我试过gluPerspective画事情3D,然后用glOrtho画的东西在2D,但随后3D的东西似乎覆盖所有的二维图纸,从而使这些2D东西闪烁! – jondinham
任何解决方案?.. – jondinham
请提供您现有的代码,我们需要查看您所尝试的内容,以便我们可以进一步帮助您。 –