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