首页 > 代码库 > OpenGL中两种计算投影矩阵的函数
OpenGL中两种计算投影矩阵的函数
OpenGL无意间同时看到两种创建投影矩阵的写法,可以说它们完成的是同样的功能,但写法完全不同,可以观摩一下什么叫做异曲同工之妙...
第一种:
gltMakeShadowMatrix函数是重点
1 // Gets the three coefficients of a plane equation given three points on the plane. 2 void gltGetPlaneEquation(GLTVector3 vPoint1, GLTVector3 vPoint2, GLTVector3 vPoint3, GLTVector3 vPlane) 3 { 4 // Get normal vector from three points. The normal vector is the first three coefficients 5 // to the plane equation... 6 gltGetNormalVector(vPoint1, vPoint2, vPoint3, vPlane); 7 8 // Final coefficient found by back substitution 9 vPlane[3] = -(vPlane[0] * vPoint3[0] + vPlane[1] * vPoint3[1] + vPlane[2] * vPoint3[2]);10 }11 12 void gltMakeShadowMatrix(GLTVector3 vPoints[3], GLTVector4 vLightPos, GLTMatrix destMat)13 {14 GLTVector4 vPlaneEquation;15 GLfloat dot;16 17 gltGetPlaneEquation(vPoints[0], vPoints[1], vPoints[2], vPlaneEquation);18 19 // Dot product of plane and light position20 dot = vPlaneEquation[0]*vLightPos[0] + 21 vPlaneEquation[1]*vLightPos[1] + 22 vPlaneEquation[2]*vLightPos[2] + 23 vPlaneEquation[3]*vLightPos[3];24 25 26 // Now do the projection27 // First column28 destMat[0] = dot - vLightPos[0] * vPlaneEquation[0];29 destMat[4] = 0.0f - vLightPos[0] * vPlaneEquation[1];30 destMat[8] = 0.0f - vLightPos[0] * vPlaneEquation[2];31 destMat[12] = 0.0f - vLightPos[0] * vPlaneEquation[3];32 33 // Second column34 destMat[1] = 0.0f - vLightPos[1] * vPlaneEquation[0];35 destMat[5] = dot - vLightPos[1] * vPlaneEquation[1];36 destMat[9] = 0.0f - vLightPos[1] * vPlaneEquation[2];37 destMat[13] = 0.0f - vLightPos[1] * vPlaneEquation[3];38 39 // Third Column40 destMat[2] = 0.0f - vLightPos[2] * vPlaneEquation[0];41 destMat[6] = 0.0f - vLightPos[2] * vPlaneEquation[1];42 destMat[10] = dot - vLightPos[2] * vPlaneEquation[2];43 destMat[14] = 0.0f - vLightPos[2] * vPlaneEquation[3];44 45 // Fourth Column46 destMat[3] = 0.0f - vLightPos[3] * vPlaneEquation[0];47 destMat[7] = 0.0f - vLightPos[3] * vPlaneEquation[1];48 destMat[11] = 0.0f - vLightPos[3] * vPlaneEquation[2];49 destMat[15] = dot - vLightPos[3] * vPlaneEquation[3];50 }
1 // Given three points on a plane in counter clockwise order, calculate the unit normal 2 void gltGetNormalVector(const GLTVector3 vP1, const GLTVector3 vP2, const GLTVector3 vP3, GLTVector3 vNormal) 3 { 4 GLTVector3 vV1, vV2; 5 6 gltSubtractVectors(vP2, vP1, vV1); 7 gltSubtractVectors(vP3, vP1, vV2); 8 9 gltVectorCrossProduct(vV1, vV2, vNormal);10 gltNormalizeVector(vNormal);11 }
第二种:
CreateShadowMatrix函数是重点
1 第二种 2 /** 创建投射矩阵 */ 3 void CPlanarShadow::CreateShadowMatrix(float m[16], Vector3 point, Vector3 normal, float lp[4]) 4 { 5 /** 计算顶点到平面的距离 */ 6 float d = - ((normal.x * point.x) + (normal.y * point.y) + (normal.z * point.z)); 7 8 /** 计算光源向量和法向量的点积 */ 9 float dot = normal.x*lp[0] + normal.y*lp[1] + normal.z*lp[2] + d*lp[3];10 11 /** 设置矩阵元素值 */12 m[0] = dot - lp[0]*normal.x; m[1] = -lp[1]*normal.x; m[2] = -lp[2]*normal.x; m[3] = -lp[3]*normal.x;13 m[4] = -lp[0]*normal.y; m[5] = dot -lp[1]*normal.y; m[6] = -lp[2]*normal.y; m[7] = -lp[3]*normal.y;14 m[8] = -lp[0]*normal.z; m[9] = -lp[1]*normal.z; m[10] = dot - lp[2]*normal.z; m[11] = -lp[3]*normal.z;15 m[12] = -lp[0]*d; m[13] = -lp[1]*d; m[14] = -lp[2]*d; m[15] = dot -lp[3]*d;16 }
OpenGL中两种计算投影矩阵的函数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。