hw1

总的来说,hw1是比较简单的.
本次作业的任务是填写一个旋转矩阵和一个透视投影矩阵,只要记得上课时候给的公式,写进去就行.

  • 旋转矩阵
    注意,cos,sin等,需要的参数为弧度制(rad),而不是角度制
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    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 / 180 * MY_PI;
    Eigen::Matrix4f rotate;
    rotate << 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 = rotate * model;
    return model;
    }

先进行挤压,再进行正交投影得到结果

  • persp2ortho

    • 透视投影到正交投影的变化矩阵
  • ortho

    • 正交矩阵
      projection = ortho * persp2ortho * projection;
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      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();
      Eigen::Matrix4f ortho = Eigen::Matrix4f::Identity();

      float top = abs(zNear) * tan(eye_fov / 2 / 180 * MY_PI);
      float bottom = -top;
      float right = top * aspect_ratio;
      float left = -right;

      persp2ortho << zNear, 0, 0, 0, 0, zNear, 0, 0, 0, 0, zNear + zFar,
      -zNear * zFar, 0, 0, 1, 0;

      ortho << 2 / (right - left), 0, 0, -(right + left) / (right - left), 0,
      2 / (top - bottom), 0, -(top + bottom) / (top - bottom), 0, 0,
      2 / (zNear - zFar), -(zNear + zFar) / (zNear - zFar), 0, 0, 0, 1;

      projection = ortho * persp2ortho * projection;
      return projection;
      }

最后得到的结果与官方答案不同,图形倒转了.这是zNear值导致的.
根据课上的内容,看向的是Z轴负半轴,Z<0; 而代码中,看向Z正半轴,Z>0;

小问题懒得修改了.

顺便学习了一下cmake的相关知识
[[07archive/tech/cmake|cmake]]