/Lgc graphic rendering/OpenGL concept Creation

Source: Internet
Author: User
OpenGL concept Establishment Author:
Liu Peng Date:

2009-06-30

This article gives a detailed introduction to OpenGL in terms of concepts, interfaces, workflows, and operation steps, and tries to give readers a comprehensive grasp of OpenGL after reading it. What is OpenGL
  • OpenGL is a software interface to graphics hardware.
  • Library of about 250 commands, platform and hardware independent.
  • The base library supports very minimal shape primitives: points, lines and polygons
  • Higher level Libraries add more rendering abilities and higher level operations (draw spheres, cylinders, teapots, etc)
OpenGL extensions

Open extensions: Other Libraries add higher level constructs

  • GLU: Gl utility Library

    Adds support for textures, additional shapes (spheres, cylinders, disks)

  • GLUT: Gl utility Toolkit

    Adds UI level things like Windows, menus, keyboard, mouse events, etc.

Naming Conventions
  • Function names indicate what library function belongs:

    • Glbegin (), glend (), gllight (), glpointsize ()
    • Glubegincurve (), glcylinder (), glsphere ()
    • Fig (), FIG ()
  • Often indicate parameter types
    • Void glvertex2d (gldouble X, gldouble y );
    • Void glvertex2i (glint X, glint y );
    • Void glvertex4f (glfloat X, glfloat y, glfloat Z, glfloat W );
  • Parameter types
    • B signed Char (8-bit)
    • S signed short (16-bit)
    • I signed int (32-bit)
    • F signed float (32-bit)
    • D signed double (64-bit)
    • UB unsigned char (8-bit)
    • US unsigned short (16-bit)
    • Ui unsigned int (32-bit)
OpenGL basic understanding 2

OpenGL is a software interface with a hardware Graphics Generator. It includes more than 100 graphic operation functions,
Developers can use these functions to construct scene models and develop 3D graphics interaction software.
OpenGL is a high-performance graphics development software package. OpenGL supports networks. Users in Network Systems
You can run program display graphics on different graphics terminals. OpenGL is a hardware independent graph.
It does not provide device operation functions that are closely related to the hardware, nor does it provide descriptions similar
Graphic operation functions of complex shapes such as airplanes, automobiles, and molecules. The user must start from a vertex, line, plane, etc.
The most basic graphic unit begins to construct its own 3D model. Of course, a higher level than Blender
The OpenGL-based 3D graphic modeling development package provides a convenient tool. Therefore, OpenGL
Graphic operation functions are very basic and flexible. For example, the process of drawing models in OpenGL is diverse.
OpenGL provides the following ways to draw 3D objects:

  • Gridline drawing method (wireframe ):
    In this way, only the mesh contour of a three-dimensional object is drawn.
  • Depth_cued ):
    Drawing with gridlines, adding a simulated human eye object is the same. Objects in the distance are darker than objects in the near corner.
  • Method for drawing grid lines (antialiased ):
    Use gridlines to draw images. Use reverse sampling to reduce the variation of the drawing lines.
  • Flat_shade ):
    The hidden surface of the model is reduced, and the plane unit of the model is colored according to the illumination level, but not smooth.
  • Smooth drawing method (smooth_shade ):
    This method is closer to reality.
  • Shadows and textures ):
    Pasting textures on the model surface or even adding light shadows makes the 3D landscape look like a photo.
  • Motion Blur (motion-blured ):
    Simulate the motion of an object.
  • Atmosphere-effects ):
    Add atmosphere effects such as fog to 3D landscapes to bring people into reality.
  • Depth-of-effects ):
    Similar to the camera lens effect, the model is clear at the focal point, and vice versa.

These 3D object plotting and special effect processing methods show that OpenGL can simulate complex 3D objects or natural landscapes.

OpenGL workflow 2

The basic workflow of OpenGL is as follows:

The geometric vertex data includes the vertex set, line set, and polygon set of the model.
The top part, including the vertex generator and vertex-by-vertex operations. The image data includes pixel sets, image sets, and map sets,
The Processing Method of pixel data is different from that of geometric vertex data.
Fragment processing until the final grating data is written into the frame buffer. In
All data in OpenGL, including geometric vertex data and pixel data, can be stored in the display list or
Can be processed immediately. In OpenGL, the display list technology is an important technology.

OpenGL requires that all geometric units be described by vertices, so that the Attention generator and vertex-by-vertex Calculation
Operations can be calculated and operated on each vertex, and then raster to form image fragments.
Pixel data, pixel operation results are stored in the memory for texture assembly, and then operated like geometric vertices
Raster to form a graphic element.

At the end of the entire process, the graphic element must perform a series of operations, such
The prime value BZ is sent to the frame buffer for graphic display.

OpenGL image operation step 2

