Games101 作业1 记录

使用CMake编译

配置文件

// CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(Rasterizer)

find_package(OpenCV REQUIRED)

set(CMAKE_CXX_STANDARD 17)

include_directories(/usr/local/include)

add_executable(Rasterizer main.cpp rasterizer.hpp rasterizer.cpp Triangle.hpp Triangle.cpp)
target_link_libraries(Rasterizer ${OpenCV_LIBRARIES})

编译步骤

$ mkdir build && cd build # 在main.cpp所在目录创建build目录
$ cmake ..
$ make -j4                # 编译程序
$ ./Rasterizer            # 运行程序
$ ./Rasterizer -r 30 image.png

完成作业

描述

  • 实现MVP变换
  • 目标
    • 正确构建模型矩阵
    • 正确构建透视投影矩阵
    • 可以在当前框架下正确运行,并能看见变换后的三角形
    • 按下A或D键时能正确旋转,能使用命令行等到旋转结果图像
    • 【提高】能围绕任何过原点轴进行旋转

实现

constexpr double MY_PI = 3.1415926;

// 视图变换
Eigen::Matrix4f get_view_matrix(Eigen::Vector3f eye_pos)
{
    Eigen::Matrix4f view = Eigen::Matrix4f::Identity();

    Eigen::Matrix4f translate;
    translate << 1, 0, 0, -eye_pos[0],
        0, 1, 0, -eye_pos[1],
        0, 0, 1, -eye_pos[2],
        0, 0, 0, 1;

    view = translate * view;

    return view;
}

// 模型变换
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 angle = rotation_angle / 180.0 * MY_PI;
    double sina = sin(angle),
           cosa = cos(angle);

    model << cosa, -sina, 0,
        sina, cosa, 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();
    Eigen::Matrix4f persp2ortho = Eigen::Matrix4f::Identity();

    Eigen::Matrix4f orthoScale = Eigen::Matrix4f::Identity();
    Eigen::Matrix4f orthoTrans = Eigen::Matrix4f::Identity();
    Eigen::Matrix4f ortho = Eigen::Matrix4f::Identity();

    // TODO: Implement this function
    // Create the projection matrix for the given parameters.
    // Then return it.
    float halfFov = eye_fov / (2 * 180.0) * MY_PI;
    float t = zNear * tan(halfFov),
          b = -t,
          r = t * aspect_ratio,
          l = -r;
          
    // 将透视投影变换为正交投影
    persp2ortho << zNear, 0, 0, 0,
        0, zNear, 0, 0,
        0, 0, zNear + zFar, -zNear * zFar,
        0, 0, 1, 0;
    // 缩放到标准立方体
    orthoScale << 2 / (r - l), 0, 0, 0,
        0, 2 / (t - b), 0, 0,
        0, 0, 2 / (zNear - zFar), 0,
        0, 0, 0, 1;
    // 移到原点
    orthoTrans << 1, 0, 0, -(r + l) / 2,
        0, 1, 0, -(t + b) / 2,
        0, 0, 1, -(zNear + zFar) / 2,
        0, 0, 0, 0;
    ortho = orthoScale * orthoTrans;
    projection = ortho * persp2ortho;

    return projection;
}

提高

  • 使用旋转公式与基向量推导旋转矩阵
Eigen::Matrix4f get_rotation(Vector3f axis, float angle)
{
    Eigen::Matrix4f model = Eigen::Matrix4f::Identity();

    // TODO: Implement this function
    // Create the model matrix for rotating the triangle around the axis.
    // Then return it.
    angle = angle / 180.0 * MY_PI;
    double sin_angle = sin(angle),
           cos_angle = cos(angle);
    double axis_x = axis.x(),
           axis_y = axis.y(),
           axis_z = axis.z();

    // I + (nhat*nhat)*(1-cos_angle)+sin_angle*nhat
    model << axis_x * axis_x * (1 - cos_angle) + cos_angle, axis_y * axis_x * (1 - cos_angle) - axis_z * sin_angle, axis_z * axis_x * (1 - cos_angle) + axis_y * sin_angle, 0,
        axis_x * axis_y * (1 - cos_angle) + axis_z * sin_angle, axis_y * axis_y * (1 - cos_angle) + cos_angle, axis_z * axis_y * (1 - cos_angle) - axis_x * sin_angle, 0,
        axis_x * axis_z * (1 - cos_angle) - axis_y * sin_angle, axis_y * axis_z * (1 - cos_angle) + axis_x * sin_angle, axis_z * axis_z * (1 - cos_angle) + cos_angle, 0,
        0, 0, 0, 1;

    return model;
}

参考链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值