新建工程复用之前的属性页
任务1:实现绕Z轴旋转的变换矩阵,并返回
找到旋转公式
给的是 angle,所以转为弧度制,选 cos 与sin 构造矩阵
float pi = acos(-1);
auto sinValue = sin(rotation_angle / 180.0 * pi);
auto cosValue = cos(rotation_angle / 180.0 * pi);
model << cosValue, -sinValue, 0, 0,
sinValue, cosValue, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1;
变换矩阵之后,第二个任务是使用给定的参数逐个元素地构建透视投影矩阵并返回该矩阵。 构建透视矩阵,需要正交矩阵,和透视->正交矩阵 透视矩阵=正交矩阵(透视->正交) 输入数据是视锥 宽高比,远和近 1.正交矩阵需要6个数据(),远近高低左右,所以需要先求出6个数据(给出了4个,根据视锥和长宽比求出6个) far near bottom top right left
根据 l r b t f n关系求出
2.然后透视矩阵需要 远近和正交矩阵的数据(透视矩阵=正交矩阵*透视->正交) 代码参考:
// TODO: Copy-paste your implementation from the previous assignment.
//tan 角度转弧度
float top = tan((eye_fov / 2)/ 180.0 * acos(-1)) * fabs(zNear);
float right = aspect_ratio * top;
float left = -right;
float bottom = -top;
Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
Eigen::Matrix4f orthographic;
Eigen::Matrix4f orthographic_trans;
Eigen::Matrix4f orthographic_scale;
Eigen::Matrix4f persp_to_ortho;
//正交位移矩阵
orthographic_trans <<
1, 0, 0, -(right + left) / 2,
0, 1, 0, -(top + bottom) / 2,
0, 0, 1, -(zNear + zFar) / 2,
0, 0, 0, 1;
//正交缩放矩阵
orthographic_scale <<
2 / (right - left), 0, 0, 0,
0, 2 / (top - bottom), 0, 0,
0, 0, 2 / (zNear - zFar), 0,
0, 0, 0, 1;
//正交矩阵
orthographic << orthographic_scale * orthographic_trans;
//透视->正交矩阵
persp_to_ortho <<
zNear, 0, 0, 0,
0, zNear, 0, 0,
0, 0, zNear + zFar, -zNear * zFar,
0, 0, 1, 0;
//透视矩阵
projection = orthographic * persp_to_ortho;
return projection;
结果: 三角形倒置
修改eye_pos 为0,0,-7 即可看到正三角,或者将zNear=-zNear,ZFar=-ZFar
三角形倒置原因:
远近平面反了, 如图可以看到 代码中以0.1为近平面,50为远平面,对于 5 这个camera位置来说,并不对(0.1是近平面50远平面),对于 -7这个位置就是正确的了,所以-7可以,但是相机位置近了,所以三角形会变大
同理,将zNear=-zNear,ZFar=-ZFar,即改-0.1为近平面,-50为远平面,显然这对于 5 是正确的,三角形也可正过来
那么交换0.1与50可以么?不行。。