反射矩陣的推導

前幾天寫了一篇unity官方例子的鏡面反射的博文(鏈接:http://blog.csdn.net/lj820348789/article/details/48139841),其中的攝像機鏡面矩陣只是提示了官方代碼的位置,對於反射矩陣,已經有許多相關的文摘,所以我也不說這是原創,只是自己的一番重新推導,也相當於是做個讀書筆記。

我們先來看看代碼裏的反射矩陣時怎麼樣的:

static Matrix4x4 CalculateReflectionMatrix (Matrix4x4 reflectionMatVector4 plane
{
reflectionMat.m00 = (1.0F - 2.0F*plane[0]*plane[0]);
        reflectionMat.m01 = (   - 2.0F*plane[0]*plane[1]);
        reflectionMat.m02 = (   - 2.0F*plane[0]*plane[2]);
        reflectionMat.m03 = (   - 2.0F*plane[3]*plane[0]);
    
        reflectionMat.m10 = (   - 2.0F*plane[1]*plane[0]);
        reflectionMat.m11 = (1.0F - 2.0F*plane[1]*plane[1]);
        reflectionMat.m12 = (   - 2.0F*plane[1]*plane[2]);
        reflectionMat.m13 = (   - 2.0F*plane[3]*plane[1]);
    
        reflectionMat.m20 = (   - 2.0F*plane[2]*plane[0]);
        reflectionMat.m21 = (   - 2.0F*plane[2]*plane[1]);
        reflectionMat.m22 = (1.0F - 2.0F*plane[2]*plane[2]);
        reflectionMat.m23 = (   - 2.0F*plane[3]*plane[2]);
    
        reflectionMat.m30 = 0.0F;
        reflectionMat.m31 = 0.0F;
        reflectionMat.m32 = 0.0F;
        reflectionMat.m33 = 1.0F;
           
        return reflectionMat;

}

plane表示反射鏡面的平面方程組的abcd(ax + by + cz + d = 0,abc分別表示平面法向量的xyz軸上的分量)。

上面的代碼表示了這樣的一個矩陣:




先求出點v(x, y, z)在平面上的距離:

    設平面上存在點v0(x0, y0, z0),空間中存在的點v(x, y, z), v的鏡像點v1(x1, y1, z1),則有v - v0 = (x - x0, y - y0, z - z0),而平面的法向量爲n(a, b, c),則有點v到平面的距離爲dis = (v - v0)n = nv - nv0;v0滿足平面方程,由平面方程nv0 + d = 0可知nv0 = -d.於是dis = n·v+d.

得到距離後,並且又有平面法向量,所以鏡像點的位置爲

v1 = -2n*dis+v; 

v1=-2n*(n·v+d)+v;

(x1, y1, z1) = (x, y, z) - 2n(nx*x + ny*y + nz*z + d)   //nx,ny,nx爲n的xyz分量

於是就有:

x1 = x - 2n(nx*x+ ny*y + nz*z) -2nd;

y1 = y - 2n(nx*x+ ny*y + nz*z) -2nd;
z1 = z - 2n(nx*x+ ny*y + nz*z) -2nd;

就得到上面的矩陣了


y1 = y - 2n(nx*x+ ny*y + nz*z) -2nd;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章