Ric*_*ral 7 opengl clipping viewport foreground depth
我有这个功能,它在屏幕的左下角绘制一个小的3D轴坐标系,但根据我面前的情况,它可能会被剪裁.
例如,我在地面上绘制了一个平坦的地形,在XZ平面上Y = 0.摄像机位于Y = 1.75(模拟一般人的身高).如果我抬头,它工作正常,如果我向下看,它会被地平面夹住.
向上看
:http://i.stack.imgur.com/Q0i6g.png向下看:http://i.stack.imgur.com/D5LIx.png
我调用的函数在角落绘制轴系统是这样的:
void Axis3D::DrawCameraAxisSystem(float radius, float height, const Vector3D rotation) {
if(vpHeight == 0) vpHeight = 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, vpWidth, vpHeight);
gluPerspective(45.0f, 1.0 * vpWidth / vpHeight, 1.0f, 5.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -3.0f);
glRotatef(-rotation.x, 1.0f, 0.0f, 0.0f);
glRotatef(-rotation.y, 0.0f, 1.0f, 0.0f);
DrawAxisSystem(radius, height);
}
Run Code Online (Sandbox Code Playgroud)
我认为现在有几个主要功能与问题相关:
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
void changeSize(int width, int height) {
if(height == 0) height = 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, width, height);
gluPerspective(60.0f, 1.0 * width / height, 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
}
void renderScene(void) {
glClearColor(0.7f, 0.7f, 0.7f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
changeSize(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
glLoadIdentity();
SceneCamera.Move(CameraDirection, elapsedTime);
SceneCamera.LookAt();
SceneAxis.DrawCameraAxisSystem(0.03f, 0.8f, SceneCamera.GetRotationAngles());
glutSwapBuffers();
}
Run Code Online (Sandbox Code Playgroud)
建议?
JCo*_*per 17
您可以glClear(GL_DEPTH_BUFFER_BIT);在渲染叠加层之前禁用深度测试,而不是禁用深度测试.然后,无论深度信息是什么都消失了,但像素仍然存在.但是,您的叠加层仍会正确呈现.
您还可以为 3D 轴保留一小部分深度范围。剩下的一切都适合您的场景。像这样:
// reserve 1% of the front depth range for the 3D axis
glDepthRange(0, 0.01);
Draw3DAxis();
// reserve 99% of the back depth range for the 3D axis
glDepthRange(0.01, 1.0);
DrawScene();
// restore depth range
glDepthRange(0, 1.0);
Run Code Online (Sandbox Code Playgroud)