Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function for perspective projection of a matrix in C++

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;
}
like image 879
Disband Avatar asked Sep 14 '25 19:09

Disband


1 Answers

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);
}
like image 194
William Knight Avatar answered Sep 16 '25 09:09

William Knight