美文网首页
Games101:作业1笔记

Games101:作业1笔记

作者: BIMnotes | 来源:发表于2021-12-17 18:03 被阅读0次

实现模型和投影变换的矩阵

模型变换

只需要处理绕z轴旋转的变换,需要注意输入的角度非弧度。

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.
    auto angle = to_radian(rotation_angle);
    model << cos(angle), -sin(angle), 0, 0,
        sin(angle), cos(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.
    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;

    auto t = sin(aspect_ratio / 2) * zNear;
    auto b = -t;
    auto r = t * aspect_ratio;
    auto l = -r;

    Eigen::Matrix4f orthoProjection = Eigen::Matrix4f::Identity();
    orthoProjection << 2 / (r - l), 0, 0, 0,
        0, 2 / (t - b), 0, 0,
        0, 0, 2 / (zNear - zFar), 0,
        0, 0, 0, 1;

    projection = orthoProjection * persp2ortho;

    return projection;
}

相关文章

网友评论

      本文标题:Games101:作业1笔记

      本文链接:https://www.haomeiwen.com/subject/tkscmktx.html