来自帧缓冲区的正确深度图值

时间:2012-08-18 17:05:44

标签: opengl depth

我目前从帧缓冲区获取深度图,但值卡在0.0或1.0或更高。如何获得介于0.0和1.0之间的深度?

另外,我在片段着色器中将depthmap作为sampler2D访问。

    modelsDepthTextureId = glGenTextures();
    glBindTexture(GL_TEXTURE_2D, modelsDepthTextureId);
    glTexImage2D( GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT24, screenWidth, screenHeight, 0,
        GL_DEPTH_COMPONENT, GL_INT, (java.nio.ByteBuffer) null);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

1 个答案:

答案 0 :(得分:0)

float linearize(float depth) {
    float zNear = 0.1;
    float zFar = 100.0;

    return (2.0 * zNear) / (zFar + zNear - depth * (zFar - zNear));
}

深度在深度纹理中显然保存。要使用适当的near和far值来线性化上面的代码。