2013-05-25 93 views
0

我正在嘗試使用立方體貼圖爲點光源創建陰影。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。

有沒有人知道我做錯了什麼?

+0

你試圖只返回'dist'或'curr_fragment_dist_to_light',看看它如何影響你的現場? – Grimmy

+0

是的。 'curr_fragment_dist_to_light'很好 - 離光線越遠越好。問題是'dist' - 立方體映射查找。它總是1.0(或者至少是一個非常高的值)並且總是不變的。 這是生成的陰影貼圖:[http://oi40.tinypic.com/6gcwte.jpg](http://oi40.tinypic.com/6gcwte.jpg)(我切換到背面剔除截圖,通常我使用正面剔除渲染陰影貼圖。) –

回答

0

您需要反轉lookup_vector的y座標。雖然渲染(我假設)你的Y座標點向上,但對於OpenGL紋理,Y點向下。場景是對稱的,所以你的紋理似乎是第一個正確的,但它需要得到體現 - 或者只是轉身你lookup_vector

vec3 new_vec = vec3(lookup_vector .x, -lookup_vector .y, lookup_vector .z); 

然後只是仰望呈現給您的陰影貼圖,你已經做了的距離:

float dist = texture(shadowMap, new_vec); 

測試對具有深度計算是這樣的:

vec3 abs_vec = abs(new_vec); 
float local_z_comp = max(abs_vec .x, max(abs_vec .y, abs_vec .z)); 
float norm_z_comp = (z_far + z_near)/
    (z_far - z_near) - (2 * z_far * z_near)/(z_far - z_near)/local_z_comp; 
float curr_fragment_dist_to_light = (norm_z_comp + 1.0) * 0.5; 

z_far是你lightRangez_near是近平面的等效物。最後但並非最不重要的,返回你的結果:

float result = 1.0; 
if (dist < curr_fragment_dist_to_light) 
    result = 0.0; 
return result; 

我基礎上,這個問題的答案,這些計算:Cubemap shadow mapping not working

+0

因爲我無法回答編輯建議:我使用samplerCube而不是samplerCubeShadow。 SamplerCube查找需要3個分量矢量(float dist = texture(shadowMap,new_vec);)。 –