繁体   English   中英

OpenGL-z缓冲区的深度值不正确

[英]OpenGL- inaccurate depth values from z 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中绘制的对象:

在此处输入图片说明

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM