Http://www.glprogramming.com/dgs.php? DG = 1
An alternative to gluperspective by dranith
Motive
Gluperspective is a very handy function for setting up the projection matrix. the only problem it has that I see is the FOV it takes is the FOV in the Y direction. so either you have no control over the FOV in the X direction, or you have the source to gluperspective and you do some fancy Trig to figure out the y FOV value you need to input to get the desired x FOV.
Both these ways suck. So heres the code that lets you specify the FOV in the X direction. Code is based off the mesa Implementation of gluperspective.
Not to mention this is a point of confusion for people new to OpenGL. I didn't even notice that it took the y FOV till a few days ago when I was doing something that I needed to be able to specify in X and things weren't working =).
Void gldperspective (gldouble fovx, gldouble aspect, gldouble znear, gldouble zfar) <br/> {<br/> // This Code is based off the mesa source for gluperspective <br/> // * Note * This assumes gl_projection is the current matrix </P> <p> gldouble xmin, xmax, ymin, Ymax; <br/> gldouble M [16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; <br/> xmax = znear * Tan (fovx * m_pi/360.0); <br/> xmin =-xmax; </P> <p> ymin = xmin/aspect; <br/> Ymax = xmax/aspect; <br/> // set up the projection matrix <br/> M (2.0) = (* znear)/(xmax-xmin); <br/> M) = (2.0 * znear)/(Ymax-ymin); <br/> M () =-(zfar + znear)/(zfar-znear ); <br/> M (0, 2) = (xmax + xmin)/(xmax-xmin); <br/> M (1, 2) = (Ymax + ymin) /(Ymax-ymin); <br/> M (1.0) =-2.0; <br/> M () =-(* zfar * znear) /(zfar-znear); </P> <p> // Add to current matrix <br/> glmultmatrixd (m); <br/>}
Notes
Usage syntax is exactly the same as gluperspective. This function assumes that gl_projection is the current matrix. This code is not optimised at all, but shoshould only need to be called once.
-- Dranith