MVP矩阵
M: model
矩阵:代表从模型的局部坐标变换到世界坐标中;
V:view
矩阵:代表从世界坐标到view(观察)坐标的转换;
P:Projection
矩阵:代表投影变换,包含正则化,即将坐标变换到[-1,1][-1,1][-1,1]立方体空间;
模型变换
将模型的局部坐标系下的点,通过缩放
、旋转
、平移
得到其在世界坐标中的顶点坐标。房间的中心,当作世界坐标,房间中的每一个物体的位置都是经过上述变换到世界坐标系下。
以绕Z轴旋转为例:
默认情况下,旋转轴经过原点,世界坐标原点
1.通过旋转向量得到对应的矩阵
2.通过基变换得到变换矩阵
二维中的坐标系变换有如下关系
将uv坐标下的P,通过变换,转换到xy世界坐标系下:
反之,将xy世界坐标变换到uv坐标下:
三维坐标系下则有:
视图变换
将世界坐标下的点转换成相机坐标系:
注:此处参考视图变换
投影变换
透视投影,可以理解:先将锥体变成立方体,就会转化成正交投影的问题
注:Game101 作业1
Eigen::Matrix4f get_view_matrix(Eigen::Vector3f eye_pos)
{
Eigen::Matrix4f view = Eigen::Matrix4f::Identity();
Eigen::Matrix4f translate;
translate << 1, 0, 0, -eye_pos[0], 0, 1, 0, -eye_pos[1], 0, 0, 1,
-eye_pos[2], 0, 0, 0, 1;
view = translate * view;
return view;
}
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;
model(0, 0) = cos(rotation_angle);
model(0, 1) = -sin(rotation_angle);
model(1, 0) = sin(rotation_angle);
model(1, 1) = cos(rotation_angle);
return model;
}
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.
double t = tan(eye_fov / 2.0) * abs(zNear);//top
double b = -t;
double r = aspect_ratio * t;//right
double l = -r;
double n = zNear;
double f = zFar;
Eigen::Matrix4f Mpro_oth;
Mpro_oth << n, 0, 0, 0,
0, n, 0, 0,
0, 0, n+f, -n*f,
0, 0, 1, 0;
Eigen::Matrix4f Moth;
Eigen::Matrix4f Mt,Ms;
Mt << 1, 0, 0, -(r + l) / 2.0,
0, 1, 0, -(t + b) / 2.0,
0, 0, 1, -(n + f) / 2.0,
0, 0, 0, 1;
Ms << 2 / (r - l), 0, 0, 0,
0, 2 / (t - b), 0, 0,
0, 0, 2 / (n - f), 0,
0, 0, 0, 1;
Moth = Ms * Mt;
projection = Moth * Mpro_oth;
return projection;
}