OpenGL-------State Machine

Source: Internet
Author: User

State machine is a kind of machine which exists in theory, it has the following characteristics:

1. It has the ability to memorize, to remember its current state.

2. It can receive input, according to the input content and its own state, modify its own state, and can get output.

3. When it enters a special state (down state), it no longer receives input and stops working.

Theory is very abstract, but in fact it is well understood.

First of all, in essence, our current computer is a typical state machine. Can be compared with the understanding:

1. The memory of the computer (memory, hard disk, etc.), can remember the computer's own current state (currently installed in the computer software, stored in the computer data, in fact, are binary values, all belong to the current state).

2. The input device of the computer receives input (keyboard input, mouse input, file input), according to the input content and own state (mainly refers to the program code which can run), modifies its own state (modifies the memory value), and can obtain the output (displays the result to the screen).

3. When it enters a special state (shutdown state), it no longer receives input and stops working.

OpenGL can also be seen as such a machine. Let us first understand:

1. OpenGL can record its own status (for example: the current color, whether to turn on the hybrid function, etc., which are to be recorded)

2. OpenGL can receive input (when we call the OpenGL function, we can actually consider OpenGL to receive our input), according to the input content and its own state, modify its own state, and can get output (such as we call glcolor3f, When OpenGL receives this input, it modifies its "current color" state; we call GLRECTF, then OpenGL outputs a rectangle)

3. OpenGL can go into a stop state and no longer receive input. This may not be obvious in our program, but OpenGL always stops working before the program exits .

Why should I mention the dull, obscure concept of "state machine"? It can actually help us understand something.

For example I in the previous tutorial, often said:

You can use the glcolor* function to select a color, and all objects drawn later are this color, unless you re-set it again using the glcolor* function.

You can use the gltexcoord* function to set a texture coordinate that will be used for all objects drawn later, unless you re-set it again using the gltexcoord* function.

You can use the Glblendfunc function to specify the source and target factors for the blending function, and all objects drawn later will take this source factor and target factor, unless you re-specify it again using the Glblendfunc function.

You can use the gllight* function to specify the position and color of the light source, and all objects drawn later are the position and color of the light, unless you re-specify it again using the Glblendfunc function.

OpenGL is a state machine that maintains its state unless the user enters a command to change the state.

color, texture coordinates, source and target factors, various parameters of the light source, and so on, these are states, so this sentence contains all the content described above.

In addition, whether light is enabled, texture enabled, mixed enabled, deep test enabled, and so on, these are all States, as well as the above description: OpenGL will remain in state unless we call the OpenGL function to change it.

Get the current state of OpenGL

OpenGL preserves its state, and we can use some functions to get these states.

First, some of the enabled/disabled states.

We enable States through glenable and disable them through gldisable. For example:

Glenable (gl_depth_test);

Glenable (Gl_blend);

Glenable (Gl_cull_face);

Glenable (gl_lighting);

Glenable (gl_texture_2d);

You can use the Glisenabled function to detect whether these states are turned on. For example:

Glisenabled (gl_depth_test);

Glisenabled (Gl_blend);

Glisenabled (Gl_cull_face);

Glisenabled (gl_lighting);

Glisenabled (gl_texture_2d);

If the state is turned on, the glisenabled function returns Gl_true (this is a nonzero constant, typically defined as 1), otherwise the Gl_false is returned (this is a constant with a value of 0).

We can write in the program:

