0

我正在尝试将深度缓冲区从 OpenGL 提取到 CPU 上的数组中。

我在下面的代码(稍微修改为原始代码是一个旨在提取渲染图像并成功完成的现有组件)“有效”,但总是返回正好 0。

GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_PIXEL_PACK_BUFFER, buffer);
int size = w*h*sizeof(GLubyte);
glBufferData(GL_PIXEL_PACK_BUFFER, size, NULL, GL_STREAM_READ);
glReadPixels(0, 0, w, h, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
GLubyte* depth = (GLubyte*)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
return depth;

glReadBuffer(GL_DEPTH)在通话后尝试过glBindBuffer,以及使用GL_DEPTH_COMPONENT24(每个资源都推荐但似乎没有人解释原因),但两者都会产生无效的枚举错误。GL_DEPTH_TEST 已启用,并且没有生成错误。

4

1 回答 1

0

确保你有一个深度缓冲区:

#include <GL/glew.h>
#include <GL/glut.h>

GLuint pbo = 0;
void display()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glEnable( GL_DEPTH_TEST );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( -2, 2, -2, 2, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glColor3ub( 255, 0, 0 );
    glBegin( GL_QUADS );
    glVertex3i( -1, -1, 0 );
    glVertex3i(  1, -1, 0 );
    glVertex3i(  1,  1, 0 );
    glVertex3i( -1,  1, 0 );
    glEnd();

    int w = glutGet( GLUT_WINDOW_WIDTH );
    int h = glutGet( GLUT_WINDOW_HEIGHT );
    glBindBuffer( GL_PIXEL_PACK_BUFFER, pbo );
    glBufferData( GL_PIXEL_PACK_BUFFER, w * h * sizeof( GLfloat ), 0, GL_STREAM_READ );
    glReadPixels( 0, 0, w, h, GL_DEPTH_COMPONENT, GL_FLOAT, 0 );
    GLfloat* ptr = (GLfloat*)glMapBuffer( GL_PIXEL_PACK_BUFFER, GL_READ_ONLY );
    if( ptr )
    {
        // look for values of ~0.5 here in ptr...
    }
    glUnmapBuffer( GL_PIXEL_PACK_BUFFER );
    glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );

    glutSwapBuffers();
}

int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
    glutInitWindowSize( 128, 128 );
    glutCreateWindow( "GLUT" );
    glewInit();

    glGenBuffers( 1, &pbo );

    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}
于 2013-09-12T15:21:46.757 回答