Based on the OpenGL workflow described above, we can summarize the main graphics operations in OpenGL.
To render a 3D image landscape on a computer screen, follow these steps:

  1. Create a scene model based on the basic graphics unit and perform a mathematical description of the model (OpenGL uses points, lines, polygon, images, and bitmaps as the basic graphics unit );
  2. Place the scene model in a proper position in a three-dimensional space, and set the viewpoint (viewpoint) to observe the landscape of interest;
  3. Calculate the colors of all objects in the model. The colors are determined based on application requirements, and the lighting conditions and texture pasting methods are also determined;
  4. The process of converting the mathematical description and color information of a scene model into pixels on a computer screen is also called Rasterization ).

During the execution of these steps, OpenGL may execute other operations, such as automatic hiding
. In addition, after the scenes are raster and sent to the frame buffer, the object prime data can also be imported as needed.
A row operation is a so-called fragment operation.

Fragment is the pixel after raster. It has more location and normal vector attributes than the pixel. One by one
Metadata operations have pixel ownership operations (determine whether the target pixel is visible or covered by an overlapping window ),
Shear testing, Alpha testing, template testing, and hybrid testing. For example, OpenGL must be based on screen
The coverage value of each pixel block is calculated. In the rgba Module
In, OpenGL uses this value to multiply the Alpha value of the element, and then uses the result Alpha value to mix the element with the corresponding pixel in the cache.

From fragment to pixel (framebuffer frame cache) 3
About frame caching

The frame cache includes the cache colors, scissor, Alpha, stencer, and depth. Therefore, the frame cache does not
It is a cache, but a combination of all these caches. The frame cache corresponds to each pixel on the screen.
(Not the real pixel, but the location corresponding to a fragment ).
Color, zbuffer, etc.), the fragment of the ry is not cached, and the frame cache is on the screen.
, One bit of a cache is one-sided.

Colorbuffer is the final display on the screen, but other caches determine whether the color and
How to draw on the screen. Besides those frame caches of colorbuffer, they are also called secondary caches.

After raster to get fragment, perform various tests in the following order.
The pixels are eligible to be placed on the screen. After all tests are passed, these fragment may be written
Into the original frame cache (however, the color value mixed at the end may be the same as that in the original frame cache of the screen.
Color cache performs various operations, so the color cache may not be simply overwritten, but other caches will.

Color cache read/write: gldrawbuffer and glreadbuffer (), then call glwritepixel,
Glreadpixel.

Cache mask (mask)

You can set the cache mask for each frame, and then write it into the corresponding cache, it will be masked, as shown in

Glindexmask (glint mask): If the I-bit of the mask is 0 in the index mode, the color of the I-th index will not be written into the color cache, you will see that the area of the color has become empty, and you can use it to perform layer animation for 1 normal painting.

Glcolormask (glboolean red, glboolean green, glboolean blue, glboolean
Alpha): it can control the channel in the color value to draw the one not drawn (1 is drawn), this
It allows you to view the rendering of various colors, that is, only the red color is displayed:

Color Mask

Gldepthmask (glboolean
Flag): When the flag is true, it is written to the deep cache. Otherwise, it is not written. It is written by default. If it is not written, what effect will you see? In this way, the object drawn after each frame does not write a frame.
Memory, that is, its frame cache defaults to the smallest 0, so now you will always see that the post is drawn in front (this is the same as the scenario you can see without a deep test under normal circumstances ).

The mask of the deep cache has many applications. For example, you can set it to false at any time.
The reality behind the painting is at the top layer.

Fragment Testing

Note that all tests to be conducted here are at the fragmen level. The significance of the test is that when
The calculated fragment will eventually become a pixel of the screen and pass the test in the following order,
If the image fails to be drawn at any stage, the image will not be drawn. If the image passes the painting at the end, make sure that the image is displayed on the screen.
When you draw this point on the Color cache, you also need to cache the original color for some mixing.

Scissor test: glscissor () is used to define a rectangular area (scissor box). If
The fragment is passed in the rectangle and does not pass (not displayed). This is equivalent
Stenciltest rectangular version. The effect is as follows:

Scissor Test

Alpha test: Uses alpha (A of rgba) for its basic functions)
Cache to determine whether to draw a pixel, although its value is often used for mixed effect. Use glalphafunc () to define the reference value and conditions for passing tests. The test process is to use
When the Alpha value of fragment is compared with a defined reference value, the comparison method is specified by glalphafunc to determine whether the test is passed.

Alpha test can be used to create a hollow-out effect or a billboard. The process of creating a billboard is as follows:
A four-channel texture with rgba attached. The Alpha of the transparent part is 0, and the Alpha of the transparent part is 1.
A subdivided polygon is attached with this texture, and the Alpha value of the vertex is the point of the texture.
Alpha value, use Alpha test, and set the comparison function to gl_greter. the reference value is 0.5,
In this way, the part of 0 will fail and become completely transparent.

