如果我从GLUT_DEPTH
中删除glutInitDisplayMode()
,茶壶将不会隐藏隐藏的表面,但会渲染。我想隐藏隐藏的表面,因此添加GLUT_DEPTH
茶壶消失了。我不知道是否有些东西乱了,或者我是否缺少深度缓冲区的东西?
void reshape( int x, int y )
{
if( y == 0 || x == 0 ) return;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 39.0, (GLdouble)x / (GLdouble)y, 0.6, 40.0 );
glMatrixMode( GL_MODELVIEW );
glViewport( 0, 0, x, y ); //Use the whole window for rendering
}
GLfloat xRotated, yRotated, zRotated;
GLdouble size = 0.5;
void display( void )
{
glMatrixMode( GL_MODELVIEW );
// clear the drawing buffer.
glClear( GL_COLOR_BUFFER_BIT );
// clear the identity matrix.
glLoadIdentity();
// traslate the draw by z = -4.0
// Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
glTranslatef( 0.0, 0.0, -4.5 );
// Red color used to draw.
glColor3f( 0.8, 0.5, 0.1 );
// changing in transformation matrix.
// rotation about X axis
glRotatef( xRotated, 1.0, 0.0, 0.0 );
// rotation about Y axis
glRotatef( yRotated, 0.0, 1.0, 0.0 );
// rotation about Z axis
glRotatef( zRotated, 0.0, 0.0, 1.0 );
// scaling transfomation
glScalef( 1.0, 1.0, 1.0 );
glPushMatrix();
// built-in (glut library) function , draw you a Teapot.
glutSolidTeapot( size );
// Flush buffers to screen
glPopMatrix();
glFlush();
// swap buffers called because we are using double buffering
glutSwapBuffers();
}
void reshapeFunc( int x, int y )
{
if( y == 0 || x == 0 ) return; //Nothing is visible then, so return
//Set a new projection matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
//Angle of view:40 degrees
//Near clipping plane distance: 0.5
//Far clipping plane distance: 20.0
gluPerspective( 40.0, (GLdouble)x / (GLdouble)y, 0.5, 20.0 );
glViewport( 0, 0, x, y ); //Use the whole window for rendering
}
void idleFunc( void )
{
yRotated += 0.3;
display();
}
int main( int argc, char **argv )
{
//Initialize GLUT
glutInit( &argc, argv );
//double buffering used to avoid flickering problem in animation
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
// window size
glutInitWindowSize( 400, 350 );
// create the window
glutCreateWindow( "Teapot Rotating Animation" );
glEnable( GL_LIGHTING ); // enable the light source
glEnable( GL_LIGHT0 );
glShadeModel( GL_SMOOTH );
glEnable( GL_DEPTH_TEST ); // for hidden surface removal
glEnable( GL_NORMALIZE ); // normalize vectors for proper shading
//set properties of the surface material
GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f }; // color
GLfloat mat_diffuse[] = { 0.6f, 0.6f, 0.6f, 1.0f };
GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat mat_shininess[] = { 50.0f };
glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient );
glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse );
glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular );
glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess );
// set the light source properties
GLfloat lightIntensity[] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat light_position[] = { 2.0f, 6.0f, 3.0f, 0.0f };
glLightfv( GL_LIGHT0, GL_POSITION, light_position );
glLightfv( GL_LIGHT0, GL_DIFFUSE, lightIntensity );
xRotated = yRotated = zRotated = 0;
yRotated = 40;
glClearColor( 0.1f, 0.1f, 0.1f, 0.0f ); // background is light gray
//Assign the function used in events
glutIdleFunc( idleFunc );
glutDisplayFunc( display );
glutReshapeFunc( reshapeFunc );
//Let start glut loop
glutMainLoop();
return 0;
}
最佳答案
如果应使用深度测试,则必须完成三件事:
窗口需要一个深度缓冲区。对于glut,可通过将GLUT_DEPTH添加到glutInitDisplayMode()
来完成
必须通过调用glEnable(GL_DEPTH_TEST);
启用深度测试
必须通过调用glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
清除深度缓冲区(在每帧中)
您正在执行1和2,但没有执行3。由于您没有清除缓冲区,因此深度值可能位于当前存储的深度值后面,并且茶壶消失了。