if (glisenabled (gl_blend)) {     // is currently enabled for the blending function  Else  {      // no blending feature is currently turned on }

Look at other types of states.

For example, the current color, whose value is four floating-point numbers, the current set line width, its value is a floating-point number, the current viewport (Viewport, see Lesson Five), whose value is four integers.

To get the status of integer type, floating-point type, OpenGL provides GLGETBOOLEANV, Glgetintegerv, GLGETFLOATV, Glgetdoublev four functions. When a function is called, specifying the name of the state to be obtained, and where the state value needs to be stored (a pointer), these four functions can hold the state value to the pointer value position. For example:

// Gets the current line width  &LW); // get the current color glfloat cc[4];glgetfloatv (Gl_current_color, cc); // get the current viewport Glint viewport[4];glgetintegerv (Gl_viewport, viewport);

Description

1. Note the number of elements. For example, the Gl_line_width state has only one value, while the Gl_current_color has four values. Variables or arrays should be carefully defined to avoid the subscript crossing.

2. Using four different functions, the same state can also be returned as a different type of value. For example, to get the current color, you can generally return glfloat type or gldouble type. The code is as follows:

Glfloat cc[4]; Gldouble dcc[4];glgetfloatv (Gl_current_color, CC); Glgetdoublev (Gl_current_color, DCC);

Glgetbooleanv, Glgetintegerv, GLGETFLOATV, Glgetdoublev These four functions can get most of the state of OpenGL, but there are some states that are inconvenient to use these four functions to obtain. such as the state of the light source, because there may be multiple light sources, it is not possible to use similar GLGETFLOATV (Gl_light_position, POS), such a method to get the light source position. To solve this problem, OpenGL specifically provides the GLGETLIGHT* series functions to obtain the state of the light source.

Similarly, glgetmaterial*, glgettexparameter*, and so on, each function has its own scope of application.

Setting the OpenGL State

Oh, the reader may have doubts. Since there are functions like getxxx to get the state of OpenGL, why is there no setxxx such a function to set the OpenGL state?

The answer is simple, because OpenGL has provided a number of functions to set the state: glcolor*, glmaterial*, Glenable, gldisable, and so on, most OpenGL functions are used to set the OpenGL state, Therefore, you do not need to design a setxxx function to set the OpenGL state.

From the point of view of the state machine. The state machine modifies its state according to the input, rather than modifying its state directly by the outside world. So it is reasonable not to set a function such as setxxx.

OpenGL Work Flow

1. Vertex data: Vertex. For example, we specify the color, texture coordinates, normal vector, vertex coordinates, etc., all belong to vertex data. 2. Pixel data: pixels. We use pixel data when we draw pixels and specify textures. 3. Display list: Displays lists. You can save the OpenGL function of the call. (See lesson eighth)4. Evaluators: the evaluation device. This is not mentioned in the previous course, and is not expected to be mentioned later. A Bezier or Bezier surface can be specified using a locator, but it can actually be understood as specifying vertices, specifying texture coordinates, specifying normal vectors, and so on. 5. per-vertex operations and primitive assembly: single vertex operation and primitive assembly. Start by manipulating a single vertex, such as a transform (see Lesson five). The vertices are then assembled into elements (the simplest graphics that OpenGL can draw, such as points, line segments, triangles, quads, polygons, and so on, see Lesson two) .6. Pixel operations: Pixel operation. For example, convert the in-memory pixel data format to the data format supported by the graphics hardware. For textures, you can replace a subset of the pixels, which is also a pixel operation. 7. Rasterization: Rasterization. Vertex data and pixel data converge here (as you can imagine: vertices and textures, grouped together into a textured triangle), to form a complete, visible block (possibly point, segment, triangle, quadrilateral, or other irregular shape) containing several pixels. This whole block is called fragment (fragment). 8. per-fragment operations: fragment operation. Includes various fragment tests (see Lesson 12th). 9. Framebuffer: Frame buffer. This is a piece of storage space where the display device reads the data and then displays it to the screen. Ten. Texture assembly: Texture Assembly, I'm not sure how to figure it out here: (presumably, the manipulation of textures is related to pixel manipulation.)

Description: The solid line in the picture represents the normal processing flow, the dashed lines indicate that the data can be read in the opposite direction, for example, you can use Glreadpixels to read the pixel data from the frame buffer (actually read the data from the frame buffer, through the pixel operation, the pixel data format in the display device into the in-memory pixel data format, Eventually becomes the pixel data in memory).

Summary

This lesson is a dull theoretical knowledge.

OpenGL is a state machine that maintains its state and changes its state based on the functions that the user calls. Depending on the state, invoking the same function may have different effects.

Some functions can be used to obtain the current state of OpenGL. Commonly used functions are: glisenabled, Glgetbooleanv, Glgetintegerv, GLGETFLOATV, Glgetdoublev.

OpenGL workflow, input pixel data and vertex data, two kinds of data after the operation, by Rasterization, to get fragments, and then through the fragment processing, and finally drawn to the frame buffer. The plotted results can also be transferred in reverse direction and eventually converted to pixel data.



OpenGL-------State Machine

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.