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