1

我有以下代码:

#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 旋转相机视图

4

1 回答 1

2

您的问题可能出在gluPerspective通话中。你永远不应该将你的近平面设置为零,因为它会导致奇怪的矩阵(当你考虑类似的glFrustum调用时,这会更加明显)。尝试将近平面更改为 1 甚至 0.01,看看它是否看起来更好。

关于为什么会发生这种情况的线索可以在gluPerspective文档中找到:

注意:
深度缓冲区精度受zNearzFar指定值的影响。zFar 与 zNear 的比率越大,深度缓冲区在区分彼此靠近的表面方面的效果就越差。如果

r = zFar / zNear

大约 log 2 (r) 位深度缓冲区精度丢失。因为当zNear接近 0 时 r 接近无穷大,所以zNear绝不能设置为 0。

于 2013-11-14T15:15:19.287 回答