透视投影矩阵的构建
2015-04-07 21:46
603 查看
透视投影矩阵的构建
投影矩阵最终建立的是一个平截头体(也可以称为台),在这种变换下呈现远小近大的效果。这里我将我学到知识记录下来,以后备忘用。蒋彩阳原创文章,首发地址:http://blog.csdn.net/gamesdev/article/details/44926299。欢迎同行前来探讨。
首先是使用OpenGL的glFrustum函数,它要求传入的是前后、左右、上下等参数,这要求这个平截头体是轴对称的。由它构成的矩阵是为:
如果我们使用的不是glFrustum,而是glPerspetive,那么它的公式就更加简单了:
我们可以借助Qt来验证一下:
#include <math.h> #include <QDebug> #include <QCoreApplication> #include <QVector3D> #include <QMatrix4x4> qreal deg2Rad( qreal deg ) { return deg * 2 * M_PI / 180.0f; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qreal fov = 45.0; qreal aspectRatio = 16.0 / 9.0; qreal near = 0.5; qreal far = 500.0; qreal left = -near * tan( deg2Rad( fov ) / 2.0 ) * aspectRatio; qreal right = near * tan( deg2Rad( fov ) / 2.0 ) * aspectRatio; qreal bottom = -near * tan( deg2Rad( fov ) / 2.0 ); qreal top = near * tan( deg2Rad( fov ) / 2.0 ); QMatrix4x4 matrix; matrix.frustum( left, right, bottom, top, near, far ); qDebug( ) << "The normal perspective matrix is: " << matrix; QMatrix4x4 matrix_2; float* d = matrix_2.data( ); d[0] = 2 * near / ( right - left ); d[1] = 0.0; d[2] = 0.0; d[3] = 0.0; d[4] = 0.0; d[5] = 2 * near / ( top - bottom ); d[6] = 0.0; d[7] = 0.0; d[8] = ( right + left ) / ( right - left ); d[9] = ( top + bottom ) / ( top - bottom ); d[10] = -( far + near ) / ( far - near ); d[11] = -1; d[12] = 0.0; d[13] = 0.0; d[14] = -2 * far * near / ( far - near ); d[15] = 0.0; qDebug( ) << "The user manipulated matrix is: " << matrix_2; QMatrix4x4 matrix_3; d = matrix_3.data( ); d[0] = 1.0 / ( tan( deg2Rad( fov ) / 2.0 ) * aspectRatio ); d[1] = 0.0; d[2] = 0.0; d[3] = 0.0; d[4] = 0.0; d[5] = 1.0 / ( tan( deg2Rad( fov ) / 2.0 ) ); d[6] = 0.0; d[7] = 0.0; d[8] = 0.0; d[9] = 0.0; d[10] = -( far + near ) / ( far - near ); d[11] = -1; d[12] = 0.0; d[13] = 0.0; d[14] = -2 * far * near / ( far - near ); d[15] = 0.0; qDebug( ) << "Another user manipulated matrix is: " << matrix_3; return a.exec( ); }
注意:和OpenGL一样,矩阵是列主序的,所以上述公式在赋值的时候要一列一列地读,先读第一列的四个数,然后第二列……运行结果如下:
相关文章推荐
- 从零实现3D图像引擎:(13)把宽高比、透视投影矩阵、屏幕变换矩阵说透
- 由对应四点求透视投影矩阵
- 现实中“二向箔” -----透视投影变换及其投影矩阵
- OpenGL.ES在Android上的简单实践:8-曲棍球(构建冰球木槌 下 & 模型视图投影矩阵)
- (转)从零实现3D图像引擎:(13)把宽高比、透视投影矩阵、屏幕变换矩阵说透
- opengl投影透视矩阵的个人总结
- 26 WebGL的透视投影矩阵
- 平行投影与透视投影的关系与统一的矩阵表示
- opengles透视矩阵投影原理
- 投影矩阵推导遇到的问题
- 透视矩阵的推导 opengl
- 写给VR手游开发小白的教程:(四)补充篇,详细介绍Unity中相机的投影矩阵
- 利用投影算法来计算系统矩阵左乘和右乘
- 关于向量、矩阵和投影
- Android OpenGL ES(六)----进入三维在代码中创建投影矩阵和旋转矩阵
- 投影矩阵的实现以及如何从投影矩阵中获取各视裁体平面
- iOS的三维透视投影
- OpenGL 模型视图投影矩阵 仿射矩阵
- iOS的三维透视投影
- OpenGL教程翻译 第十二课 透视投影