Games101 Asignment01

发布时间 2023-12-03 01:22:26作者: memo2586

编写代码构建模型视图变换矩阵

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.

    float sin_rotation_angle = sin(rotation_angle / 180.0 * acos(-1));
    float cos_rotation_angle = cos(rotation_angle / 180.0 * acos(-1));
    model <<
    cos_rotation_angle, -sin_rotation_angle, 0, 0,
    sin_rotation_angle, cos_rotation_angle, 0, 0,
    0, 0, 1, 0,
    0, 0, 0, 1;

    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.

    // Matrix for M_persp -> M_troho
    Eigen::Matrix4f M_persp_troth = Eigen::Matrix4f::Identity();
    M_persp_troth << zNear, 0, 0, 0,
    0, zNear, 0, 0,
    0, 0, zNear + zFar, -(zNear * zFar),
    0, 0, 1, 0;

    // value of l,f,t,b;
    float top, bottom, left, right;
    top = tan(eye_fov / 2) * abs(zNear);
    bottom = (-1) * top;
    right = aspect_ratio * top;
    left = (-1) *right;

    // Matrix for move to origin.
    Eigen::Matrix4f M_move = Eigen::Matrix4f::Identity();
    M_move << 1, 0, 0, (-1) * (left + right) / 2,
    0, 1, 0, (-1) * (top + bottom) / 2,
    0, 0, 1, (-1) * (zNear +  zFar) / 2,
    0, 0, 0, 1;

    // Matrix for scale.
    Eigen::Matrix4f M_scale = Eigen::Matrix4f::Identity();
    M_scale <<
    2 / (right - left), 0, 0, 0,
    0, 2 / (top - bottom), 0, 0,
    0, 0, 2 / (zNear - zFar), 0,
    0, 0, 0, 1;

    // Matrix for ortho transformation.
    Eigen::Matrix4f M_ortho = Eigen::Matrix4f::Identity();
    M_ortho = M_scale * M_move;

    projection = M_ortho * M_persp_troth;

    return projection;
}