Implementation of projection transformation functions in OpenGL and opengl projection transformation Functions
In some cases, we need to implement the Perspective Projection and normal projection functions by ourselves. Then, based on the previous blog reposted, the derivation of the OpenGL projection matrix, we can easily write the implementation of glFrustum and glOrtho functions.
The glFrustum function is implemented as follows:
void MyFrustum (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar){GLdouble matProj[16];memset(matProj,0,sizeof(double)*16);matProj[0] = 2*zNear/(right-left);matProj[2] = (right+left)/(right-left);matProj[5] = 2*zNear/(top-bottom);matProj[6] = (top+bottom)/(top-bottom);matProj[10] = -(zFar+zNear)/(zFar-zNear);matProj[11] = -2*zNear*zFar/(zFar-zNear);matProj[14] = -1;glMultMatrixd(matProj);}
The glOrtho function is implemented as follows:
void MyOrtho (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar){GLdouble matProj[16];memset(matProj,0,sizeof(double)*16);matProj[0] = 2/(right-left);matProj[3] = (right+left)/(right-left);matProj[5] = 2/(top-bottom);matProj[7] = (top+bottom)/(top-bottom);matProj[10] = -2/(zFar-zNear);matProj[11] = -(zFar+zNear)/(zFar-zNear);matProj[15] = 1;glMultMatrixd(matProj);}
Hope to be useful to everyone