反射矩阵的推导

前几天写了一篇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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章