视口变换

img

img

作业1 基础版

//视图转换
Eigen::Matrix4f get_view_matrix(Eigen::Vector3f eye_pos)
{
    // 定义 4 * 4 的单位矩阵
    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;
}

透视投影变换矩阵

1 .将视锥压缩成正交投影变换矩阵可处理的形式

img

2 .正交投影

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 
// 绕 z 轴旋转的变换矩阵
Eigen::Matrix4f get_model_matrix(float rotation_angle)
{
// 初始化一个单位矩阵model
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.
// --------------------
float r = rotation_angle * MY_PI / 180; //转化为弧度
Eigen::Matrix4f rotation;

// 围绕z轴进行旋转
rotation << cos(rotation_angle), -sin(rotation_angle), 0, 0,
sin(rotation_angle), cos(rotation_angle), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1;

model *= rotation;

// --------------------
return model;

}

//eye_fov:纵向视野(度), aspect_ratio:宽高比
Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
float zNear, float zFar)
{

Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();

// TODO: Implement this function
// Create the projection matrix for the given parameters.
// Then return it.
Eigen::Matrix4f M_perspToOrtho(4, 4);
Eigen::Matrix4f M_ortho_scale(4, 4);
Eigen::Matrix4f M_ortho_trans(4, 4);

float half_angle = eye_fov * MY_PI / 360.0; // 纵向视野的一半,eye_fov*MY_PI/2 / 180.0
float top = zNear * tan(half_angle);
float bottom = -top;
float right = top * aspect_ratio;
float left = -right;

M_ortho_trans << 1, 0, 0, -(right + left) / 2,
0, 1, 0, -(top + bottom) / 2,
0, 0, 1, -(zNear + zFar) / 2,
0, 0, 0, 1;
M_ortho_scale << 2 / (right - left), 0, 0, 0,
0, 2 / (top - bottom), 0, 0,
0, 0, 2 / (zNear - zFar), 0,
0, 0, 0, 1;
M_perspToOrtho << zNear, 0, 0, 0,
0, zNear, 0, 0,
0, 0, zNear + zFar, -zNear * zFar,
0, 0, 1, 0;

Eigen::Matrix4f M_ortho = M_ortho_scale *M_ortho_trans;

projection = M_ortho * M_perspToOrtho;

return projection;
}

效果(测试数据改了改)

img

img