为什么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;
}
如果要使用深度测试,必须做三件事:
- 窗口需要深度缓冲区。在过剩的情况下,这是通过在
glutInitDisplayMode()
中添加GLUT_DEPTH来完成
的 - 必须通过调用
glEnable(GL_DEPTH_TEST);
来启用深度测试 - 必须通过调用
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
来清除深度缓冲区(在每一帧中)
你正在做 1 和 2,而不是 3。由于您没有清除缓冲区,因此深度值可能位于当前存储的深度值后面,并且茶壶消失。
相关文章:
- 与互斥锁相比,旋转锁可以保证上下文切换
- 绘制旋转的三角形
- 旋转模型矩阵时的形状失真
- 四边形的 2D 旋转
- 垂直方向的 Gtk3+ 旋转按钮 (c/c++)
- 发布旋转矩阵(openGL/glm)
- 顺时针迭代旋转 3 位数字
- 形状对象的旋转和缩放不正确C++
- 如何在 OpenSceneGraph 中缩放/旋转/移动资产
- 如何有效地计算将单位立方体映射到自身的反射和旋转?
- 子轴围绕父轴而不是他自己的轴旋转
- 将使用太多的纹理插值器 - 带旋转的着色器
- SFML 向下移动时如何围绕屏幕中心旋转?
- 如何在OpenGL中正确旋转和缩放对象?
- 使用 C++在 OpenGL 中移动自动旋转的 3D 多边形
- 按时间自动旋转形状,同时在 OpenGL 中使用C++移动它们
- 如何使用增强::几何计算多边形的旋转固体体积?
- 使用 GDI+ 旋转位图,然后转换为 HDC
- 如何绕任意轴旋转点?
- 为什么GLUT_DEPTH破坏我的旋转茶壶的渲染