An alternative to gluperspective by dranith

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.