Eigen::Matrix4f get_model_matrix(float rotation_angle)
{
Eigen::Matrix4f model = Eigen::Matrix4f::Identity();
// TODO: Implement this function
// Create the model matrix for rotating the triangle around the Z axis.
// Then return it.
Eigen::Matrix4f rotate;
float radian_angle = rotation_angle / 180 * MY_PI;
rotate << std::cos(radian_angle), -std::sin(radian_angle), 0, 0,
std::sin(radian_angle), std::cos(radian_angle), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1;
model = model * rotate;
return model;
}
对于任意一个点,要求有矩阵M满足: M · (x, y, z, 1)T = (nx, ny, unknown, z)T;
n是近平面距离相机的距离
Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
float zNear, float zFar)
{
Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
// TODO: Implement this function
// Create the projection matrix for the given parameters.
// Then return it.
Eigen::Matrix4f persp_matrix;//透视矩阵
Eigen::Matrix4f translate_matrix;//移动矩阵
Eigen::Matrix4f scale_matirx;//缩放矩阵
persp_matrix << zNear, 0, 0, 0,
0, zNear, 0, 0,
0, 0, zNear + zFar, -zNear * zFar,
0, 0, 1, 0;
float halfAngle = eye_fov / 2 / 180 * MY_PI;
float height = -2 * std::tan(halfAngle) * zNear;//没有负号则三角形上下颠倒
float width = height * aspect_ratio;
translate_matrix << 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, -(zNear + zFar) / 2,
0, 0, 0, 1;
scale_matirx << 2 / width, 0, 0, 0,
0, 2 / height, 0, 0,
0, 0, 2 / (zNear - zFar), 0,
0, 0, 0, 1;
projection = scale_matirx * translate_matrix * persp_matrix;
return projection;
}