码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-12-05 00:35:20      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   

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

第一种:

 gltMakeShadowMatrix函数是重点

bubuko.com,布布扣
 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 position
20     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 projection
27     // First column
28     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 column
34     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 Column
40     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 Column
46     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 }
View Code
bubuko.com,布布扣
 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     }
View Code


第二种:

CreateShadowMatrix函数是重点

 

bubuko.com,布布扣
 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 }
View Code

 

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

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://www.cnblogs.com/QQ122252656/p/4145312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!