2021SC@SDUSC
目录
书接上回,本文我们来讲一下矩阵在图形变换中最重要的变换,就是怎么将3维空间的物体转换为2维的图像。
观察矩阵(View)
在一个3维场景中,物体的坐标都基于场景的世界坐标系建立,而为了将世界坐标系中的坐标投影到屏幕上,我们首先需要将物体的坐标转换到摄像机的的坐标空间,俗称观察空间。
以下的代码返回了一个观察矩阵view,它的参数有9个,分别构成3个3维向量。
OvMaths::FMatrix4 OvMaths::FMatrix4::CreateView(const float p_eyeX, const float p_eyeY, const float p_eyeZ, const float p_lookX, const float p_lookY, const float p_lookZ, const float p_upX, const float p_upY, const float p_upZ)
{
const OvMaths::FVector3 eye(p_eyeX, p_eyeY, p_eyeZ);
const OvMaths::FVector3 look(p_lookX, p_lookY, p_lookZ);
const OvMaths::FVector3 up(p_upX, p_upY, p_upZ);
const OvMaths::FVector3 forward(eye - look);
FVector3::Normalize(forward);
const OvMaths::FVector3 upXForward(OvMaths::FVector3::Cross(up, forward));
FVector3::Normalize(upXForward);
const OvMaths::FVector3 v(OvMaths::FVector3::Cross(forward, upXForward));
OvMaths::FMatrix4 View;
View.data[0] = upXForward.x;
View.data[1] = upXForward.y;
View.data[2] = upXForward.z;
View.data[3] = -OvMaths::FVector3::Dot(eye, upXForward);
View.data[4] = v.x;
View.data[5] = v.y;
View.data[6] = v.z;
View.data[7] = -OvMaths::FVector3::Dot(eye, v);
View.data[8] = forward.x;
View.data[9] = forward.y;
V