games101:二,视图变换(projection、 view、 model、camera)+ 作业1
视图变换(Viewing Transformation)
模型视图变换由四个变换合成:
M
=
M
m
o
d
e
l
∗
M
v
i
e
w
∗
M
p
e
r
M = M_{model} * M_{view} * M_{per}
M=Mmodel∗Mview∗Mper
模型变换(modeling tranformation)
即模型本身的旋转,平移,缩放等操作:
视口变换(viewport transformation)
投影变换(projection tranformation)
M p e r = M o r t h o ∗ M p e r s p − > o r t h o M_{per} = M_{ortho}*M_{persp->ortho} Mper=Mortho∗Mpersp−>ortho
正交投影变换(Orthographic Projection Transformation)
因此,
M
o
r
t
h
o
=
M
缩
放
∗
M
平
移
=
M_{ortho} = M_{缩放} * M_{平移} =
Mortho=M缩放∗M平移=
透视投影变换(Perspective Projection Transformation)
,因此
求第三行这里运用透视投影的一个性质:
可视空间,即本节第一张图的Frustum的前后面变换之后z坐标不变
即:
A
n
+
B
=
n
2
An + B = n^2
An+B=n2
A
f
+
B
=
f
2
Af + B = f^2
Af+B=f2
解出:A = n+f; B = −nf
因此
M
p
e
r
s
p
−
>
o
r
t
h
o
=
M_{persp->ortho} =
Mpersp−>ortho=
最后,
M
p
e
r
=
M
o
r
t
h
o
∗
M
p
e
r
s
p
−
>
o
r
t
h
o
M_{per} = M_{ortho}*M_{persp->ortho}
Mper=Mortho∗Mpersp−>ortho
作业1
Eigen::Matrix4f get_model_matrix(float rotation_angle)
{
Eigen::Matrix4f model = Eigen::Matrix4f::Identity();
Eigen::Matrix4f rotat;
float theta = rotation_angle/180.0*M_PI;
rotat << cos(theta), -1.0*sin(theta), 0,0,
sin(theta), cos(theta), 0,0,
0,0,1,0,
0,0,0,1;
model = rotat*model;
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();
float rad = (MY_PI / 180.0) * eye_fov;
float tanHalfFovy = std::tan(rad / 2);
//直接用了最终公式
projection(0,0) = 1.0 / (aspect_ratio * tanHalfFovy);
projection(1,1) = 1.0 / (tanHalfFovy);
projection(2,2) = (zFar + zNear) / (zFar - zNear);
projection(2,3) = (2.0 * zFar * zNear) / (zFar - zNear);
projection(3,2) = 1.0;
projection(3,3) = 0.0;
return projection;
}