来自z缓冲区的OpenGL-不准确的深度值

时间:2016-10-25 20:00:17

标签: c++ opengl graphics depth-buffer

我在OpenGL中绘制四边形,然后使用glReadPixels访问深度缓冲区。但是,问题是我总是从OpenGL窗口的所有位置得到1。例如,对于zbuffer [0] [0](请参阅下面的代码),该值应为0,因为窗口的该部分没有对象。同样,对于zbuffer [639] [470],该值也应为0,但我同时收到1。

你能指点一下我在这里做错了什么吗?

#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>
#include <fstream>

using namespace std;



void display ( )   // Create The Display Function
{

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    gluLookAt(0.0 ,0.0 ,0.0 ,0.0 ,0.0 ,-1.0 ,0.0 ,1.0 ,0.0);


    glBegin(GL_QUADS); 

        glColor3f(0.813205,0.813205,0.813205);
        glVertex3f(-0.389662380802125,0.249152038469432,  -1.12528215173346); // top left corner

        glColor3f(0.309385,0.309385,0.309385);
        glVertex3f(0.433401472883409,0.272288002674917,  -0.923754348668237); // top right corner

        glColor3f(0.288292,0.288292,0.288292);
        glVertex3f(0.441173907261717,-0.296124431126115,  -0.915317251508196);// bottom right corner

        glColor3f(0.74028,0.74028,0.74028);
        glVertex3f(-0.388657135983495,-0.317680965994535,  -1.09611219327051); // bottom left corner


    glEnd();

    GLfloat zbuffer[640][480];

    glReadPixels(0,0,640,480,GL_DEPTH_COMPONENT,GL_FLOAT, zbuffer);

    cout<<"Depth1:"<<zbuffer[0][0]<<endl;
    cout<<"Depth2:"<<zbuffer[639][470]<<endl;

    glFlush();

    glutSwapBuffers ( );
}

void reshape ( int w, int h )   // Create The Reshape Function (the viewport)
{
    glViewport(0 ,0 ,(GLsizei)w,( GLsizei)h);
    glMatrixMode (GL_PROJECTION );
    glLoadIdentity ( );
    gluPerspective(48.6f, (GLfloat) 640 / 480, 0.01f, 1000.0f);
    glMatrixMode (GL_MODELVIEW) ;
}

void keyboard ( unsigned char key, int x, int y )  // Create Keyboard Function
{
  switch ( key ) {
    case 27:        // When Escape Is Pressed...
      exit ( 0 );   // Exit The Program
      break;        // Ready For Next Case
    default:        // Now Wrap It Up
      break;
  }
}

void mouse(int button, int state, int x, int y) {

        float mx = x ;
        float my = y ;
//      ox = (mx/320-1.0);
//      oy = -(my/240 -1.0);
        switch(button){
            case GLUT_LEFT_BUTTON:
                if(state==GLUT_DOWN){
                    printf("%f,",mx);
                    printf("%f\n",my);
                }
                break;
        }
}

int main ( int argc, char** argv )   
{
    glutInit            ( &argc, argv ); 
    glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize  (640, 480);
    glutInitWindowPosition(0, 0);
    glutCreateWindow    ( "My Window" ); directory as title)
    glutReshapeFunc ( reshape ) ;
    glutDisplayFunc     ( display );  // Matching Earlier Functions To Their Counterparts
    glutKeyboardFunc    ( keyboard );
    glutMouseFunc(mouse);
    glutMainLoop        ( );          // Initialize The Main Loop


  return 0;
}

这是在OpenGL中绘制的对象:

enter image description here

1 个答案:

答案 0 :(得分:2)

  

例如,对于zbuffer [0] [0](请参阅下面的代码),该值应该为0,因为窗口的那部分没有对象。

不,不应该。默认glClearDepth值为1.0,因此glClear( GL_DEPTH_BUFFER_BIT)将深度缓冲区中的每个像素设置为1.0。这也是有道理的,因为使用dfeault约定,近平面映射到窗口空间中的z=0,远平面映射到z=1,深度测试设置为GL_LESS,所以那些碎片会越过那个当前在该位置绘制的碎片。