Games101 作业1 旋转与投影
作业简述
作业要求得到一个三角形不同角度的图像。
作业给出了代码框架,框架包含一个画三形的函数和一个简单的光栅器。
我们仅需要实现主函数的部分代码(关于旋转和透视投影这一部分)。
特别说明
在实现作业时发现框架并没有给出cmath库,但仍能使用三角函数,于是给出了下述的实验代码:
// #include "Triangle.hpp"
// #include "rasterizer.hpp"
#include <eigen3/Eigen/Eigen>
#include <iostream>
// #include <opencv2/opencv.hpp>int main(){std::cout << sin(30.0) << std::endl;return 0;
}
通过实验得出,注释掉#include <eigen3/Eigen/Eigen>后无法用三角函数,因此确定eigen3中有实现三角函数的函数。
作业代码
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 ang_rat = rotation_angle / 180.0 * MY_PI;Eigen::Matrix4f rotating_of_Z;rotating_of_Z << cos(ang_rat), -sin(ang_rat), 0.0f, 0.0f,sin(ang_rat), cos(ang_rat) , 0.0f, 0.0f,0.0f , 0.0f , 0.0f, 0.0f,0.0f , 0.0f , 0.0f, 1.0f;model = model * rotating_of_Z;return model;
}Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,float zNear, float zFar)
{// Students will implement this functionEigen::Matrix4f projection = Eigen::Matrix4f::Identity();// TODO: Implement this function// Create the projection matrix for the given parameters.// Then return it.// projection MatrixEigen::Matrix4f projection_Matrix;projection_Matrix << zNear , 0.0f , 0.0f , 0.0f ,0.0f , zNear , 0.0f , 0.0f ,0.0f , 0.0f , zNear + zFar , -zNear * zFar ,0.0f , 0.0f , 1.0f , 0.0f ;float l, r, t, b, n, f;float ang_rat_fov = eye_fov / 180.0f * MY_PI;t = zNear * tan(ang_rat_fov / 2.0f);b = -t;r = t * aspect_ratio;l = -r;n = zNear;f = zFar;// Orthographic MatrixEigen::Matrix4f Ort_tra_mat;Ort_tra_mat << 1.0f , 0.0f , 0.0f , -1.0f * (r + l) / 2.0f ,0.0f , 1.0f , 0.0f , -1.0f * (t + b) / 2.0f ,0.0f , 0.0f , 1.0f , -1.0f * (n + f) / 2.0f ,0.0f , 0.0f , 0.0f , 1.0f ;Eigen::Matrix4f Ort_rat_mat;Ort_rat_mat << 2.0f / (r - l), 0.0f , 0.0f , 0.0f ,0.0f , 2.0f / (t - b) , 0.0f , 0.0f ,0.0f , 0.0f , 2.0f / (n - f) , 0.0f ,0.0f , 0.0f , 0.0f , 1.0f ;projection = Ort_rat_mat * Ort_tra_mat * projection_Matrix * projection;return projection;
}
有几个需要注意的点
- 代码框架中π\piπ已给出
- 在透视投影中给出的参数解释如下
- 在我的代码中,正交投影的平移和放缩是分开求的
- 整数后加f的语法会报错