Analysis of 2D and 3D switching of OpenGL

Source: Internet
Author: User
Recently, we encountered a small problem when playing games. Our games are made of 2D and 3D (specifically, the main menu and Council interface are 2D, but the actual fighting scene is 3D ), there is no problem switching from 2D to 3D, but the problem is that switching from 3D to 2D only works for glClear (), and other images cannot be displayed. Analysis

Recently, we encountered a small problem when playing games. Our games are made of 2D and 3D (specifically, the main menu and Council interface are 2D, but the actual fighting scene is 3D ), there is no problem switching from 2D to 3D, but the problem is that switching from 3D to 2D only works for glClear (), and other images cannot be displayed. Analysis

Recently, we encountered a small problem when playing games. Our games are made of 2D and 3D (specifically, the main menu and Council interface are 2D, but the actual fighting scene is 3D ), there is no problem switching from 2D to 3D, but the problem is that switching from 3D to 2D only works for glClear (), and other images cannot be displayed. After analysis, it seems that the view matrix is a problem, because in 3D scenarios, we call glLookAt (), gluPerspective (), and other functions multiple times, each call, the mechanism of OpenGL is to multiply the current matrix by the conversion operator, so every time we multiply it, a problem will occur if we do not convert it back in the 2D scenario.

Follow my personal homepage: http://alanzjl.sinaapp.com

There are two solutions to this problem. The first one is to solve this problem through glPushMatrix () and glPopMatrix () in the display () function. However, the problem we encountered was that there may be too many Push and Pop times in the display, resulting in the final failure of this method. Another way is to record the initial matrix in the initial state and restore the initial matrix when switching back to 2D.

Available
GlGetIntegerv (GL_VIEWPORT, & view); // viewport
Glgetdoublev( GL_MODELVIEW_MATRIX, & model); // model
Glgetdoublev( GL_PROJECTION_MATRIX, & proj); // projection
.

But we only need to get a matrix once and it is the first time, so we need to use a static int variable. Specific implementation code:

Void my_display (void) {static flagggg; if (flagggg = 0) {// only obtain the first initial matrix glGetIntegerv (GL_VIEWPORT, & view); // viewport glgetdoublev( GL_MODELVIEW_MATRIX, & model); // model glgetdoublev( GL_PROJECTION_MATRIX, & proj); // projection flagggg = 1 ;} /*************************************** *************** **************************************** * *****/if (Enter_Mode =-1) introduction (); else if (Enter_Mode = 0) main_menu (); else if (Enter_Mode = 1) {adventure (); // here is 3D} else if (Enter_Mode = 2) {// here is 2D. The previous mode switches to this mode and the glLoadMatrixd (view) needs to be restored in a matrix ); glMatrixMode (GL_MODELVIEW); glLoadMatrixd (model); glMatrixMode (GL_PROJECTION); glLoadMatrixd (proj); congress ();} else if (Enter_Mode = 3) achievement (); else if (Enter_Mode = 4) quit (); else if (Enter_Mode = 5) complete (); /*************************************** ***********************/}

Here, view, model, and proj are defined as global variables. They are:

GLint view[4];GLdouble model[16];GLdouble proj[16];

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.