gpt4 book ai didi

c++ - 阴影贴图定位和分辨率

转载 作者:太空狗 更新时间:2023-10-29 23:01:57 24 4
gpt4 key购买 nike

我目前正在学习 C++ 和 OpenGL,想知道是否有人可以向我介绍以下代码到底发生了什么。它目前计算 3D 环境中阴影贴图的定位和分辨率。

代码目前有效,只是想了解一些事情。

//Vertex Shader Essentials.
Position = ProjectionMatrix * ViewMatrix * WorldMatrix * vec4 (VertexPosition, 1);
Normal = (ViewMatrix * WorldMatrix * vec4 (VertexNormal, 0)).xyz;
EyeSpaceLightPosition = ViewMatrix * LightPosition;
EyeSpacePosition = ViewMatrix * WorldMatrix * vec4 (VertexPosition, 1);
STCoords = VertexST;

//What is this block of code currently doing?
ShadowCoord = ProjectionMatrix * ShadowMatrix * WorldMatrix * vec4 (VertexPosition, 1);
ShadowCoord = ShadowCoord / ShadowCoord.w;
ShadowCoord = (ShadowCoord + vec4 (1.0, 1.0, 1.0, 1.0)) * vec4 (1.0/2.0, 1.0/2.0, 1.0/2.0, 1.0);

//Alters the Shadow Map Resolution.
// Please Note - c is a slider that I control in the program execution.
float rounding = (c + 2.1) * 100.0;
ShadowCoord.x = (floor (ShadowCoord.x * rounding)) / rounding;
ShadowCoord.y = (floor (ShadowCoord.y * rounding)) / rounding;
ShadowCoord.z = (floor (ShadowCoord.z * rounding)) / rounding;


gl_Position = Position;

最佳答案

ShadowCoord = ProjectionMatrix * ShadowMatrix * WorldMatrix * vec4 (VertexPosition, 1);

这会计算该顶点在光眼空间内的位置。您正在重新计算的是 Position = ProjectionMatrix * ViewMatrix * WorldMatrix * vec4 (VertexPosition, 1); 行必须在您渲染到阴影缓冲区时产生的结果。

ShadowCoord = ShadowCoord / ShadowCoord.w;  

这应用了透视投影,确定阴影坐标应落在光的视平面上的哪个位置。

这样想:从光的角度来看,(1, 1, 1) 处的坐标应该与 (2, 2, 2) 处的坐标出现在同一点上。对于这两者,您应该在深度缓冲区上采样相同的 2d 位置。除以 w 即可。

ShadowCoord = (ShadowCoord + vec4 (1.0, 1.0, 1.0, 1.0)) * vec4 (1.0/2.0, 1.0/2.0, 1.0/2.0, 1.0);

这也与在正确的位置进行采样有关。上面的投影在光的视野中心有一个东西——例如。 (0, 0, 1) — 结束于 (0, 0)。但是 (0, 0) 是光照贴图的左下角,而不是中心。这条线确保光照贴图覆盖光的投影空间中从 (-1, -1) 到 (1, 1) 的区域。

...所以,总的来说,代码是关于从描述从光到光空间中的点的 vector 的 3d vector 到描述点落在光的 View 平面上的位置的 2d vector 的映射 - the为生成深度图而渲染的平面。

关于c++ - 阴影贴图定位和分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29924889/

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