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.
rotation_angle = rotation_angle * MY_PI / 180.0;
Eigen::Matrix4f rotation;
rotation << cos(rotation_angle), -sin(rotation_angle), 0, 0,
sin(rotation_angle), cos(rotation_angle), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1;
model *= rotation;
return model;
}
Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
float zNear, float zFar)
{
// Students will implement this function
Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
// TODO: Implement this function
// Create the projection matrix for the given parameters.
// Then return it.
Eigen::Matrix4f M_ortho_trans, M_ortho_scale, M_persp2ortho;
float angle = eye_fov * MY_PI / 180.0;
float height = zNear * tan(angle) * 2;
float width = height * aspect_ratio;
float t = -zNear * tan(angle / 2);
float r = t * aspect_ratio;
float l = -r;
float b = -t;
M_persp2ortho << zNear, 0, 0, 0,
0, zNear, 0, 0,
0, 0, zNear + zFar, -zNear * zFar,
0, 0, 1, 0;
M_ortho_scale << 2 / (r - l), 0, 0, 0,
0, 2 / (t - b), 0, 0,
0, 0, 2 / (zNear - zFar), 0,
0, 0, 0, 1;
M_ortho_trans << 1, 0, 0, -(r + l) / 2,
0, 1, 0, -(t + b) / 2,
0, 0, 1, -(zNear + zFar) / 2,
0, 0, 0, 1;
projection *= M_ortho_scale * M_ortho_trans * M_persp2ortho;
return projection;
}
绕任意轴旋转:
Eigen::Matrix4f get_rotation(Vector3f axis, float angle)
{
Eigen::Matrix4f rotation = Eigen::Matrix4f::Identity();
angle = angle / 180.0 * MY_PI;
float x = axis[0], y = axis[1], z = axis[2];
float l = sqrt(x * x + y * y + z * z);
x /= l;
y /= l;
z /= l;
float oneMCos = 1 - cos(angle);
float xx = x * x;
float yy = y * y;
float zz = z * z;
rotation << cos(angle) + xx * oneMCos, x * y * oneMCos - z * sin(angle), x * z * oneMCos + y * sin(angle), 0,
y * x * oneMCos + z * sin(angle), cos(angle) + yy * oneMCos, y * z * oneMCos - x * sin(angle), 0,
z * x * oneMCos - y * sin(angle), z * y * oneMCos + x * sin(angle), cos(angle) + zz * oneMCos, 0,
0, 0, 0, 1;
return rotation;
}