簡體   English   中英

陰影貼圖適用於正交投影,但不適用於透視投影

[英]Shadowmap works with ortho projection, but not perspective projection

我已經實現了陰影映射,只要我使用正交投影它就可以很好地工作(例如從“太陽”獲得陰影)

然而,一旦我切換到聚光燈的透視投影,陰影貼圖就不再適合我了。 即使我使用完全相同的矩陣代碼來創建我的(工作)透視相機投影。

透視陰影貼圖是否存在一些我不知道的陷阱?

這個投影矩陣(正交)有效:

        const float projectionSize = 50.0f;

        const float left  = -projectionSize;
        const float right =  projectionSize;
        const float bottom= -projectionSize;
        const float top   =  projectionSize;

        const float r_l = right - left;
        const float t_b = top - bottom;
        const float f_n = zFar - zNear;
        const float tx = - (right + left) / (right - left);
        const float ty = - (top + bottom) / (top - bottom);
        const float tz = - (zFar + zNear) / (zFar - zNear);

        float* mout = sl_proj.data;

        mout[0] = 2.0f / r_l;
        mout[1] = 0.0f;
        mout[2] = 0.0f;
        mout[3] = 0.0f;

        mout[4] = 0.0f;
        mout[5] = 2.0f / t_b;
        mout[6] = 0.0f;
        mout[7] = 0.0f;

        mout[8] = 0.0f;
        mout[9] = 0.0f;
        mout[10] = -2.0f / f_n;
        mout[11] = 0.0f;

        mout[12] = tx;
        mout[13] = ty;
        mout[14] = tz;
        mout[15] = 1.0f;

此投影矩陣(透視)可用作相機,但無法與陰影貼圖配合使用:

        const float f = 1.0f / tanf(fov/2.0f);
        const float aspect = 1.0f;
        float* mout = sl_proj.data;

        mout[0] = f / aspect;
        mout[1] = 0.0f;
        mout[2] = 0.0f;
        mout[3] = 0.0f;

        mout[4] = 0.0f;
        mout[5] = f;
        mout[6] = 0.0f;
        mout[7] = 0.0f;

        mout[8] = 0.0f;
        mout[9] = 0.0f;
        mout[10] = (zFar+zNear) / (zNear-zFar);
        mout[11] = -1.0f;

        mout[12] = 0.0f;
        mout[13] = 0.0f;
        mout[14] = 2 * zFar * zNear /  (zNear-zFar);
        mout[15] = 0.0f;

考慮到它當然是不現實的,正射投影會按預期創建光影(見下文)。

正交投影光

要創建 lightviewprojection 矩陣,我只需將投影矩陣與光變換的逆相乘。

對於透視相機,一切正常,但不適用於透視(聚光)光。

“不工作”我的意思是:零光出現,因為不知何故,沒有一個片段落在光的視野內? (這不是紋理問題,而是轉換問題。)

這是 GLSL 代碼中由齊次 W 坐標丟失的除法的情況。

不知何故,使用正交投影,不除以 W 很好。

對於透視投影,光空間中的坐標需要除以 W。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM