视图矩阵 是 相机世界矩阵的逆矩阵;
位于相机坐标系里的点,乘以相机世界矩阵,即转到了世界坐标系里;
位于世界坐标系里的点,乘以视图矩阵(相机世界矩阵的逆矩阵),即转到了相机坐标系里;
在相机坐标系里,相机对着-z轴;
下面方法构造出来的矩阵是相机世界矩阵,要想构造视图矩阵,得求逆矩阵,见这里;
Matrix44f lookAt(const Vec3f& from, const Vec3f& to, const Vec3f& tmp = Vec3f(0, 1, 0))
{
Vec3f forward = normalize(from - to);
Vec3f right = crossProduct(normalize(tmp), forward);
Vec3f up = crossProduct(forward, right);
Matrix44f camToWorld;
camToWorld[0][0] = right.x;
camToWorld[0][1] = right.y;
camToWorld[0][2] = right.z;
camToWorld[1][0] = up.x;
camToWorld[1][1] = up.y;
camToWorld[1][2] = up.z;
camToWorld[2][0] = forward.x;
camToWorld[2][1] = forward.y;
camToWorld[2][2] = forward.z;
camToWorld[3][0] = from.x;
camToWorld[3][1] = from.y;
camToWorld[3][2] = from.z;
return camToWorld;
}