gpt4 book ai didi

OpenGL GLSL SSAO 实现

转载 作者:行者123 更新时间:2023-12-03 23:49:38 26 4
gpt4 key购买 nike

我尝试基于此处找到的 R5 演示来实现屏幕空间环境光遮蔽 (SSAO):http://blog.nextrevision.com/?p=76

事实上,我尝试调整他们的 SSAO - 线性着色器以适应我自己的小引擎。

1)我计算 View 空间表面法线和线性深度值。
我使用以下着色器将它们存储在 RGBA 纹理中:

顶点:

varNormalVS = normalize(vec3(vmtInvTranspMatrix * vertexNormal));
depth = (modelViewMatrix * vertexPosition).z;
depth = (-depth-nearPlane)/(farPlane-nearPlane);
gl_Position = pvmtMatrix * vertexPosition;

分段:
gl_FragColor = vec4(varNormalVS.x,varNormalVS.y,varNormalVS.z,depth)

对于我的线性深度计算,我引用了: http://www.gamerendering.com/2008/09/28/linear-depth-texture/

这是对的吗?
纹理似乎是正确的,但也许不是?

enter image description here

2) 实际的 SSAO 实现:
如上所述,原件可以在这里找到: http://blog.nextrevision.com/?p=76

或更快:关于粘贴箱 http://pastebin.com/KaGEYexK

与原始纹理相比,我只使用 2 个输入纹理,因为我的一个纹理存储了这两种纹理,即 RGB 法线和线性深度 Alpha。

我的第二个纹理,随机的普通纹理,看起来像这样:
http://www.gamerendering.com/wp-content/uploads/noise.png

我使用几乎完全相同的实现,但我的结果是错误的。

在详细介绍之前,我想先澄清一些问题:

1) ssao 着色器使用projectionMatrix 和它的逆矩阵。

由于它是通过正交投影渲染到屏幕对齐的四边形上的后处理效果,因此projectionMatrix 是正交矩阵。正确还是错误?

2)具有组合的法线和深度纹理,而不是两个单独的纹理。

在我看来,这是 R5 实现和我的实现尝试之间的最大区别。我认为这应该不是一个大问题,但是,由于不同的深度纹理,这最有可能导致问题。

请注意 R5_clipRange 看起来像这样
vec4 R5_clipRange = vec4(nearPlane, farPlane, nearPlane * farPlane, farPlane - nearPlane);

原来的:
float GetDistance (in vec2 texCoord)
{
//return texture2D(R5_texture0, texCoord).r * R5_clipRange.w;
const vec4 bitSh = vec4(1.0 / 16777216.0, 1.0 / 65535.0, 1.0 / 256.0, 1.0);
return dot(texture2D(R5_texture0, texCoord), bitSh) * R5_clipRange.w;
}

我不得不承认我不理解代码片段。我的深度存储在我的纹理的 alpha 中,我认为这样做就足够了
return texture2D(texSampler0, texCoord).a  * R5_clipRange.w;

正确还是错误?

最佳答案

你的正常纹理似乎是错误的。我的猜测是您的 vmtInvTranspMatrix是模型- View 矩阵。但是它应该是模型- View -投影矩阵(请注意,您需要屏幕空间法线,而不是 View 空间法线)。深度计算是正确的。

我已经实现了一次 SSAO,正常的纹理看起来像这样(注意这里没有蓝色):

screen space normal

1) ssao shader uses projectionMatrix and it's inverse matrix. Since it is a post processing effect rendered onto a screen aligned quad via orthographic projection, the projectionMatrix is the orthographic matrix. Correct or Wrong ?



如果您的意思是在第二遍渲染四边形以计算实际 SSAO,是的。您可以完全避免与正交投影矩阵相乘。如果您使用 [x,y] 尺寸范围从 -1 到 1 来渲染屏幕四边形,您可以使用非常简单的顶点着色器:
const vec2 madd=vec2(0.5,0.5);

void main(void)
{
gl_Position = vec4(in_Position, -1.0, 1.0);
texcoord = in_Position.xy * madd + madd;
}

2) Having a combined normal and Depth texture instead of two seperate ones.



不,这不会造成问题。这样做是一种常见的做法。

关于OpenGL GLSL SSAO 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9062116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com