Introduction to basic knowledge of OpenGL Development

Source: Internet
Author: User

  Recently, due to the need to use OpenGL ES in mobile projects, OpenGL related knowledge is being studied during this time. Since OpenGL ES is the cropping version of OpenGL, it is very easy for me to start with OpenGL and then look at OpenGL ES. Starting from this article, I will post a series of articles to gradually introduce OpenGL-related knowledge. We can use VC6.0 or. NET for development.

 

So today I want to introduce the basic knowledge of OpenGL development, which is very important and will make us familiar with the working principle and process of OpenGL as a whole.

  1. Keep mode and instant mode:
Generally, we can use two different methods to write 3D images:
A method is called a persistence mode. In the persistence mode, we can provide an object and scenario description to the compiled API or toolbox, and then the graphic package will create this image on the screen, what we need to do is to provide commands to change the position and observation direction of other objects in the camera or scene. For our developers, the scenario diagrams that can describe objects and scenarios are called scenario diagrams. What is a scenario diagram? You may be familiar with this name but cannot tell the exact meaning of it. Generally, a scene map is a directed acyclic graph data structure. It contains all objects in the scene and their relationships, describes objects and scenarios. It is understood that many game engines and high-level toolboxes now use this method. This makes it unnecessary for our developers to strictly control the rendering process. It only needs to provide a model or scenario to the graphic function library, and the graphic function library will be responsible for rendering, this greatly reduces the workload of our developers.

 

Another method is called the instant mode. In Instant mode, we do not need to provide a model or scenario as in keep mode, but instead send commands to the graphics processor, the graphics processor immediately produces results based on its status and commands. When querying graphics books, we know that APIs or scenario diagrams in most persistence modes use an instant mode API internally to execute actual rendering tasks.

 

  2. What is OpenGL?

OpenGL is an application programming interface (API). With this API, developers can develop programs that have access to graphic hardware. We can use OpenGL to develop graphics programs or games with high operating efficiency, Because OpenGL is very close to the underlying hardware and OpenGL makes it unnecessary for us to focus on the details of graphics hardware. Since our developers do not have to pay attention to the details of the graphic hardware, what do we need to pay attention? We need to pay attention to how OpenGL is drawn. According to the terminology, the image of the object is generated by using a virtual camera and light based on the specifications and properties of the object. The OpenGL program has nothing to do with the platform. Therefore, the OpenGL API does not contain any input function or window function, because both functions depend on a specific platform, such as Windows, linux or other systems.

 

OpenGL APIs are procedural and not descriptive. That is, OpenGL is not object-oriented, so OpenGL cannot use object-oriented features, such as overloading and inheritance, however, we can use object-oriented programs to link with OpenGL implementation. As developers, we do not need to describe the nature and appearance of the scenario, but determine some operation steps, which are used to implement certain graphics or images. When implementing these steps, we can call some commands in OpenGL to draw points, straight lines, polygon, or other graphics. We can also call these commands to achieve illumination and coloring, animation and other effects.

 

OpenGL implementation can be software implementation or hardware implementation. The software implementation is the response to the OpengGL function call and the creation of a two-dimensional or three-dimensional image function library, the hardware implementation is to set the graphics card driver that can draw graphics or images. Generally, hardware implementation is much faster than software implementation. We should all be familiar with it. On Windows, graphical device interfaces display graphics or images on the screen or on other display devices. In terms of software implementation, OpenGL will generate a corresponding image or image based on the requirements of program commands on Windows, and then hand over the image or image to the graphic device interface, graphical device interfaces display graphics or images on our screens or other display devices. In this case, we may understand that OpengGL originally operates between the application and the graphic device interface, but I do not feel that it is accurate yet. You may understand the working principle of OpenGL in the following figure:

  

 

Is the working principle of OpenGL software implementation. Note that the constructor graph in is constructed by software.

 

The hardware implementation of OpenGL is slightly different from the software implementation. The hardware implementation is to pass OpenGL calls to the hardware driver, the hardware driver does not transmit the generated graphics or images to the graphic device interface, but directly communicates with the display device and directly transmits the graphics or image results to the display or other display devices. As shown in:

  

  

OpenGL is based on a model called pipeline model when drawing a graph. In other words, the ry specifies its shape by describing the space position or vertex in the program and is generated by the program. When these vertices flow through a series of modules, each module performs one or more operations on the basic components of a graph (called an element here) when it passes through. The module is responsible for implementing one or more operation transformations on the elements of the stream, such as rotation, translation, scaling, and camera positioning.

  

  3. OpenGL Composition

