OpenGL learning 03_ Double buffering doublebuffering

Source: Internet
Author: User

What is double buffering?

When we watch TV, the screen we see is called the OSD layer, which means we can only see the image on the OSD layer. Now, I need to create a virtual, invisible, but can draw (for example, draw dots, lines) of the OSD layer, I call offscreen (back buffer). This offscreen exists in memory, we draw on it, this offscreen above can be displayed on the OSD layer, need a function to create this offscreen, return this offscreen handle (int pointer), width, height, A pointer to the new offscreen data buffer, which is a offscreen data buffer created outside the function, and the size is offscreen height * Width * The size of each pixel point data. Flicker is a common problem in graphic programming. Graphics operations that require multiple complex drawing operations can cause the rendered image to blink or have other unacceptable appearances. The use of double buffering solves these problems. Double buffering uses memory buffers to resolve flicker problems caused by multi-draw operations. When double buffering is enabled, all drawing operations are first rendered to the memory buffer, not the drawing surface on the screen. When all the drawing operations are complete, the memory buffers are copied directly to the drawing surface to which they are associated. Because only one graphics operation is performed on the screen, the image flicker caused by complex drawing operations is eliminated.

Dual Buffering in OpenGL

OpenGL does not directly provide a double-buffered interface, because not all hardware supports this technology, and the implementation of the double buffering technology is highly dependent on the Windows operating system, but the GLUT function library provides the corresponding interface, the glut library solves the problem of differences between platforms.

You can set the display mode of the window to double buffering by the following methods

Glutinitdisplaymode (glut_double| GLUT_RGB);//glut_double is double buffered

Swap buffer data When a display change is required

Glutswapbuffers ();//Swap Buffer

The following implements a rotation animation of a rectangle, demonstrating the use of double buffering.

main.cpp//opengl_02_doublebuffering////Created by Apple on 14/12/28.//Copyright (c) 2014 CC. All rights reserved.//#include <iostream>//glut library with a set of graphical UI interfaces compatible with various operating system OpenGL # <GLUT/GLUT.h>    static Glfloat spin = 0.0f;/** * Initialization operation */void Init () {Glclearcolor (0.0f, 0.0f, 0.0f, 0.0f);    Sets the shading mode, Gl_flat uses constant shading, and renders the entire entity with the color of a vertex in the entity. Glshademodel (Gl_flat);}    /** * Display Drawing effect */void display () {//Cleanup color buffer glclear (gl_color_buffer_bit);    The current matrix is pressed into the stack to save Glpushmatrix ();    Set Color glcolor3f (1.0f, 0.0f, 0.0f);    Rotation operation, rotation around the z-axis spin degrees GLROTATEF (spin, 0.0f, 0.0f, 1.0f);    Construct a rectangular GLRECTF ( -25.0f, -25.0f, 25.0f, 25.0f);    Restores the Matrix Glpopmatrix () before the drawing transformation; Swap buffer, swap the color buffer that is being displayed and the color buffer data being drawn glutswapbuffers ();} /** * Resize Window * * @param width Width * @param height */void reshape (int width, int height) {//Set viewport rectangle area, by default, viewport is set    Glviewport (0, 0, (Glsizei) width, (glsizei) height) for the entire pixel rectangle that occupies the open window;    The subsequent matrix operation Glmatrixmode (gl_projection) is applied to the projection matrix; And soResets the stack top matrix, which was previously transformed by the matrix transformation, back to the unit matrix!    is equal to the effect of the previous matrix transformation.    Glloadidentity ();    Set cropping coordinate system Glortho ( -50.0f, 50.0f, -50.0f, 50.0f, -1.0f, 1.0f);    Apply the subsequent matrix operation Glmatrixmode (Gl_modelview) to the Model view matrix; Glloadidentity ();}    /** * Play rotation animation */void Spindisplay () {spin + = 3.0f;    if (Spin > 360.0f) {spin-= 360.0f; }//Mark the current window needs to be redrawn. By Glutmainloop the next loop, the window display will be called back to re-display the window's normal panel.    Calling Glutpostredisplay multiple times, the next display callback only produces a single re-display callback. Glutpostredisplay ();}  /** * Mouse Event Callback * * @param button button * @param state Status * @param x x * @param y y */void Mouse (int button, int State, int x, int y) {switch (button) {//press the left mouse button case Glut_left_button:if (state = = Glut_do WN) {//Set global callback function, GLUT program function can perform background processing task or continuous animation when no window event arrives.                If enabled, this idle function is called continuously until a window event occurs.            Play rotation animation Glutidlefunc (spindisplay);        } break;               Press the right mouse button case Glut_right_button:if (state = = Glut_down) { Stop function callback//Stop rotation animation glutidlefunc (nullptr);    } Default:break;    }}int Main (int argc, const char * argv[]) {//Initialize GLUT library Glutinit (&AMP;ARGC, (char**) argv); Sets the double-buffered, RGB-pixel-formatted window glutinitdisplaymode (glut_double |    GLUT_RGB);    Set Window Size glutinitwindowsize (250, 250);        Create Window Glutcreatewindow ("Double Buffer");        Initialization Operation Init ();    Set the display callback method Glutdisplayfunc (display);    Sets the callback method when the window changes glutreshapefunc (reshape);    Set Mouse Event callback method Glutmousefunc (mouse);        Draw thread Start loop glutmainloop (); return 0;}

OpenGL learning 03_ Double buffering doublebuffering

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.