games101 作业1


本次作业基本版只需要实现mp变换

非常简单的一次作业(如果不做提高题的话)

要求1:实现绕z轴旋转

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();//定义 4*4 单位矩阵

	float r = rotation_angle / 180.0 * MY_PI;//定义旋转的弧度

	Eigen::Matrix4f translate;//初始化模型变换矩阵
	translate << cos(r), -sin(r), 0, 0,
		     sin(r), cos(r),  0, 0,
		     0,      0,       1, 0,
		     0,      0,       0, 1;
				 
	model = translate * model;

	return model;
}

要求2:实现投影矩阵

按照课上教的,这一步分成两步来实现

  1. 先把frustum压成一个三维空间中的矩形体(我不太确定是不是叫这个名字)
  2. 应用正交投影
    image
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();//定义 4*4 单位矩阵

	// TODO: Implement this function
	// Create the projection matrix for the given parameters.
	// Then return it.

	Eigen::Matrix4f persp_to_ortho = Eigen::Matrix4f::Identity();//透视->正交的“挤压”矩阵”
	persp_to_ortho << zNear, 0,     0,            0,
			  0,	 zNear, 0,            0,
		          0,	 0,     zNear + zFar, -zNear*zFar,
		          0,	 0,     1,            0;

	float half_eye_fovY = eye_fov / 2 / 180.0 * MY_PI;

	float top = zNear * tan(half_eye_fovY);
	float bottom = -top;
	float right = aspect_ratio * top;
	float left = -right;//锥体已经变成了方块(长方体正方体都有可能)

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

	projection = ortho * persp_to_ortho;

	return projection;
}