我有以下代码:
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
void display(void);
void reshape(int, int);
void keyboard_input(unsigned char key, int x, int y);
int d =0;
float angle = 0;
float x = 2;
bool add_flag = false,flag =true,flag1 = false,flag2 = false;
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(512, 512);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("Lab 6");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard_input);
glClearColor(0,0,0,1);
glutIdleFunc(display);
glutMainLoop();
return 0;
}
void drawSnowMan() {
glRotatef(angle,0,1,0);
glColor3f(1.0f, 1.0f, 1.0f);
// Draw Body
glTranslatef(0.0f ,0.75f, 0.0f);
glutSolidSphere(0.75f,20,20);
// Draw Head
glTranslatef(0.0f, 1.0f, 0.0f);
glutSolidSphere(0.25f,20,20);
// Draw Eyes
glPushMatrix();
glColor3f(0.0f,0.0f,0.0f);
glTranslatef(0.05f, 0.10f, 0.18f);
glutSolidSphere(0.05f,10,10);
glTranslatef(-0.1f, 0.0f, 0.0f);
glutSolidSphere(0.05f,10,10);
glPopMatrix();
// Draw Nose
//
glColor3f(1.0f, 0.5f , 0.5f);
glRotatef(0.0f,1.0f, 0.0f, 0.0f);
glTranslatef(0, 0.0f, 0.25f);
glutSolidCone(0.04f,0.2f,10,2);
}
void drawSnow()
{
{
glRotatef(angle,0,1,0);
glColor3f(1.0, 1 , 1);
glutSolidSphere(0.05f,10,10);
}
}
void display()
{
if(flag1 == true)
{
angle = angle + 2;
flag1=false;
}
else if(flag2 == true)
{
angle = angle - 2;
flag2 = false;
}
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
for(float i = -2; i <= 2 ; i=i+0.5)
{
if(d%3==0)
glTranslatef(0,i,0);
glTranslatef(i,x,0.0);
drawSnow();
if(x >= 2)
{
x = 2;
add_flag = true;
}
else if(x <= -2)
{
x = 2;
add_flag = false;
}
if(add_flag == false)
x = x + 0.0002;
else if(add_flag == true )
x = x - 0.0002;
d++;
glLoadIdentity();
}
glTranslatef(0.0f, -1.0f, 0.0f);
drawSnowMan();
glLoadIdentity();
glRotatef(angle,0,1,0);
glTranslatef(0.0f, -1, 1.5f);
glColor3f(0, 1,1);
glRotatef(90,-1,0,0);
glutSolidCone(0.08f,0.5f,10,2);
glTranslatef(0.1, 0, 0);
glutSolidCone(0.08f,0.2f,10,2);
glTranslatef(-0.2, 0, 0);
glutSolidCone(0.08f,0.2f,10,2);
glTranslatef(-0.1, 0, 0);
glutSolidCone(0.08f,0.5f,10,2);
glRotatef(-90,-1,0,0);
glLoadIdentity();
glRotatef(angle,0,1,0);
glTranslatef(1.0f, -1, -3.5f);
glColor3f(0, 1,1);
glRotatef(90,-1,0,0);
glutSolidCone(0.08f,0.5f,10,2);
glTranslatef(0.1, 0, 0);
glutSolidCone(0.08f,0.2f,10,2);
glTranslatef(-0.2, 0, 0);
glutSolidCone(0.08f,0.2f,10,2);
glTranslatef(-0.1, 0, 0);
glutSolidCone(0.08f,0.5f,10,2);
glRotatef(-90,-1,0,0);
glLoadIdentity();
glutSwapBuffers();
}
void reshape(int width, int height)
{
//glEnable(GL_DEPTH_TEST);
glViewport(0,0,width,height);
/* switch to the projection matrix */
glMatrixMode(GL_PROJECTION);
/* clear the projection matrix */
glLoadIdentity();
gluPerspective(45,1,0,20);
glTranslatef(0,0,-5);
/* at centre but resizing won't disturb drawing */
/* switch back to the model view matrix */
glMatrixMode(GL_MODELVIEW);
}
void keyboard_input(unsigned char key, int x, int y)
{
switch(key)
{
case '2':
flag1 = true;
break;
case '1' :
flag2 = true;
break;
}
}
每当我启用深度测试时,屏幕变为空白。我需要隐藏后面的视图中的对象。
键盘类型1和2以旋转摄像机视图
答案 0 :(得分:2)
您的问题可能出在gluPerspective
来电。你不应该将近平面设置为零,因为它会导致奇怪的矩阵(当你考虑类似的glFrustum
调用时,这更为明显)。尝试将近平面更改为1或甚至0.01以查看它是否看起来更好。
关于为什么会发生这种情况的线索可以在gluPerspective
documentation:
备注:强>
深度缓冲区精度受为 zNear 和 zFar 指定的值的影响。 zFar与zNear的比率越大,深度缓冲区在区分彼此靠近的表面时的效果越差。如果r = zFar / zNear
大致记录 2 (r)位深度缓冲区精度丢失。因为当 zNear 接近0时r接近无穷大,所以 zNear 绝不能设置为0.