我正在尝试使用立方体贴图为点光源创建阴影。
我找到了对我有帮助的各种教程(关于现代 OpenGL 阴影立方体映射的指针? ,https : //www.opengl.org/discussion_boards/showthread.php/169743-cubemap-shadows-for-pointlights,http: //www.cg 。 tuwien.ac.at/courses/Realtime/repetitorium/2011/OmnidirShadows.pdf , ...) 但我仍然遇到立方体贴图查找问题。
此着色器创建立方体贴图(格式:GL_DEPTH_COMPONENT,GL_UNSIGNED_BYTE):
//vertex shader
#version 330
uniform mat4 Model;
uniform mat4 View;
uniform mat4 Projection;
in vec3 vertex;
out vec3 vertexModel;
void main()
{
gl_Position = Projection * View * Model * vec4(vertex, 1.0);
vertexModel = vec3(Model * vec4(vertex, 1.0));
}
//fragment shader
#version 330
uniform vec3 lightPosition;
uniform float lightRange;
out float fragDepth;
out vec4 fragColor;
in vec3 vertexModel;
void main()
{
gl_FragDepth = length(lightPosition - vertexModel) / lightRange;
fragColor = vec4(1.0);
}
lightPosition
是灯光在世界空间中的位置,lightRange
基本上是灯光投影矩阵的 zFar。
使用 gDEBugger 调试应用程序时,生成的立方体贴图看起来不错。
主着色器中的阴影查找:
float shadowValue(int i)
{
vec3 lookup_vector = vertexModel - lightPosition;
float dist = texture(shadowMap, lookup_vector).r;
float curr_fragment_dist_to_light = length(lookup_vector) / lightRange;
float result = 1.0;
if (dist < curr_fragment_dist_to_light)
result = 0.0;
return result;
}
该函数的结果与光照计算的值相乘。问题是它总是返回 1.0。
有谁知道我做错了什么?