mathFunction.cpp

//正交投影函数
template<typename T>
Matrix44<T> orthographic(T left, T right, T bottom, T top, T near, T far) {
Matrix44<T> result(static_cast<T>(1));

result.set(0, 0, static_cast<T>(2) / (right - left));
result.set(0, 3, -(right + left) / (right - left));
result.set(1, 1, static_cast<T>(2) / (top - bottom));
result.set(1, 3, -(top + bottom) / (top - bottom));
result.set(2, 2, -static_cast<T>(2) / (far - near));
result.set(1, 3, -(far + near) / (far - near));

return result;
}

//透视投影函数
//这里的fov是y方向的fov
template<typename T>
Matrix44<T> perspective(T fovy, T aspect, T n, T f) {
T const tanHalfFovy = std::tan(DEG2RAD(fovy / static_cast<T>(2)));

Matrix44<T> result(static_cast<T>(0));
result.set(0, 0, static_cast<T>(1) / (aspect * tanHalfFovy));
result.set(1, 1, static_cast<T>(1) / (tanHalfFovy));
result.set(2, 2, -(f + n) / (f - n));
result.set(2, 3, -static_cast<T>(2) * f * n / (f - n));
result.set(3, 2, -static_cast<T>(1));

return result;
}


//屏幕空间变换函数
template<typename T>
Matrix44<T> screenMatrix(const uint32_t& width, const uint32_t& height) {
Matrix44<T> result(static_cast<T>(1));

//x
result.set(0, 0, static_cast<T>(width) / static_cast<T>(2));
result.set(0, 3, static_cast<T>(width) / static_cast<T>(2));

//y
result.set(1, 1, static_cast<T>(height) / static_cast<T>(2));
result.set(1, 3, static_cast<T>(height) / static_cast<T>(2));

//z
result.set(2, 2, 0.5f);
result.set(2, 3, 0.5f);

return result;
}