最近,我一直在努力设置一个好的透视投影矩阵,并将其应用于一个简单的三角形。在我显示任何代码之前,我对矩阵顺序有一个小问题:我是否必须将我的视图矩阵与我的投影矩阵相乘或将我的投影矩阵与我的视图矩阵相乘?
现在好了代码。我已经尝试了许多不同的方法来做一个没有任何好结果的透视矩阵。
static Matrix4x4<T> Perspective_S(const T &fovy, const T &aspectRatio, const T &zNear, const T &zFar)
{
T range = tanf(fovy / 2.0f) * zNear;
return Matrix4x4<T>((2.0f * zNear) / (range * aspectRatio + range * aspectRatio), 0.0f, 0.0f, 0.0f,
0.0f, zNear / range, 0.0f, 0.0f,
0.0f, 0.0f, -(zFar + zNear) / (zFar - zNear), -1.0f,
0.0f, 0.0f, (-(2.0f * zFar * zNear) / (zFar - zNear)), 0.0f);
}
static Matrix4x4<T> Perspective_S(const T &fovy, const T &aspectRatio, const T &zNear, const T &zFar)
{
T f = 1.0f / tan(fovy / 2.0f);
return Matrix4x4<T>(f / aspectRatio, 0.0f, 0.0f, 0.0f,
0.0f, f, 0.0f, 0.0f,
0.0f, 0.0f, (zFar + zNear) / (zNear - zFar), (2.0f * zFar * zNear) / (zNear - zFar),
0.0f, 0.0f, -1.0f, 0.0f);;
}
static Matrix4x4<T> Frustum_S(const T &left, const T &right, const T &bottom, const T &top,
const T &zNear, const T &zFar)
{
return Matrix4x4<T>(2.0f * zNear / (right - left), 0.0f, 0.0f, 0.0f,
0.0f, 2.0f * zNear / (top - bottom), 0.0f, 0.0f,
(right + left) / (right - left), (top + bottom) / (top - bottom), -(zFar + zNear) / (zFar - zNear), -1.0f,
0.0f, 0.0f, -2.0f * zFar * zNear / (zFar - zNear), 0.0f);
}
static Matrix4x4<T> Perspective_S(const T &fovy, const T &aspectRation, const T &zNear, const T &zFar)
{
T scale = tan(fovy) * zNear;
T r = aspectRation * scale, l = -r;
T t = scale, b = -t;
return Frustum_S(l, r, b, t, zNear, zFar);
}
static void Perspective_S(Matrix4x4<T> &matrix, T fovyInDegrees, T aspectRatio, T znear, T zfar)
{
T ymax = znear * tanf(fovyInDegrees * 3.14159265358979323846 / 360.0); //c'est pas 180?
//ymin = -ymax;
//xmin = -ymax * aspectRatio;
T xmax = ymax * aspectRatio;
Frustum(matrix, -xmax, xmax, -ymax, ymax, znear, zfar);
}
static void Frustum_S(Matrix4x4<T> &matrix, T left, T right, T bottom, T top,
T znear, T zfar)
{
T temp = 2.0f * znear;
T temp2 = right - left;
T temp3 = top - bottom;
T temp4 = zfar - znear;
matrix = Matrix4x4<T>(temp / temp2, 0.0f, 0.0f, 0.0f,
0.0f, temp / temp3, 0.0f, 0.0f,
(right + left) / temp2, (top + bottom) / temp3, (-zfar - znear) / temp4, -1.0f,
0.0f, 0.0f, (-temp * zfar) / temp4, 0.0f);
}
有些函数看起来像我的其他一些trys的转置结果矩阵。所有这些功能都来自教程。一个人甚至来自我以前的帖子,它仍然没有工作......
万一你认为这是我的LookAt代码,这里是:
我在main.cpp中做了什么
matptr = (Matrix4x4f::LookAt_S(eye, center, up) *
Matrix4x4f::Perspective_S(M_PI / 3.0f, (float)window->getSize().x / (float)window->getSize().y, 0.001f, 1000.0f)).ToArray();
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "myMatrix"), 1, GL_FALSE, &matptr[0]);
我的LookAt代码:
static Matrix4x4<T> LookAt_S(Vector3<T> &eye, Vector3<T> ¢er, Vector3<T> &up)
{
Vector3<T> forward(center - eye);
forward.Normalize();
Vector3<T> side(forward.CrossProduct(up));
side.Normalize();
up = side.CrossProduct(forward);
return Matrix4x4<T> (side.x, up.x, -forward.x, 0.0f,
side.y, up.y, -forward.y, 0.0f,
side.z, up.z, -forward.z, 0.0f);
}