我正在调试 SSAO 的问题并尝试可视化我的深度缓冲区。结果如下:
我将深度和法线存储在单个 16 位 RGBA 纹理中。这是我的深度通道着色器:
// Vertex shader
#version 150 core
#extension GL_ARB_explicit_attrib_location : enable
uniform mat4 _ViewMatrix;
uniform mat4 _ViewProjectionMatrix;
uniform mat4 modelMatrix;
layout (location = 0) in vec4 aPosition;
layout (location = 2) in vec3 aNormal;
out vec4 vPosition;
out vec3 vNormal;
void main()
{
gl_Position = _ViewProjectionMatrix * modelMatrix * aPosition;
mat4 modelViewMatrix = _ViewMatrix * modelMatrix;
vPosition = modelViewMatrix * aPosition;
vNormal = mat3( modelViewMatrix ) * aNormal;
}
// Fragment shader.
#version 150 core
// Calculated as 1.0 / (far - near)
uniform float uLinearDepthConstant;
in vec4 vPosition;
in vec3 vNormal;
out vec4 outDepthNormal;
void main()
{
float linearDepth = -vPosition.z * uLinearDepthConstant;
outDepthNormal = vec4( linearDepth, normalize( vNormal ) );
}
然后我在渲染纹理的着色器中可视化深度(我已经硬编码了近平面和远平面的距离):
void main()
{
float depth = texture( depthNormalMap, vTexCoord ).r;
fragColor = vec4((2.0 * 1.0) / (200.0 + 1.0 - depth * (200.0 - 1.0)));
}
结果应该看起来很顺利还是可能是什么问题?我正在创建这样的纹理:
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_HALF_FLOAT, 0 );