Implementation of projection transformation functions in OpenGL and opengl projection transformation Functions

Source: Internet
Author: User

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


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.