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;
}

最新文章

  1. JavaScript闭包的底层运行机制
  2. Jenkins进阶系列之——03parameterized-trigger插件
  3. BZOJ-1066 蜥蜴 最大流+拆点+超级源超级汇
  4. 解决PHP在IE浏览器下载文件,中文文件名乱码问题
  5. iOS开发中的远程推送实现(最新,支持iOS9)
  6. C语言学习_查找三分之二
  7. 小团队git开发模式
  8. [转]使用Openssl的AES加密算法
  9. ubuntu系统的teamviewer的安装及使用
  10. 【JVM虚拟机】(3)---垃圾回收器
  11. SQLSERVER 聚集一个表的字段2008及以后,要求支持XML
  12. JavaScript基本概念
  13. windows环境在本地配nginx
  14. mysql 文件操作 表
  15. ProtoBuf3.3 安装记录
  16. python3笔记<一>基础语法
  17. C#Redis Sorted-Sets
  18. Android文件系统编译出错记录
  19. git format-patch 用法【转】
  20. Mac OS 10.12 - 在VMwear Workstation12.5.2中大写键和中英文输入法的切换!

热门文章

  1. ViewModel在MVC3中的应用:实现多字段表格的部分更新
  2. 探讨mvc下linq多表查询使用viewModel的问题
  3. FragmentActivity和Activity的区别
  4. Java Main Differences between Java and C++
  5. myscroll
  6. PHP的压力测试工具ab.exe 和mpm介绍提高并发数
  7. 第四篇 SQL Server代理配置数据库邮件
  8. 使用Proj.Net创建空间参考
  9. iOS NSUserDefaults的基本使用
  10. NSNotificationCenter