在没有着色器的情况下在OpenGL中渲染深度缓冲区

时间:2014-06-17 14:46:16

标签: c++ opengl depth-buffer

我有以下C ++ OpenGL代码,用于渲染场景中像素的RGB值:

glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, _windowWidth, _windowHeight);

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

glMatrixMode( GL_PROJECTION );
glLoadIdentity();

float aspectRatio = float(_windowWidth) / float(_windowHeight);
gluPerspective(60.0f, aspectRatio, 0.1f, 1000.0f);

glMatrixMode(GL_MODELVIEW);
_camera.Update();
glLoadMatrixf(_camera.Matrix()[0]);

_scene.Render();

glutSwapBuffers();

但是,我想改为渲染场景的深度缓冲区,即每个像素值都是摄像机前面的z距离。

也就是说,我目前正在获得顶部图片,但我想要底部图片(来自here的图片):

enter image description here

我应该怎么做?我不认为我正在使用着色器(我是吗?) - 给出的建议here似乎表明这可能只适用于着色器。

还有this code,虽然这看起来像是使用着色器。修改我的代码以使用着色器有多难?

2 个答案:

答案 0 :(得分:6)

您可以通过glReadPixels()GL_DEPTH_COMPONENT将深度缓冲区粘贴到主机内存中,然后将缓冲区重新上传为GL_LUMINANCE纹理:

screenshot

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

#include <vector>
using namespace std;

void display()
{
    int w = glutGet( GLUT_WINDOW_WIDTH );
    int h = glutGet( GLUT_WINDOW_HEIGHT );

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    double ar = w / static_cast< double >( h );
    const float zNear = 0.1;
    const float zFar = 10.0;
    gluPerspective( 60, ar, zNear, zFar );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    glTranslatef( 0, 0, -4 );

    static float angle = 0;
    angle += 3;

    glPushMatrix();
    glRotatef( angle, 0.1, 0.5, 0.3 );
    glColor3ub( 255, 0, 0 );
    glutSolidTeapot( 1 );
    glPopMatrix();

    vector< GLfloat > depth( w * h, 0 );
    glReadPixels( 0, 0, w, h, GL_DEPTH_COMPONENT, GL_FLOAT, &depth[0] ); 

    // linearize depth
    // http://www.geeks3d.com/20091216/geexlab-how-to-visualize-the-depth-buffer-in-glsl/
    for( size_t i = 0; i < depth.size(); ++i )
    {
        depth[i] = ( 2.0 * zNear ) / ( zFar + zNear - depth[i] * ( zFar - zNear ) );
    }

    static GLuint tex = 0;
    if( tex > 0 )
        glDeleteTextures( 1, &tex );
    glGenTextures(1, &tex);
    glBindTexture( GL_TEXTURE_2D, tex);
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_LUMINANCE, w, h, 0, GL_LUMINANCE, GL_FLOAT, &depth[0] );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( 0, w, 0, h, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glEnable( GL_TEXTURE_2D );
    glColor3ub( 255, 255, 255 );
    glScalef( 0.3, 0.3, 1 );
    glBegin( GL_QUADS );
    glTexCoord2i( 0, 0 );
    glVertex2i( 0, 0 );
    glTexCoord2i( 1, 0 );
    glVertex2i( w, 0 );
    glTexCoord2i( 1, 1 );
    glVertex2i( w, h);
    glTexCoord2i( 0, 1 );
    glVertex2i( 0, h );
    glEnd();
    glDisable( GL_TEXTURE_2D );

    glutSwapBuffers();
}

void timer( int value )
{
    glutPostRedisplay();
    glutTimerFunc( 16, timer, 0 );
}

int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
    glutInitWindowSize( 600, 600 );
    glutCreateWindow( "GLUT" );
    glewInit();
    glutDisplayFunc( display );
    glutTimerFunc( 0, timer, 0 );
    glEnable( GL_DEPTH_TEST );
    glutMainLoop();
    return 0;
}

往返CPU的速度并不快(特别是在主机端线性化时)。您可以使用PBOs将其转换为GPU到GPU的转移,但您将失去线性化。

答案 1 :(得分:5)

您可以使用Framebuffer Object (fbo)将深度值渲染为纹理。然后在第二个渲染过程中,在整个视口上绘制一个四边形,并在其上应用深度纹理。

或者你可以编写一个片段着色器,根据它们的深度输出片段颜色,这可以单次渲染。

请参阅here一些示例。你可以找到更多搜索'fbo'。

相关问题