OpenGL中两种计算投影矩阵的函数

时间:2022-09-09 14:34:23

OpenGL无意间同时看到两种创建投影矩阵的写法,可以说它们完成的是同样的功能,但写法完全不同,可以观摩一下什么叫做异曲同工之妙...

第一种:

gltMakeShadowMatrix函数是重点

 // Gets the three coefficients of a plane equation given three points on the plane.
void gltGetPlaneEquation(GLTVector3 vPoint1, GLTVector3 vPoint2, GLTVector3 vPoint3, GLTVector3 vPlane)
{
// Get normal vector from three points. The normal vector is the first three coefficients
// to the plane equation...
gltGetNormalVector(vPoint1, vPoint2, vPoint3, vPlane); // Final coefficient found by back substitution
vPlane[] = -(vPlane[] * vPoint3[] + vPlane[] * vPoint3[] + vPlane[] * vPoint3[]);
} void gltMakeShadowMatrix(GLTVector3 vPoints[], GLTVector4 vLightPos, GLTMatrix destMat)
{
GLTVector4 vPlaneEquation;
GLfloat dot; gltGetPlaneEquation(vPoints[], vPoints[], vPoints[], vPlaneEquation); // Dot product of plane and light position
dot = vPlaneEquation[]*vLightPos[] +
vPlaneEquation[]*vLightPos[] +
vPlaneEquation[]*vLightPos[] +
vPlaneEquation[]*vLightPos[]; // Now do the projection
// First column
destMat[] = dot - vLightPos[] * vPlaneEquation[];
destMat[] = 0.0f - vLightPos[] * vPlaneEquation[];
destMat[] = 0.0f - vLightPos[] * vPlaneEquation[];
destMat[] = 0.0f - vLightPos[] * vPlaneEquation[]; // Second column
destMat[] = 0.0f - vLightPos[] * vPlaneEquation[];
destMat[] = dot - vLightPos[] * vPlaneEquation[];
destMat[] = 0.0f - vLightPos[] * vPlaneEquation[];
destMat[] = 0.0f - vLightPos[] * vPlaneEquation[]; // Third Column
destMat[] = 0.0f - vLightPos[] * vPlaneEquation[];
destMat[] = 0.0f - vLightPos[] * vPlaneEquation[];
destMat[] = dot - vLightPos[] * vPlaneEquation[];
destMat[] = 0.0f - vLightPos[] * vPlaneEquation[]; // Fourth Column
destMat[] = 0.0f - vLightPos[] * vPlaneEquation[];
destMat[] = 0.0f - vLightPos[] * vPlaneEquation[];
destMat[] = 0.0f - vLightPos[] * vPlaneEquation[];
destMat[] = dot - vLightPos[] * vPlaneEquation[];
}
 // Given three points on a plane in counter clockwise order, calculate the unit normal
void gltGetNormalVector(const GLTVector3 vP1, const GLTVector3 vP2, const GLTVector3 vP3, GLTVector3 vNormal)
{
GLTVector3 vV1, vV2; gltSubtractVectors(vP2, vP1, vV1);
gltSubtractVectors(vP3, vP1, vV2); gltVectorCrossProduct(vV1, vV2, vNormal);
gltNormalizeVector(vNormal);
}

第二种:

CreateShadowMatrix函数是重点

 第二种
/** 创建投射矩阵 */
void CPlanarShadow::CreateShadowMatrix(float m[], Vector3 point, Vector3 normal, float lp[])
{
/** 计算顶点到平面的距离 */
float d = - ((normal.x * point.x) + (normal.y * point.y) + (normal.z * point.z)); /** 计算光源向量和法向量的点积 */
float dot = normal.x*lp[] + normal.y*lp[] + normal.z*lp[] + d*lp[]; /** 设置矩阵元素值 */
m[] = dot - lp[]*normal.x; m[] = -lp[]*normal.x; m[] = -lp[]*normal.x; m[] = -lp[]*normal.x;
m[] = -lp[]*normal.y; m[] = dot -lp[]*normal.y; m[] = -lp[]*normal.y; m[] = -lp[]*normal.y;
m[] = -lp[]*normal.z; m[] = -lp[]*normal.z; m[] = dot - lp[]*normal.z; m[] = -lp[]*normal.z;
m[] = -lp[]*d; m[] = -lp[]*d; m[] = -lp[]*d; m[] = dot -lp[]*d;
}