复制粘贴下面的投影矩阵函数:
Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio, float zNear, float zFar)
{
#// 初始化一个单位矩阵projection
Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
#/* 初始化一个单位矩阵persp2ortho,其作用为将透视点映射为适合
# 进行正交投影的点*/
Eigen::Matrix4f persp2ortho = Eigen::Matrix4f::Identity();
persp2ortho << zNear, 0, 0, 0,
0, zNear, 0, 0,
0, 0, zNear + zFar, -zNear * zFar,
0, 0, 1, 0;
double halfEyeRadian = eye_fov * MY_PI / 2 / 180.0;
double top = -zNear * tan(halfEyeRadian); //*zNear原本是正的,但我取-zNear求top,bottom,right,left就好了
double bottom = -top;
double right = top * aspect_ratio;
double left = -right;
Eigen::Matrix4f orthoScale = Eigen::Matrix4f::Identity();
orthoScale << 2 / (right - left), 0, 0, 0,
0, 2 / (top - bottom), 0, 0,
0, 0, 2 / (zNear - zFar), 0,
0, 0, 0, 1;
Eigen::Matrix4f orthoTrans = Eigen::Matrix4f::Identity();
orthoTrans << 1, 0, 0, -(right + left) / 2,
0, 1, 0, -(top + bottom) / 2,
0, 0, 1, -(zNear + zFar) / 2,
0, 0, 0, 1;
Eigen::Matrix4f matrixOrtho = orthoScale * orthoTrans;
projection = matrixOrtho * persp2ortho;
return projection;
}
主要是求出的摄像机nearPlane的四个方向距离top,bottom,right,left,必须全部取它的反数。
因此有了 double top = -zNear * tan(halfEyeRadian),其他3个值都是依赖top产生的,top取反,其他都会被取反。