在某些情况下,我们需要自己实现透视投影和正射投影的函数,那么根据前一篇转载的博客,OpenGL投影矩阵的推导,我们很容易写出glFrustum和glOrtho函数的实现。
glFrustum的函数实现如下:
void MyFrustum (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar) { GLdouble matProj[16]; memset(matProj,0,sizeof(GLdouble)*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; glMultTransposeMatrixd(matProj); }
glOrtho函数实现如下:
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; glMultTransposeMatrixd(matProj); }
希望对大家有用