个人觉得难点在n,f,t,r,l,b的取值上,因为z是负半轴,所以n.f不能直接取值nNear.nFar,要取负值。同时t,r分别代表高和宽(一半)。t可用n的tan求解,r用t*aspect_ratio(aspect_ratio=width/height)。l,b则分别是e,t的负值(对称)
Eigen::Matrix4f get_model_matrix(float rotation_angle)
{
Eigen::Matrix4f model = Eigen::Matrix4f::Identity();
float angle = (rotation_angle/180.0) * MY_PI;
Eigen::Matrix4f i;
i <<
cos(angle), -sin(angle), 0, 0,
sin(angle), cos(angle), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1;
model = model * i;
return model;
}
Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
float zNear, float zFar)
{
Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
float angle_ = (eye_fov/180.0/2) * MY_PI;
float n = -1 * zNear;
float f = -1 * zFar;
float t = zNear * std::tan(angle_);//height
float r = t * aspect_ratio;//width
float l = (-1) * r;
float b = (-1) * t;
Eigen::Matrix4f j,k,h;
j <<
n, 0, 0, 0,
0, n, 0, 0,
0, 0, n + f, -1 * n * f,
0, 0, 1, 0;
k <<
1, 0, 0, -(r+l)/2,
0, 1, 0, -(t+b)/2,
0, 0, 1, -(zNear + zFar)/2,
0, 0, 0, 1;
h <<
2/(r-l), 0, 0, 0,
0, 2/(t-b), 0, 0,
0, 0, 2/(zNear - zFar), 0,
0, 0, 0, 1;
projection = projection * j * k * h;
return projection;
}
参考文章:games101 作业1 的一些思考