OpenGL plotting process

Source: Internet
Author: User

The basic process of OpenGL drawing is to clear the buffer, set the current color, draw geometric elements, and output the image.

 

1. Clear the Buffer Zone

The buffer zone is the memory area used to save the image data generated during the drawing process. OpenGL has four common buffers: color, depth, accumulation, and template. This section only describes how to clear the color buffer and the depth buffer.

Since the buffer may retain the image data left over from the previous drawing, this data affects this drawing, so they must be cleared before creating a new drawing. The color buffer stores the pixel color data of the image, and the depth buffer stores the depth information used for image hiding. In a 3D scenario, objects closer to the viewpoint may partially or entirely block objects farther away from the viewpoint. This technology makes Image Hiding Technology invisible to some of the hidden cuts. The simplest way to achieve image hiding is to use the depth Buffer (Z-Buffer) to clear the depth Buffer so that the operation can be properly performed. In general, the buffer to be cleared is the two buffers.

The operation of clearing the buffer is divided into two steps.

1) set the clear value. The clear value is the data used to fill the buffer when the buffer is cleared. The function used to set the clear value of the color buffer is glClearColor (). It has four floating-point parameters, indicating the R, G, B, And A values of the clear value. The default value is 0. The fourth parameter, A, indicates the Alpha value, which is mainly used for fusion. Generally, it is set to 0. If you use the entire window as the drawing area of OpenGL, you can clear the color buffer value as the background color of the window. The function that sets the depth buffer clearing value is glClearDepth (). It has only one parameter, that is, the depth clearing value. The value range is [0.01, 1.0], and the default value is 1.0.

2) Clear the buffer. The function used to clear the buffer is glClear (). It has only one parameter, indicating the type of the buffer to be cleared. The valid values include GL_COLOR_BUFFER_BIT (color buffer) and GL_DEPTH_BUFFER_BIT (depth buffer), GL_ACCUM_BUFFER_BIT (cumulative buffer), GL_STENCIL_BUFFER_BIT (template buffer ). These values can also be used logically or in combination to clear multiple buffers at a time.

 

2. Set the color

Set the current color as needed. Once a color is set to the current color, all objects will be drawn with this color until the current color is set to the new color. The function used to set the color is glColor ().

 

3. drawing geometric figures

There are many functions used for drawing in OpenGL. Here we only introduce the plotting functions of basic geometric elements. Call the basic primitive function between glBegin () and glEnd () to draw points, lines, and polygon. The following is a prototype of the glBegin () function:

Void glBegin (GLenum mode );

Mode indicates the type of the geometric element. The values are as follows.

The following is a prototype of the glEnd () function:

Void glEnd (void );

Between glBegin () and glEnd (), you can only call the functions listed in the following table. calling other functions will cause OpenGL errors. The glBegin () and glEnd () functions must be used in pairs. Otherwise, unexpected errors may occur.

 

Iv. Graphic Output

The glFlush () function starts the execution of the OpenGL drawing command and outputs the image raster on the device. If OpenGL works in double buffer mode, the buffer switching function (SwapBuffers () implicitly executes glFlush ().

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.