Stencel test: it is a template test. The image is equivalent to the following:
Determine whether to discard pixels based on the data stored in the memory of stencel buffer. Its
The test process is like this. Some values are stored in the stenpencil buffer of all pixels and then defined.
A comparison method and a reference value.
If this comparison method is passed, this is tested and drawn. You cannot write directly
Stencilbuffer, but can be indirectly written through glstencilop (). This function is set
When fragment passes or does not pass the stencer cache, what changes does the stencer cache start.

The most common use of stencel test is to define an irregular area on the screen and limit it to display in this area. Its implementation method is as follows:

First, clear the current stencer cache as 0: glclearstencer (0); glclear (gl_stencil_buffer_bit)

Then set glstencilfunc () to glalways, 1, that is, all pass this test, and set
Glsencilop (gl_replace, gl_replace, gl_replace) then draws the expected irregularities
The shape of the region. This code is used to test the shape you have drawn through stencel.
And use 1 to replace the stenpencil cache at that point. In this way, the stencel cache is changed to that shape.
The area is 1, and a template of this shape is customized.
Empty.

Finally, clear the screen color and depth cache, and set glstencilfunc to gl_equal, 1,
Glsencilop is gl_keep (the stencel cache remains unchanged, and then draw the scenario you want to draw. This way
The positions equal to 1 are drawn, but those equal to 1 are not drawn. This is the effect of this algorithm.
Result: It is drawn in a humanoid area, which is helpful for some game guis.

Stencel Test

Depth test: Generally, the deep test process is like this. A large value is used at the beginning of each frame.
To clear the depth cache, and the current depth is automatically generated during painting. The default comparison function is used.
It is a relatively small depth drawn by testing, so that the background is removed. You can also use
Gldepthfunc to set the comparison function.

After testing

After the above tests, all clips that will be considered to be written to the same screen position will mix their
The color value is written on the screen as the final color. You can set this blending method here.

Dithering: this sort is used in the index mode, because in the index mode, if the mixed color is no longer
In the index, he will find a color patchwork method to replace, turn on the gl_dither switch.

Last step logical operation: In the last step, we generate the color that will be written on the screen before the logical operation. At this time, the colorbuffer has the color value just now, which can be selected for the two
The default value is gl_copy, that is, simply copying the new value to the screen, but there are actually many optional logical operations, of course, to open
Gl_color_logic_op/index switch. In fact, this operation is very useful. It can directly control the display status on the screen. It only changes the pixels on the screen,
Therefore, you can easily implement Kendo trik, such as color inversion, switching scenarios, and clearing.

Accumulation Buffer

This framebuffer has nothing to do with the test. It is used to accumulate the value on the Color cache. That is to say, he can add the color cache value class of each frame to her, and then she can add the accumulated content.
Re-write the color cache, And we look like a few overlapping images. We can control these processes. Glaccum () controls the operations on this cache. It usually begins to accumulate, clear and repeat
Add, write to the screen color cache, and perform numeric operations. The buffer can be used for reverse sample and fuzzy processing. You must enable the glu_accum mode before using it.
Points: such as anti-image, simulated motion blur, simulated multiple exposures (that is, generating HDR images), and soft shadow.

OpenGL in Max OS X

Quartz uses OpenGL for all the whi1_effects like the genie effect when minimizing, expos é and cube rotationscompiz effect.

FAQ

Q: How do I obtain the address of the OpenGL framebuffer, so I can
Write directly to it?

A: OpenGL doesn' t provide a standard mechanic to let an application
Obtain the address of the framebuffer. If an implementation allows this,
It's through an extension.

Typically, programmers who write graphics programs for a single
Standard graphics hardware format, such as the VGA standard under
Microsoft Windows, will want the framebuffer's address. The programmers
Need to understand that OpenGL is designed to run on a wide variety
Graphics hardware, memory of which don't run on Microsoft Windows and
Therefore, don't support any kind of standard framebuffer format.
Because a programmer will likely be unfamiliar with this proprietary
Framebuffer layout, writing directly to it wowould produce unpredictable
Results. Furthermore, some OpenGL devices might not have a framebuffer
That the CPU can address.

You can read the contents of the color, depth, and stencel Buffers
With the glreadpixels () command. Likewise, gldrawpixels () and
Glcopypixels () are available for sending images to and blting Images
Around In the OpenGL buffers.

Online tutorial
  • OpenGL basic graphics programming
See also
  1. Mac OS X cocoa Programming
  2. OpenGL basic graphics programming-OpenGL concept Creation
  3. OpenGL learning notes (5)-from fragment to pixel (framebuffer frame cache)

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.