gpt4 book ai didi

opengl - OpenGL 上的级联阴影贴图纹理访问错误

转载 作者:行者123 更新时间:2023-12-04 17:56:00 24 4
gpt4 key购买 nike

我正在尝试实现级联阴影贴图,当我想访问我的视锥体的每个分区的相应深度纹理时,我遇到了一个错误。

更具体地说,当我想选择正确的阴影纹理时会出现我的问题,如果我尝试下面的代码,我会得到一个像 this 中的工件图片,你正在看一个立方体的阴影,工件是切割平截头体边界之间的小点/正方形(红色代表近切,绿色代表远切)

uniform sampler2D   shadowMaps[10];         //GL_TEXTURE5

uniform float cameraFrustumZPartitionning[10];
uniform int cameraFrustumSize;

int getCSMlevel(float Z){
for(int iZ = 0 ; iZ < cameraFrustumSize; ++iZ)
if(Z < cameraFrustumZPartitionning[iZ])
return iZ;
return -1;
}


float ShadowCalculation(vec4 fragPosLightSpace[3], int shadowMapId, float NdotD) //WIP
{
int csmLevel = getCSMlevel(ClipSpacePosZ);

vec4 fragPosLS = fragPosLightSpace[csmLevel];
vec3 projCoords = fragPosLS.xyz / fragPosLS.w;

// Transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;

float closestDepth = 0.0;


if(csmLevel == -1)
return 0.0;
closestDepth = texture(shadowMaps[csmLevel], projCoords.xy).r;

return 1.0 - ((projCoords.z > closestDepth)?1.0:0.0);
}

如果我尝试这段代码,一切都很好。
uniform sampler2D   shadowMaps[10];         //GL_TEXTURE5

uniform float cameraFrustumZPartitionning[10];
uniform int cameraFrustumSize;

int getCSMlevel(float Z){
for(int iZ = 0 ; iZ < cameraFrustumSize; ++iZ)
if(Z < cameraFrustumZPartitionning[iZ])
return iZ;
return -1;
}


float ShadowCalculation(vec4 fragPosLightSpace[3], int shadowMapId, float NdotD) //WIP
{
int csmLevel = getCSMlevel(ClipSpacePosZ);

vec4 fragPosLS = fragPosLightSpace[csmLevel];
vec3 projCoords = fragPosLS.xyz / fragPosLS.w;

// Transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;

float closestDepth = 0.0;



if(csmLevel == 0)
closestDepth = texture(shadowMaps[0], projCoords.xy).r;
else if(csmLevel == 1)
closestDepth = texture(shadowMaps[1], projCoords.xy).r;
else if(csmLevel == 2)
closestDepth = texture(shadowMaps[2], projCoords.xy).r;
else
return 0.0;

return 1.0 - ((projCoords.z > closestDepth)?1.0:0.0);
}

在 GLSL 中,我们能够创建 sampler2D 数组并通过使用变量访问数组来获得正确的数组,或者我在这里犯了一个巨大的错误?

最佳答案

从 GLSL 4.50 规范:

When aggregated into arrays within a shader, samplers can only be indexed with a dynamically uniform integral expression, otherwise results are undefined.



这意味着所有片段着色器调用都应评估为使用相同的索引。您的第一个代码显然不满足这一点。在第二个版本中,执行是明确发散的,因此对于每个代码路径,索引是动态统一的。

编辑:由于您的阴影贴图可能大小相同,您可以使用 array textures反而。 (阵列纹理不是采样器阵列。)

关于opengl - OpenGL 上的级联阴影贴图纹理访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40323029/

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