为什么 GLUT_DEPTH 破坏了我旋转茶壶的渲染?
Why does GLUT_DEPTH destroy the rendering of my rotating teapot?
如果我从 glutInitDisplayMode()
中删除 GLUT_DEPTH
,茶壶不会隐藏隐藏的表面,但会渲染。我想隐藏隐藏的表面,所以我添加 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;
}
如果要使用深度测试,必须做三件事:
- window 需要一个深度缓冲区。在过剩情况下,这是通过将 GLUT_DEPTH 添加到
glutInitDisplayMode()
来完成的
- 必须通过调用
glEnable(GL_DEPTH_TEST);
来启用深度测试
- 必须通过调用
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
清除深度缓冲区(在每一帧中)
您正在执行 1 和 2,但没有执行 3。由于您没有清除缓冲区,深度值可能落后于当前存储的深度值并且茶壶消失了。
如果我从 glutInitDisplayMode()
中删除 GLUT_DEPTH
,茶壶不会隐藏隐藏的表面,但会渲染。我想隐藏隐藏的表面,所以我添加 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;
}
如果要使用深度测试,必须做三件事:
- window 需要一个深度缓冲区。在过剩情况下,这是通过将 GLUT_DEPTH 添加到
glutInitDisplayMode()
来完成的
- 必须通过调用
glEnable(GL_DEPTH_TEST);
来启用深度测试
- 必须通过调用
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
清除深度缓冲区(在每一帧中)
您正在执行 1 和 2,但没有执行 3。由于您没有清除缓冲区,深度值可能落后于当前存储的深度值并且茶壶消失了。