OpenGL contains many functions for image processing, including the following:

Metafunction: specify the elements for generating images or images. There are two main types: one is to draw two-dimensional or three-dimensional geometric elements, such as points, lines, polygon, and the other is discrete entities, such as bitmap.

Attribute functions: attribute functions mainly control the appearance and style of elements, such as processing the color, line type, illumination, and texture of elements.

Observation Function: The observation function mainly performs operations on camera attributes. We can operate the camera to display the image or image's near or remote effect.

Control Function: allows us to enable or use various OpenGL features.

Query function: Let's query the value of the OpenGL state variable.

Input and window control functions: This function does not belong to OpenGL, but these functions are important because we often input and output or window control operations in the program.

 

OpenGL libraries are generally included in two libraries, called GL or GLU. GL is the core library of OpenGL and contains necessary OpenGL functions. GLU is a practical library of OpenGL and contains many new functions. The following code shows the typical header files contained in many Windows programs:

#include <GL/gl.h>#include <GL/glu.h>#include <windows.h>

 

However, to achieve interaction with the window system, the following code is generally used to reference the header file:

#include <GL/glut.h>

 

GLUT represents the OpenGL toolbox, reflecting the function libraries common to modern window systems. The purpose of GLUT is to hide the details of the platform. glut. h already contains gl. h and glu. h. GLUT is used because OpenGL does not contain input and window commands, while input and window commands are determined by the platform and are highly relevant to the platform. But as mentioned above, OpenGL is irrelevant to the platform, that is, OpenGL is cross-platform. In this way, the designer needs to design a function library that needs to interact with the window system.

 

To make it easier for OpenGL code to be transplanted from one platform to another, OpenGL defines its own data types, which can be mapped to the corresponding C language data types. Display the ing relationship:

 

  

  

  4. development languages and programming conventions

Currently, the most popular way to develop OpenGL is to bind OpenGL C language. Of course, you can also use other platforms or languages, such as. NET, JAVA, Python, and Perl. In the next article, I will introduce how to configure the corresponding environment.

 

We will see that many OpenGL functions begin with gl, Because OpenGL functions follow certain naming conventions and can tell us which function library this function comes from, we can also tell you the number and type of parameters of this function.

OpenGL functions use the following format:

<Function library prefix> <root command> <number of optional parameters> <optional parameter types>

 

The following is a function annotation diagram:

  

 

  5. Coordinate System and Transformation

When developing an OpenGL program, two coordinate systems are required. One is called the object coordinate system, and the other is called the world coordinate system.

The first coordinate system is the coordinate system we used during development. The second coordinate system is also known as the window coordinate system or screen coordinate system, in which the Unit is pixel.

During the painting process, OpenGL automatically converts the object to the window coordinate system. The required information is the size of the window displayed on the screen and the size of the object space to be displayed. The coordinate system transformation required by OpenGL is determined by two matrices: The model view matrix and the projection matrix. These matrices are part of the OpenGL state. The typical steps for setting these two matrices include the following three steps:

(1) Specify the matrix we want to modify.

(2) set the matrix as the unit matrix.

(3) modify the current matrix to the expected matrix.

 

The preceding three steps correspond to the following code:

glMatrixMode(GL_PROJECTION)glLoadIdentity();gluortho2D(-1.0,1.0,-1.0,1.0)

 

  6. elements and attributes

Elements are common basic entities in graphics systems. They mainly refer to points, lines, straight lines, polygon, bitmap, and pixels, our 2D or 3D images can be drawn from these basic elements. The first four are known as geometric elements, which are mainly used to build geometric images. The last two are called non-geometric elements. OpenGL differs significantly in processing geometric and non-geometric elements.

Each element has its own attributes, which determine the display mode of OpenGL. For example, the color, shape, and line width of a polygon.

When we draw a basic graph, we always start with the glBegin () function and end with the glEnd () function. For different graphs, the parameters in the glBegin () function are different. Draw two line segments as follows:

glBegin(GL_LINES);    glVertex2f(-0.5, -0.5);    glVertex2f(-0.5, 0.5);    glVertex2f(0.5, 0.5);    glVertex2f(0.5, -0.5);glEnd();

 

OpenGL provides many functions for drawing images, such as illumination, invisibility, and texture ing. Each function affects the rendering processing speed, some features can be enabled or disabled separately in our program. Disable them when not in use to make our program more efficient. The following code enables or disables a function:

