LWJGL如何渲染SSAO

时间:2015-04-03 23:13:51

标签: java lwjgl vbo shadows ssao

我目前在LWJGL中有一个工作场景,它渲染了一堆体素。我在VBO中使用几何的verticies,法线和颜色以及glDrawArrays()到屏幕上。

我在google上搜索了如何使用SSAO或屏幕空间环境遮挡渲染场景。我发现的几乎所有结果都只是详细说明了我需要的GLSL代码,但完全省略了所需的lwjgl代码。

这是我为SSAO找到的片段着色器之一(我不记得作者是谁,或者发布者是否是原作者)

uniform sampler2D texture0;
uniform sampler2D texture1;

uniform vec2 camerarange;
uniform vec2 screensize;

varying vec2 texCoord;

float readDepth( in vec2 coord ) {
    return (2.0 * camerarange.x) / (camerarange.y + camerarange.x - texture2D( texture0, coord ).x * (camerarange.y - camerarange.x));  
}


void main(void)
{   
    float depth = readDepth( texCoord );
    float d;

    float pw = 1.0 / screensize.x;
    float ph = 1.0 / screensize.y;

    float aoCap = 1.0;

    float ao = 0.0;

    float aoMultiplier=1000.0;

    float depthTolerance = 0.0001;

    d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    pw*=2.0;
    ph*=2.0;
    aoMultiplier/=2.0;

    d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    pw*=2.0;
    ph*=2.0;
    aoMultiplier/=2.0;

    d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    pw*=2.0;
    ph*=2.0;
    aoMultiplier/=2.0;

    d=readDepth( vec2(texCoord.x+pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y+ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x+pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    d=readDepth( vec2(texCoord.x-pw,texCoord.y-ph));
    ao+=min(aoCap,max(0.0,depth-d-depthTolerance) * aoMultiplier);

    ao/=16.0;

    gl_FragColor = vec4(1.0-ao) * texture2D(texture1,texCoord);
}

我的问题是,我在LWJGL空间中需要做些什么来传递

的变量
uniform sampler2D texture0;
uniform sampler2D texture1;

uniform vec2 camerarange;
uniform vec2 screensize;

如果 texture0 是2d纹理格式的深度缓冲区,我该如何创建呢?

我是否需要将几何VBO渲染到屏幕然后获取深度缓冲纹理?

什么是 texture1 ?我的程序只使用颜色而不是纹理,所以我不知道那可能是什么。

1 个答案:

答案 0 :(得分:0)

我会将它渲染到一个额外的FBO并获得深度纹理(你需要深度纹理而不是深度缓冲区!)id。然后,您可以轻松地将其加载到着色器。