Dual buffering technology implemented in OpenGL

Source: Internet
Author: User

A simple way to implement the dual buffering technique in OpenGL: 1. When calling the Glutinitdisplaymode function, turn on glut_double, which is Glutinitdisplaymode (Glut_rgb | glut_double);. This replaces our usual glut_single with glut_double, which means to use a double buffer instead of a single buffer. 2. When you call Glutdisplayfunc (display) to register a callback function, call Glutswapbuffers () to swap two buffer pointers after all the drawing operations in the callback function have completed. 3. Call Glutidlefunc to register an idle draw operation function and register this function to call the display function again.
#include <GL/glut.h>#include<stdlib.h>StaticGlfloat spin =0;voidInitvoid) {Glclearcolor (0,0,0,0);//set Clear background colorGlshademodel (Gl_flat);//Set Shading Mode}voidDisplayvoid) {glclear (gl_color_buffer_bit);//clears the value with the current buffer,Glpushmatrix ();//Press the current matrix into the stackGLROTATEF (Spin,0,0,1);//Rotation AngleGLCOLOR3F (1,1,0); GLRECTF (- -, - -, -, -);    Glpopmatrix (); Glutswapbuffers (); //swap double buffer pointers}voidSpindisplay (void) {Spin= (spin +2.0) >= the? spin-358: Spin +2.0; Glutpostredisplay ();}voidSpinrightdisplay (void) {Spin= (Spin-2.0) <0? Spin + the: Spin-2.0; Glutpostredisplay ();}voidReshape (intWinth) {Glviewport (0,0, (Glsizei) W, (Glsizei) h);    Glmatrixmode (gl_projection);    Glloadidentity (); Glortho (- -, -, - -, -, -1,1);    Glmatrixmode (Gl_modelview); Glloadidentity ();}voidMouseintbuttonintStateintXinty) {    Switch(Button) { CaseGlut_left_button:if(state = =Glut_down)        {Glutidlefunc (Spindisplay); }         Break;  CaseGlut_middle_button:if(state = =Glut_down)        {Glutidlefunc (NULL); }         Break;  CaseGlut_right_button:if(state = =Glut_down)        {Glutidlefunc (Spinrightdisplay); }    default:         Break; }}intMainintARGC,Char**argv) {Glutinit (&argc, argv); Glutinitdisplaymode (glut_double|Glut_rgb); Glutinitwindowsize ( -, -); Glutinitwindowposition ( -, -); Glutcreatewindow (argv[0]);    Init ();    Glutdisplayfunc (display);    Glutreshapefunc (reshape);    Glutmousefunc (mouse);    Glutmainloop (); return 0;}

Dual buffering technology implemented in OpenGL

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.