2

我有一个有效的 G 缓冲区(颜色、法线、深度),对于每个聚光灯,我都有从灯光的角度看的深度图。问题出在光照贴图阶段,我无法让它工作。你能指出这里有什么问题吗?

这是我对这个过程的“理解”:

1. Pass the G-buffer depth map to the lighting shader
2. Pass the light's depth map to the lighting shader
3. For each pixel in the G-buffer depth map:
    3.1. Get the depth value from the texture
    3.2. Convert the X and Y coordinates from texture space to clip space
        3.2.1. pos.x = texcoord.x * 2.0 - 1.0;
        3.2.2. pos.y = -(texcoord.y * 2.0 - 1.0);
    3.3. Set pos.w to 1
    3.4. Multiply the position by inverse view projection matrix to get the world space coordinate
    3.5. Divide the new coordinates by w to "correct" the coords
    3.6. Multiply the coordinates by light's view matrix
    3.7. Change the coordinates from clip space to texture space
    3.8. Compare the Z value of the pixel to the depth value in the light's depth map
    3.9. If pos.z > depthmap.z, the pixel is in shadow
4

1 回答 1

1

这个过程实际上是相反的。您不会将纹理坐标转换为剪辑空间,而是将坐标从剪辑空间转换为纹理。为此,您需要将光照相机和投影传递给片段着色器(并且至少在 OpenGLES 2.0 中将位置从顶点着色器传递给片段着色器,不了解 OpenGL 3.3)。

  • 将位置乘以相机和投影,您将获得灯光视图中的位置。
  • 将 xyz 除以 w,您将获得光照视图剪辑空间中的位置。
  • 将 x 和 y 乘以 0.5 并加上 0.5,您将获得阴影贴图的 uv 坐标。

现在您可以在 uv 处读取阴影贴图的深度并将其与像素的深度进行比较。

于 2013-02-24T19:22:22.617 回答