OpenGL Series Tutorial Two: OpenGL overview

Source: Internet
Author: User
Introducing State Machines Glbegin () and Glend () Glflush () and Glfinish ()

OpenGL Introduction
OpenGL is a software interface for graphics hardware. It is designed as a hardware-independent interface that can run on a variety of different hardware platforms. OpenGL programs can also work across networks (in the form of client-server), even if the client and server are different machine types. The client in OpenGL is the machine that the OpenGL program actually executes, and the server is the machine that implements the rendering.
OpenGL uses the prefix GL before the commands in the OpenGL core, using the prefix Glu before the commands in the OpenGL tool class. Similarly, OpenGL constants start with Gl_ and are all uppercase letters. OpenGL also uses suffixes to specify the number and type of functions.
glcolor3f (1, 0, 0);         Use 3 floating point numbers to set the rendering color to Red
glcolor4d (0, 1, 0, 0.2);    Use the double-precision floating-point setting to render a color green with an opacity of 20%
GLVERTEX3FV (vertex);        Using vectors to set coordinates for x, y, Z


State machine OpenGL is a state machine. Properties and patterns will remain in effect until they are changed. Most state variables can be enabled and disabled using the glenable () function and the gldisable () function. You can also use the glisenabled () function to check whether a state is now enabled. You can use the Glpushattrib () function and the Glpopattrib () function to save many state variables in a stack that holds property variables or to recover from the stack. The Gl_all_attrib_bits parameter can be used to save or restore all states. The number of stacks is at least 16 in standard OpenGL (use the Glinfo function to check the maximum number of elements the stack can hold).
Glpushattrib (gl_lighting_bit);    The best way to change the state, so that you can use the Glpopattrib () function to revert to the current shape
    gldisable (gl_lighting);       State, the first line of code will gl_lighting_bit status into the stack, so that the next can be
    glenable (gl_color_material);  Modified, and a moment can revert to the state
    Glpushattrib (gl_color_buffer_bit) at the time of the stack;
    Gldisable (gl_dither);
    Glenable (gl_blend);

...//do something

glpopattrib ();                    The first call will revert to the last state of the stack, i.e. Gl_color_buffer_bit
glpopattrib ();                    The second call restores the state of the second-to-last call, which is gl_lighting_bit



Glbegin () and glend () functions
In order to draw geometric entities (points, lines, triangles, etc.) in OpenGL, you can specify a series of vertex data between the Glbegin () and the Glend () functions. This method is called immediate mode. (You can also use other methods to draw geometric entities, such as vertex arrays)
Glbegin (gl_triangles);
    glcolor3f (1, 0, 0);     Sets the color of the vertex to red
    glvertex3fv (v1);        Draw a triangle
    GLVERTEX3FV (v2) using the V1,v2,v3 3 vertices;
    GLVERTEX3FV (v3);
Glend ();

There are 10 types of primitives in OpenGL: Gl_points, Gl_lines, Gl_line_strip, Gl_line_loop, Gl_triangles, Gl_triangle_strip, Gl_triangle_fan, Gl_quads, Gl_quad_strip, and Gl_polygon.
Note that not all OpenGL commands can be placed between Glbegin () and Glend (). Only a subset of the commands can be placed between the two functions, such as glvertex* (), glcolor* (), glnormal* (), gltexcoord* (), glmaterial* (), Glcalllist (), and so on.

Glflush () and Glflush () functions
Similar to the IO buffer in your computer, OpenGL commands are not executed immediately. All commands are stored in the buffer first, including the buffer in the network and the graphics accelerator itself, until the buffer is full before execution begins. For example, if an application needs to be executed over a network, sending a series of commands at a time is obviously more efficient than just one command at a time.
Glflush () empties all the commands in this buffer and forces the command to execute immediately without waiting for the buffer to fill. Therefore, Glflush guarantees that all OpenGL commands will be executed in a limited amount of time after calling Glflush (). And the Glflush function does not wait for the command to run between them and it is possible to return to your program immediately. Therefore, you can be very free to send more commands even if the commands between them are not executed.
Glfinish () Flushes the buffer like the Glflush function and forces the command to start executing. However, the Glflush function block blocks other OpenGL commands until all the commands in the Glflush function block are executed. Therefore, the Glflush () function will not return to your program until all OpenGL commands have been executed. It may be used to synchronize tasks or to measure the exact execution time of an OpenGL command.





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.