Does anyone have a function that returns the perspective projection of a 3x3 matrix in C++?
Matrix Perspective()
{
Matrix m(0, 0, 0); // Creates identity matrix
// Perspective projection formulas here
return m;
}
Here's one that returns it in a 4x4 matrix, using the formula from the OpenGL gluPerspective man page:
static void my_PerspectiveFOV(double fov, double aspect, double near, double far, double* mret) {
double D2R = M_PI / 180.0;
double yScale = 1.0 / tan(D2R * fov / 2);
double xScale = yScale / aspect;
double nearmfar = near - far;
double m[] = {
xScale, 0, 0, 0,
0, yScale, 0, 0,
0, 0, (far + near) / nearmfar, -1,
0, 0, 2*far*near / nearmfar, 0
};
memcpy(mret, m, sizeof(double)*16);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With