我一直在OpenGL中使用SSAO。我決定從OpenGL中的this教程中爲我的延遲渲染器實現SSAO。不幸的是我一直無法讓它工作得很好。取決於相機的位置,SSAO變暗的區域變化很大。我知道當相機移動時,SSAO的輸出可能會有一些變化,但這比我在其他SSAO實現中觀察到的要大得多。SSAO用相機角度顯着改變
這裏是片段着色器代碼
void main() {
vec3 origin = positionFromDepth(texture2D(gDepth, samplePosition));
vec3 normal = texture2D(gNormal, samplePosition).xyz; //multiplying this
//by 2 and subtracting 1 doesn't seem to help
vec2 random = getRandom(samplePosition);
float radius = uRadius/origin.z;
float occlusion = 0.0;
int iterations = samples/4;
for (int i = 0; i<iterations; i++) {
vec2 coord1 = reflect(kernel[i], random)*radius;
vec2 coord2 = vec2(coord1.x*0.707 - coord1.y*0.707, coord1.x*0.707 + coord1.y*0.707);
occlusion += occlude(samplePosition, coord1 * 0.25, origin, normal);
occlusion += occlude(samplePosition, coord2 * 0.50, origin, normal);
occlusion += occlude(samplePosition, coord1 * 0.75, origin, normal);
occlusion += occlude(samplePosition, coord2, origin, normal);
}
color = vec4(origin, 1);
}
的positionFromDepth()
功能:
vec3 positionFromDepth(float depth) {
float near = frustrumData.x;
float far = frustrumData.y;
float right = frustrumData.z;
float top = frustrumData.w;
vec2 ndc;
vec3 eye;
eye.z = near * far/((depth * (far - near)) - far);
ndc.x = ((gl_FragCoord.x/buffersize.x) - 0.5) * 2.0;
ndc.y = ((gl_FragCoord.y/buffersize.y) - 0.5) * 2.0;
eye.x = (-ndc.x * eye.z) * right/near;
eye.y = (-ndc.y * eye.z) * top/near;
return eye;
}
而且occlude()
功能:
float occlude(vec2 uv, vec2 offsetUV, vec3 origin, vec3 normal) {
vec3 diff = positionFromDepth(texture2D(gDepth,(uv+offsetUV)))-origin;
vec3 vec = normalize(diff);
float dist = length(diff)/scale;
return max(0.0,dot(normal,vec)-bias)*(1.0/(1.0+dist))*intensity;
}
我有一種感覺問題可能是在positionFromDepth()
函數,除了我用th e相同的代碼,用於完美渲染的渲染器的光照階段(我認爲)。我已經完成了這個代碼一千次,並沒有發現任何突出的錯誤。我已經嘗試了各種值bias
,radius
,intenisty
和scale
,但這似乎不是問題。我很擔心無論我法線或立場是錯誤的,所以這裏是其中的一些屏幕截圖:
重建的位置: 和正常緩衝:
我將包括遮擋的圖像緩衝區,但這個問題大多隻在相機移動時才顯而易見,圖像無法顯示。
有沒有人有任何想法這裏有什麼問題?
嗨!我遇到了執行相同教程的相同問題。你能告訴我,你是否管理過這個,你做了什麼? – TomatoMato