作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我从 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()
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 清除深度缓冲区(在每一帧中);
您正在执行 1 和 2 而不是 3。由于您没有清除缓冲区,深度值可能落后于当前存储的深度值并且茶壶消失了。
关于c++ - 为什么 GLUT_DEPTH 破坏了我旋转茶壶的渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46898955/
如果我从 glutInitDisplayMode() 中删除 GLUT_DEPTH,茶壶不会隐藏隐藏的表面,但会渲染。我想隐藏隐藏的表面,所以我添加了 GLUT_DEPTH,茶壶就消失了。我不知道是不
我正在使用 Glut 编写一些 OpenGl 教程,我遇到了 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) 我明白它的作用,但我不明
我是一名优秀的程序员,十分优秀!