Some practical functions for learning OpenGL Based on glut

Source: Internet
Author: User

 

 

I recently started learning OpenGL again. Some practical functions I wrote during the learning process can be shared here:

1. The following functions can output some information about current OpenGL in the console (to be called after the window is created, otherwise null value will be obtained)

Void printglinfo () <br/>{< br/> const glubyte * name = glgetstring (gl_vendor); <br/> const glubyte * biaoshifu = glgetstring (gl_renderer ); <br/> const glubyte * openglversion = glgetstring (gl_version); <br/> const glubyte * gluversion = glugetstring (glu_version ); <br/> printf ("OpenGL implementation vendor name: % s/n", name); <br/> printf ("Renderer identifier: % s/n ", biaoshifu); <br/> printf ("OpenGL implementation version: % s/n", openglversion); <br/> printf ("Glu tool library version: % s/n ", gluversion); <br/>} 

2. The following function is used to specify it as a processing function for mouse events through fig (Mouse, this allows you to scale the rotation of the view image and the observed distance between the scroll observer and the view when you drag the mouse (My glut library does not support scroll events, is the http://www.cnblogs.com/dabaopku/archive/2010/07/07/1773233.html implemented by referring to the article here)

Void mouse (INT button, int state, int X, int y) <br/>{< br/> switch (button) <br/>{< br/> case glu_left_button: <br/>{< br/> If (State = glu_down) <br/>{< br/> isdrag = true; <br/> prex = X; <br/> prey = y; <br/>}< br/> If (State = glu_up) <br/>{< br/> isdrag = false; <br/>}< br/>} break; <br/> case MAID: <br/>{< br/> closefactor + = 0.1; <br/>} break; <br/> case MAID: <br/>{< br/> closefactor-= 0.1; <br/>} break; <br/> default: break; <br/>}< br/> glupostredisplay (); <br/>} 

Register the following functions as a processing function by moving the mouse using the glumotionfunc () function. Then, you can use the above function to move the mouse to rotate the view and zoom the observer distance with the scroll wheel.

 

Void motion (glint X, glint y) <br/>{< br/> If (isdrag <br/>{< br/> spiny = spiny + (X-prex) * 0.5; <br/> spinx = spinx + (Y-Prey) * 0.5; <br/> prex = x; <br/> prey = y; <br/> maid (); <br/>}< br/>} 

3. The last practical function is to draw the X, Y, and Z coordinate axes of the world coordinate system in the view, so that you do not know the position of the object in the view.

(You can adjust the axis length as needed)

Void drawglobalaxis () <br/>{< br/> gldisable (gl_lighting); <br/> glbegin (gl_lines); <br/> glcolor3f (1.0, 0.0, 0.0 ); // draw the X axis in red <br/> glvertex3f (-100.0, 0.0, 0.0); <br/> glvertex3f (100.0, 0.0, 0.0); <br/> glcolor3f (0.0, 1.0, 0.0); // green draw Z axis <br/> glvertex3f (0.0, 0.0,-100.0); <br/> glvertex3f (0.0, 0.0, 100.0 ); <br/> glcolor3f (0.0, 0.0, 1.0); // draw the Y axis in blue. <br/> glvertex3f (0.0,-100.0 ,. 0); <br/> glvertex3f (0.0, 100.0, 0.0); <br/> glend (); </P> <p> glable (gl_lighting ); <br/>} 

 

 

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.