为什么GLUT_DEPTH会破坏旋转茶壶的渲染?

时间:2017-10-23 21:39:40

标签: c++ opengl glut freeglut

如果我从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;
}

1 个答案:

答案 0 :(得分:3)

如果应该使用深度测试,则必须做三件事:

  1. 窗口需要深度缓冲区。在过剩中,这是通过将GLUT_DEPTH添加到glutInitDisplayMode()
  2. 来完成的
  3. 必须通过调用glEnable(GL_DEPTH_TEST);
  4. 启用深度测试
  5. 必须通过调用glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  6. 清除深度缓冲区(在每一帧中)

    你正在做1和2但不是3.由于你没有清除缓冲区,深度值可能位于当前存储的深度值之后,茶壶会消失。

相关问题