我正在嘗試使用立方體貼圖爲點光源創建陰影。OpenGL陰影立方體貼圖 - 查找問題
我發現了各種幫助我的教程(Pointers on modern OpenGL shadow cubemapping?,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。
有沒有人知道我做錯了什麼?
你試圖只返回'dist'或'curr_fragment_dist_to_light',看看它如何影響你的現場? – Grimmy
是的。 'curr_fragment_dist_to_light'很好 - 離光線越遠越好。問題是'dist' - 立方體映射查找。它總是1.0(或者至少是一個非常高的值)並且總是不變的。 這是生成的陰影貼圖:[http://oi40.tinypic.com/6gcwte.jpg](http://oi40.tinypic.com/6gcwte.jpg)(我切換到背面剔除截圖,通常我使用正面剔除渲染陰影貼圖。) –