OpenGL frame caching and animation

Source: Internet
Author: User
17.1 frame Cache
The images on the screen are composed of pixels. Each pixel has a fixed color or other information with corresponding points, such as depth. Therefore, when drawing a graph, data must be stored evenly for each pixel in the memory. This memory zone for storing data for all pixels is called a buffer, also known as a buffer ). Different caches may contain unequal data of each pixel. However, in a given cache, each pixel is assigned data of the same digit. The cache for storing a pixel information is called bitplane ). All the caches in the system are collectively referred to as frame caches. These caches can be used for color settings, hidden surface elimination, reverse scene samples, templates, and other operations.

17.1.1 frame cache Composition
OpenGL frame Cache consists of the following four types of caches:

Color Buffer)
Color cache usually refers to the cache in which the image is to be drawn. The content can be color index or RGB color data (including alpha value ). If the OpenGL system is used by the reader to support the three-dimensional view, there are two caches: left and right. If the three-dimensional view is not supported, only the left cache is supported. Similarly, the dual-Cache OpenGL system has two caches: the front-end and the back-end, while the single-cache system only has the front-end cache. Each OpenGL system must provide a left front color cache.
Deep cache (depth buffer)
The depth cache stores the depth of each pixel. Depth is usually measured by the distance from the viewpoint to the object, so that pixels with large depth values will be replaced by pixels with smaller depth values, that is, objects in the distance are blocked by near objects. In practice, X and Y often measure the horizontal and vertical distances on the screen, while Z is often used to measure the vertical distance between the eyes and the screen.
Template Buffer)
The template cache keeps the images of some parts on the screen unchanged, while other parts can still be drawn. For example, you can use the template cache to draw a picture of the scene outside the vehicle through the car windshield. First, store the shape of the windshield in the template cache, and then draw the entire scenario. In this way, the template cache blocks anything that is invisible through the windshield, while the instrumentation and other items in the car only need to be drawn once. Therefore, as cars move, only the scenes outside are constantly changing.
Accumulation Buffer)
The cumulative cache also saves color data like the color cache, but it only saves the rgba color data, rather than the color index data (because the results of the cumulative cache in the form of a color table are not determined ). This cache is generally used to accumulate a series of images to form the final synthetic image. This method can be used to perform reverse sample operations.
17.1.2 clear Cache
The cache clearing application has been mentioned in the previous chapters. These functions are described in detail here. In many graphics Program , Clear the screen or clear any cache. Generally, the Operation overhead is very high. For example, on a 1280*1024 screen, it requires operations on thousands of pixels. In general, for a simple drawing program, the purge operation may take much more time than the drawing. If you not only clear the color cache, but also clear the depth and template cache, it will take three or four times the time overhead. To solve this problem, many machines use hardware to clear screens or clear caches. The OpenGL cleanup operation first provides the cleanup value for each cache to be written, and then uses a single function command to execute the operation, passing in all the cache tables to be cleared. If the hardware can be cleared at the same time, these cleanup operations can be performed at the same time; otherwise, each operation is performed in sequence.
The following function sets a clear value for each cache:

Void glclearcolor (glclampf red, glclampf green, glclampf blue, glclampf alpha );
Void glclearindex (glfloat index); void glcleardepth (glclampd depth );
Void glclearstenpencil (glint S );
Void glclearaccum (glflaot red, glfloat green, glfloat blue, glfloat alpha );

The above functions are the color cache in the rgba mode, the color cache in the color table mode, the depth cache, the template cache, and the cumulative cache to indicate the current clearing value. Data of the glclampf and glclampd (glfloat and gldouble) types is reduced to [0.0, 1.0], and the default depth clearing value is 0.0. The clear values set with the clear FUNCTION command remain valid until they are changed.
After selecting the cache to be cleared and its Clearing value, you can call glclear () to complete the clearing operation. This clearing function is:

Void glclaear (glbitfield mask );

Clears the specified cache. The mask parameter can be logical or of the following bits,

Gl_color_buffer_bit
Gl_depth_buffer_bit
Gl_stencil_buffer_bit
Gl_accum_buffer_bit

The bit logic determines the cache to be cleared. Note: The gl_color_buffer_bit method is used to clear the color cache of the rgba mode or the color cache of the color table mode, depending on the color method set by the current system. After the color cache is cleared, all color caches that start the write operation are cleared.

17.2 OpenGL Animation
OpenGL provides dual cache for animation production. That is to say, when a frame of the front-end cache content is displayed, the back-end cache is drawing the next frame. When the painting is completed, the background cache content is displayed on the screen, on the contrary, the front-end is drawing the next frame. In this way, the screen shows a picture that has already been drawn, so it seems that all the pictures are continuous.
In OpenGL, designing such an animation program is very simple. You only need to master a key function, namely:

Void auxswapbuffers (void );

Set the exchange cache. That is, after a painting process is completed, the front and back-end cache is switched to allow the next frame to be drawn after the screen. Of course, the function for setting the swap cache in different window systems may be different. For example, in the X Window System, it is best to use glxswapbuffers ().
In addition, the dual cache mode should be set in the window display mode. The following shows an example of a dual-Cache Animation: dbufcolr. c. This program is in nmlcolr in Chapter 13th. c-based restructuring, readers can compare and look at it, we will find that OpenGL animation program design is actually extremely easy and interesting.

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.