Void glable (GLenum feature) void glDisable (GLenum feature) // enable the drag mode glEnable (GL_LINE-STIPPLE)

 

7. Save status

  OpenGL is a state machine internally, and function calls modify its internal state. The state of OpenGL determines the behavior and Drawing Method of the elements. All the modifications to the attributes of the element and other state variables, such as the Model View matrix and projection matrix, change the current state. Two types of stacks are provided in OpenGL to save the current state in the stack for future use.

 

The matrix stack can be used to save the projection matrix and Model View matrix. Each type of stack can only be used to hold the corresponding type of matrix. The matrix used is determined by the current matrix mode (GL_MODELVIEW or GL_PROJECTION. Use the glPushMatrix () and glPopMatrix () functions to bring the matrix into or out of the stack.

 

The main function of the matrix stack is to use the stack to traverse the tree data structure of these layered models when building a hierarchical model. The second is to return to the previous view when drawing the hierarchical model, we do not need to re-calculate and draw. We will often see the following code during the development process:

glMatrixMode(GL_PROJECTION);glPushMatrix();glPopMatrix();

 

It should be noted that the inbound and outbound operations must be used in pairs; an outbound stack must correspond to an inbound stack. In a hierarchical system, if this pair of operations does not appear correctly, the stack is in an unpredictable state.

  

  8. Single and Double Buffering

We often see the following line of code when building a graphic window:

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 

 

This line of code shows the type of display mode used during creation. Glu_single indicates that a single buffer window is used; glu_rgba indicates that the RGBA color mode is used.

A single buffer window means that all the drawing commands are executed in the displayed window. Another display mode is the dual buffer window. The drawing command is actually executed in a buffer outside the screen, and then the quick response is switched to the window view for display. We often use the double buffer mode to develop programs with animation effects, which will improve the execution efficiency of our programs.

 

  9. A simple example

  Well, as mentioned above, some people may want to know what the OpenGL program is like and what the main framework is? If you have experience in C Programming, you can see its structure at a glance.

  Next, let's take a look at the OpenGL example in the running VC ++ environment. The Code is as follows:

# Include "StdAfx. h "# ifdef _ APPLE __# include <GLUT/glut. h> # else # include <GL/glut. h>/* glut. h contains gl. h and glu. h */# endifvoid display (void) {// clear the color buffer glClear (GL_COLOR_BUFFER_BIT); // draw a rectangle glBegin (GL_POLYGON); glVertex2f (-0.5,-0.5 ); glVertex2f (-0.5, 0.5); glVertex2f (0.5, 0.5); glVertex2f (0.5,-0.5); glEnd (); // execute the buffer glFlush ();} int main (int argc, char ** argv) {// specifies the initialization window, where the value of the value is "maid" (& argc, argv ); gluinitwindowposition (0, 0); glucreatewindow (""); gludisplayfunc (display); glumainloop (); return 0 ;}

 

This is an example running in VC6.0. When creating a project, you must note that the console project is created.

How is it? Can you understand the code in the main function? I believe it is not difficult for everyone. The following is a simple explanation.

 

(1) The "Draw rectangle" window is to create a window with the title "Draw rectangle.

(2) The gluinitdisplaymode (glu_single | glu_rgb) function has been mentioned above.

(3) gludisplayfunc (display); this line of code determines the previously defined display () function as the display callback function. That is to say, when the window needs to be drawn, GLUT will call this function. For example, this call occurs when the window is displayed for the first time, when the window size is changed, or when the window is restored from the overwritten state. This is where we place OpenGL rendering function calls.

(4) The window size and position are set respectively.

(5) This function is very important. This function starts the running of the GLUT framework. Once called, the function is no longer returned until the end of the program. Therefore, this function is called only once in our program. It can process specific messages, strike keys, and other event operations in the operating system until the end of our program.

 

The result of the above example is to draw a White Rectangle in the form, as shown below:

 

  

 

This program demonstrates the basic principle of using GLUT to create a window in OpenGL.

The above is the basic principle and basic knowledge in OpenGL development.

 

Finally, I hope to reprint friends can respect the author's labor results, plus reprint address: http://www.cnblogs.com/hanyonglu/archive/2012/05/13/2498110.html thank you.

 

Download the example:/Files/hanyonglu/OpenGL/MyOpenGLDemo4.rar

Complete. Pai_^

 

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.