本次作业主要是补充两个函数,实现的目标是一个三角形绕着一个轴旋转.主要是使用课上PPT4里面涉及的内容.
主要讲解了三个矩阵,MVP.
对应在计算机里的拍照,实际上是这三步:
Model:把要成像的物体摆好位置。
View:摆好照相机的位置,对准。
Projection:成像!
它们分别对应着三种变换,这就是 MVP transformation,我们目前要做的,就是搞清楚它们是怎么变换的。
第一个模型的变化矩阵的旋转矩阵是最简单的,直接从2维度的情况推演过来就可以了.
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.
model(0,0) = cos(rotation_angle/180*acos(-1));
model(0,1) = -sin(rotation_angle/180*acos(-1));
model(1,0) = sin(rotation_angle/180*acos(-1));
model(1,1) = cos(rotation_angle/180*acos(-1));
return model;
}
升级难度的版本中,要绕任意轴角度angle来设计,这里主要是有一个罗德里格斯旋转公式.
Eigen::Matrix4f get_rotation(Eigen::Vector3f axis,float rotation_angle)
{
rotation_angle = rotation_angle/180*acos(-1);
Eigen::Matrix3f ones = Eigen::Matrix3f::Identity();
Eigen::Matrix3f temp;
temp << 0,-axis[2],axis[1],
axis[2],0,-axis[0],
-axis[1],axis[0],0;
temp = cos(rotation_angle)*ones + (1-cos(rotation_angle))*axis*axis.transpose() + sin(rotation_angle)*temp;
Eigen::Matrix4f res= Eigen::Matrix4f::Identity();
// res.topLeftCorner<3,3>() = temp;
// res.block(0,0,3,3) = temp.block(0,0,3,3);
res.block(0,0,3,3) = temp;
return res;
}
投影三步走:透视投影到正交投影,平移中心到原点,放缩到[-1,1]
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.
// return projection;
// TODO: Copy-paste your implementation from the previous assignment.
zNear *=-1;
zFar *=-1;
std::cout<<"zNear is "<<zNear<<std::endl;
std::cout<<"zFar is "<<zFar<<std::endl;
Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
Eigen::Matrix4f m;
m<<zNear,0,0,0,
0,zNear,0,0,
0,0,zNear+zFar,-zNear*zFar,
0,0,1,0;
float halve = eye_fov/2*MY_PI/180;
float top = tan(halve)*zNear;
float bottom = -top;
float right = top*aspect_ratio;
float left = -right;
Eigen::Matrix4f n,p;
n<<2/(right-left),0,0,0,
0,2/(top-bottom),0,0,
0,0,2/(zNear-zFar),0,
0,0,0,1;
p<<1,0,0,-(right+left)/2,
0,1,0,-(top+bottom)/2,
0,0,1,-(zFar+zNear)/2,
0,0,0,1;
projection = n*p*m;
return projection;
}