问题描述
我正在使用 PyOpenGL 学习着色器和 opengl 图形,并且我正在尝试将阴影映射实现到我的程序,我已经完成了 https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping 中的方法并检查了其他来源,但似乎无法准确获取为什么它会在阴影中渲染所有内容。
由于阴影贴图被正确渲染并且算法有效,我的猜测是纹理的坐标不知何故搞砸了或超过了 [0,1] 的范围。
对于矩阵,我正在使用 pyrr 模块,这可能会弄乱坐标,但我不太确定,因为它非常适合顶点坐标。
我的顶点着色器:
# version 330
layout(location = 0) in vec3 a_position;
layout(location = 1) in vec2 a_texture;
layout(location = 2) in vec3 a_normal;
layout(location = 3) in vec3 a_color;
uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 lightPos;
uniform mat4 light_projection;
uniform mat4 light_view;
out vec2 v_texture;
out vec3 frag_normal;
out vec3 v_color;
out vec4 shadowCoord;
out vec3 frag_pos;
void main()
{
frag_normal = (normalize(model * vec4(a_normal,0.0f))).xyz;
frag_pos = vec3(model * vec4(a_position,1.0));
shadowCoord = light_projection * light_view * vec4(frag_pos,1.0);
gl_Position = projection * view * vec4(frag_pos,1.0);
v_texture = a_texture;
v_color = a_color;
}
还有我的片段着色器:
# version 330
in vec2 v_texture;
in vec3 frag_normal;
in vec4 shadowCoord;
in vec3 v_color;
uniform sampler2D s_texture;
uniform sampler2D shadowMap;
uniform mat4 light_pos;
uniform int switcher;
out vec4 out_color;
void main()
{
//shadow mapping (beta)
vec3 projCoords = shadowCoord.xyz / shadowCoord.w;
projCoords = projCoords * 0.5 + 0.5;
float shadow = texture(shadowMap,projCoords.xy).z < projCoords.z ? 0.0 : 1.0;
//ambient light
vec3 ambientlightintensity = vec3(0.5f,0.65f,0.8f);
//sunlight
vec3 sunlightcolor = vec3(0.7f,0.7f,0.5f);
vec3 sunlightdirection = vec3(-0.5f,0.5f,1.0f);
vec3 sunlightintensity = sunlightcolor * max(dot(frag_normal,sunlightdirection),0.0f);
vec4 texel = texture(s_texture,v_texture);
vec3 sunlight = shadow * sunlightintensity;
vec3 lightintensity = ambientlightintensity + sunlight;
//output
if (switcher == 0){
out_color = vec4(texel.rgb * lightintensity,texel.a);
}
else if (switcher == 1){
out_color = vec4(vec4(v_color,1.0).rgb * lightintensity,1.0);
}
}
渲染图
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)