前言

我们已经学习了如何使用矩阵变换来排列二维或三维空间中的对象。所以现在是时候通过实现一些简单的变换矩阵来获得一些实际经验了。在接下来的三次作业中,我们将去模拟一个基于CPU的光栅化渲染器的简化版本。

作业内容

填写一个旋转矩阵和一个透视投影矩阵。给定三维下三个点v0(2.0, 0.0, −2.0),v1(0.0, 2.0, −2.0),v2(−2.0, 0.0, −2.0),需要将这三个点的坐标变换为屏幕坐标并在屏幕上绘制出对应的线框三角形)在代码框架中,我们已经提供了draw_triangle函数,所以只需要去构建变换矩阵即可)。简而言之,我们需要进行模型、视图、投影、视口等变换来将三角形显示在屏幕上。在提供的代码框架中,我们留下了模型变换和投影变换的部分去完成。

要记得每次复制代码框架之后,先完成cmake编译的工作。

mkdir build
cd build
cmake ..
make

补充代码如下所示:

Eigen::Matrix4f get_model_matrix(float rotation_angle)
{
	//TODO:Implement this function
	//Create the model matrix for rotating the triangle around the Z axis.
	//Then return it.
	Eigen:Matrix4f model = Eigen::Matrix4f::Identity()
	float rotation_rad = rotation_angle * MY_PI / 180;
	float co = cos(rotation_rad),si = sin(rotation_rad);
	model << co, -si, 0, 0,
			 si, co, 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
	//TODO:Implement this function
	//Create the projection matrix for the given parameters.
	//Then return it.
	Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
	Eigen::Matrix4f scale = Eigen::Matrix4f::Identity();
	Eigen::Matrix4f perspective = Eigen::Matrix4f::Identity();
	float tb = 2 * tan(eye_fov / 2) * zNear;
	float rl = tb * aspect_ratio;
	float fn = zFar - zNear;
	scale << 2 / r1, 0, 0, 0,
			 0, 2 / tb, 0, 0,
			 0, 0, 2 / fn, 0,
			 0, 0, 0, 1:
	perspective << zNear, 0, 0, 0,
				   0, zNear, 0, 0,
				   0, 0, zNear + zFar, -zNear * zFar,
				   0, 0, 1, 0;
	projection = scale * perspective;
	return projection;
}