Overview of OpenGL programming lectures based on VC ++

Source: Internet
Author: User
Tags types of functions
Preface

With the development of computer multimedia, visualization, and graphics technologies, we can use computers to precisely reproduce the colorful 3D objects in the real world and give full play to our creative thinking, it is the most fashionable VR technology to simulate and transform the real world through human-computer interaction. With this technology, construction engineers can directly design beautiful building models. Military commanders can simulate the battlefield for military deduction, and netizens can visit scenic spots and monuments such as the Palace Museum without leaving their homes. The most important part of VR technology is 3D graphics programming. Currently, the most prominent 3D Graphics programming tool is SGI's Open Graphics Language (OpenGL), which has become an industrial standard computer 3D Graphics software development interface, it is also widely used in Game Development, architecture, product design, medicine, earth science, fluid mechanics, and other fields. It is worth mentioning that, although Microsoft has its own 3D Programming Development Tool DirectX, it also provides OpenGL graphics standards. Therefore, OpenGL can be widely used in microcomputer.

Currently, OpenGL has set off a boom both at home and abroad, but there are not many materials introduced in this field in China, especially readers who are interested in in-depth research on graphics and images, it is often difficult to grasp the OpenGL programming interface technology and cannot be extended in depth. In order to open the mysterious door of 3D graphics programming, this lecture focuses on programming implementation in the Visual C ++ 6.0 development environment based on OpenGL's theoretical knowledge. Due to the limited level, this lecture may not cover all things and have some omissions, but I believe it can give readers the key to open the "mysterious door.

  I. features and functions of OpenGL

OpenGL is the best environment for developing simple, interactive, 2d, and 3D graphics applications. Any high-performance graphics application, from 3D animation, CAD-assisted design to visualization, openGL features high quality and high performance. Since its appearance in 1992, OpenGL has gradually developed and improved. It has become the only open and independent graphics standard for application platforms, A typical OpenGL application can run on any platform-you only need to re-compile it using the OpenGL library of the target system.

OpenGL is very close to hardware and is an interface between graphics and hardware. It includes more than 100 graphic functions used to create 3D models and perform 3D real-time interaction. Powerful graphics Functions of OpenGL do not require developers to write the data of 3D object models into a fixed data format, nor require developers to write functions such as matrix transformation and external device access, it greatly simplifies the programming of 3D graphics. For example:

1) OpenGL provides a series of 3D graphics units (elements) for developers to call.

2) OpenGL provides a series of graphic transformation functions.

3) OpenGL provides a series of external device access functions, allowing developers to conveniently access external devices such as the mouse, keyboard, space ball, and Data Glove.

Because Microsoft contains OpenGL in windows, OpenGL can work closely with Visual C ++ to implement computation and graphics algorithms easily and quickly, and ensure the correctness and reliability of the algorithms. To put it simply, OpenGL provides functions such as modeling, transformation, color processing, light processing, texture ing, image processing, animation, and object motion blur:

1. Modeling

In addition to providing basic points, lines, and polygon drawing functions, the OpenGL graphics library also provides complex 3D objects, drawing functions such as the ball, cone, polygon, teapot, and complex curves and surfaces (such as curves or surfaces such as betilles and PAIs.

2. Transformation

The transformation of OpenGL graphics library includes basic transformation and projection transformation. Basic transformations include four types of transformations: translation, rotation, variation ratio, and image. projection transformations include parallel projection (also known as normal projection) and Perspective Projection.

3. color mode settings

There are two OpenGL color modes: rgba mode and color index ).

4. Lighting and material settings

OpenGL light includes emitted light, ambient light, diffuse light, and specular light ). The material is represented by light reflectivity. Objects in the objective world eventually reflect the human eye's color, which is formed by multiplying the reflectivity of the red, green, and blue components of the light and the material.

5. Texture Mapping)

The OpenGL texture ing function can be used to express the surface details of objects in a realistic manner.

6. bitmap display and Image Enhancement

In addition to basic copying and pixel reading and writing, OpenGL provides special Image Processing for fusion, antialiasing, and fog. The above three simulated objects are more realistic and enhance the graphic display effect.

7. Double Cache animation (double buffering)

OpenGL uses the foreground cache and the background cache alternate display scenario (scene) technology. In short, the background cache computing scenario, the generated image, and the foreground cache Display Background cached pictures.

8. Special Effects

OpenGL can also be used to implement special effects such as depth cue and motion blur. Motion Blur (motion-blured): simulates the motion of an object. The depth-of-effects (depth-of-effects) is 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 demonstrate that OpenGL can simulate complex 3D objects or natural landscapes.

  Ii. OpenGL Workflow

The basic workflow of OpenGL is as follows:


Figure 1. OpenGL Workflow

As shown in, the geometric vertex data includes the vertex set, line set, and polygon set of the model. The data goes through the upper part of the flowchart, including the vertex generator and vertex-by-vertex operations; image Data includes pixel sets, image sets, and map sets. The processing method of image pixel data is different from that of geometric vertex data, however, they are all rasterized and processed individually by fragment until the final grating data is written into the frame buffer.

All data in OpenGL, including geometric vertex data and pixel data, can be stored in the display list or processed immediately. In OpenGL, the display list technology is an important technology.

OpenGL requires that all geometric units be described by vertices. In this way, the Attention generator and vertex-by-vertex computing operations can calculate and operate on each vertex, and then raster to form graphical fragments; for pixel data, the pixel operation results are stored in the memory used for texture assembly, and then rastered like the geometric vertex operation to form the graphic element. At the end of the entire process, the graphic element must perform a series of operations one by one, so that the final pixel value is sent to the frame buffer to display the image.

Based on this process, we can summarize the basic steps for performing major graphic operations in OpenGL until rendering on the computer screen to draw a 3D graphic landscape:

1) create a scene model based on the basic graphics unit and perform a mathematical description of the model (in OpenGL, points, lines, polygon, images, and bitmaps are used as the basic graphics unit ).

2) Place the scene model in a proper position in the 3D 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) converts the mathematical description and color information of the scene model to the pixels on the computer screen. This process is also called rasterization ).

During the execution of these steps, OpenGL may execute other operations, such as automatic blanking. In addition, pixel data can be operated as needed before the raster is sent to the frame buffer.

Iii. OpenGL library functions and data types in Windows

(1) database functions

To develop OpenGL-based applications, you must first understand the library functions of OpenGL. It adopts the C language style and provides a large number of functions for graphic processing and display. The OpenGL graphics library contains more than 100 functions, which belong to different libraries such as the basic library, utility library, and auxiliary library of OpenGL.

1. The core library contains 115 functions, which are the most basic functions with a prefix of gl. These functions are used for conventional and core graphics processing. dll. Functions in the core library can be further divided into the following types of functions.

(1) function for drawing basic geometric elements.

GlBegain (), glEnd (), glNormal * (), glVertex *().

(2) matrix operations, geometric transformations, and Projection Transformation functions.

GlPushMatrix (), glPopMatrix (), glLoadMatrix (), glMultMatrix (), glMatrixMode () and matrix standardization functions glLoadIdentity (), geometric transformation functions glTranslate * (), glRotate * () and glScale * (), projection transformation functions glOrtho (), glFrustum () and glViewport.

(3) color, illumination, and material functions.

For example, set the color mode function glColor * (), glIndex * (), glLight * (), glLightModel * (), and glMaterial.

(4) display the list function.
 
Mainly include glNewList (), gldlist (), glGenLists (), glCallList (), and glDeleteLists () functions for creating, ending, generating, deleting, and calling the display list.

(5) Texture ing function.

Mainly include one-dimensional texture functions glTexImage1D (), two-dimensional texture functions glTexImage2D (), texture parameters, texture environment and texture coordinate functions glTexParameter * (), glTexEnv *() and glTetCoord.

(6) special effect functions.

GlBlendFunc (), glHint (), and glFog *().

(7) raster and pixel operation functions.

Pixel location glRasterPos * (), line width glLineWidth (), polygon rendering mode glPolygonMode (), read pixel glReadPixel (), copy pixel glCopyPixel (), etc.

(8) Selection and feedback functions.

It mainly includes the rendering mode glRenderMode (), the selection buffer glSelectBuffer (), and the feedback buffer glFeedbackBuffer.

(9) curve and Surface Plotting functions.

The glMap * () and glMapGrid * () functions that generate curves or surfaces, and the glEvalCoord * () glEvalMesh * () function of the calculator *().

(10) set the status and query functions. Mainly include glGet * (), glable (), and glGetError.

2. OpenGL utility library (GLU) provides more functions, such as complex curve and surface drawing, advanced coordinate transformation, and polygon division. There are 43 functions in total, the prefix is glu. By calling the functions of the core library, the Glu function provides relatively simple usage for developers and implements some complex operations. This type of function is interpreted and executed by glu. dll. It mainly includes the following types:

(1) Auxiliary texture map function.

Including gluScaleImage (), gluBuild1Dmipmaps (), and gluBuild2Dmipmaps.

(2) Coordinate Transformation and Projection Transformation functions.

Defines the projection method functions gluPerspective (), gluOrtho2D (), gluLookAt (), picks up the projection visual object functions gluPickMatrix (), and projects gluProject () and gluUnProject.

(3) polygon mosaic tool.

Including glunewtess (), gludeletetess (), glutesscallback (), glubeginpolygon () glutessvertex (), glunextcontour (), and gluendpolygon.

(4) quadratic surface rendering tool.

It mainly involves drawing the spherical surface, cone surface, cylindrical surface, ring surface glunewquadric (), glusphere (), glucylinder (), gludisk (), glupartialdisk (), and gludeletequadric.

(5) Non-Uniform Rational B-spline rendering tool.

It is mainly used to define and plot the curves and surfaces of a group, including glunewnurbsrenderer (), glunurbscurve (), glubeginsurface (), gluendsurface (), glubegincurve (), glunurbsproperty.

(6) Error feedback tools.

Returns the string gluerrorstring () of the error message.

3. OpenGL auxiliarylibrary (Glaux), including simple window management, input event processing, and some complex 3D object rendering functions. There are 31 functions prefixed with aux. Such functions are interpreted and executed by Glaux. dll. Auxiliary library functions mainly include the following types.

(1) window initialization and exit functions.

Auxinitdisplaymode () and auxinitposition ().

(2) window processing and time input functions.

Auxreshapefunc (), auxkeyfunc (), and auxmousefunc ().

(3) color index loading function.

Auxsetonecolor ().

(4) 3D object rendering function.

There are two forms of mesh and solid body, such as drawing a cube auxWireCube () and auxSolidCube (). Here we take the mesh as an example. The cuboid auxWireBox (), the ring surface auxWireTorus (), the cylindrical auxWireCylinder (), the twenty surface auxWireIcosahedron (), auxWireDodecahedron (), cone auxWireCone () and teapot auxWireTeapot (). To draw a Solid body, you only need to replace "Wire" in the above function with "Solid.

(5) others.

Background process management function auxIdleFunc (); program running function auxMainLoop ().

4. OpenGL Toolkit)

It contains more than 30 functions with a prefix of glut. This function is interpreted and executed by glut. dll. These functions mainly include:

(1) window operation functions

Window initialization, window size, window location, and other functions such as maid (), FIG (), and fig.

(2) callback function.

Response to refresh messages, keyboard messages, mouse messages, timer functions, and so on ().

(3) create complex 3D objects. These functions are the same as those of the aux database. Create a mesh and a solid body. For example.

(4) menu functions

Create the functions for adding a menu, such as glucreatemenu (), glusetmenu (), gluaddmenuentry (), gluaddsubmenu (), and gluattachmenu ().

(5) program running functions.

Glumainloop ().

5. 16 WGL functions are specially used for OpenGL and Windows Window System connections. They are prefixed with WGL and are mainly used to create and select a graphic operation description table (renderingcontexts) and display the character bitmap at any position in the window. These functions mainly include the following types:

(1) Plotting context-related functions.

Wglcreatecontext (), wgldeletecontext (), wglgetcurrentcontent (), wglgetcurrentdc () wgldeletecontent (), and so on.

(2) text and text processing functions.

Wglusefontbitmaps () and wglusefontoutlines ().

(3) covering layer, formation and Main Plane layer processing functions.

Wglcopycontext (), wglcreatelayerplane (), wgldescribelayerplane (), wglreakizelayerplatte (), and so on.

(4) other functions.

Wglsharelists () and wglgetprocaddress.

6. In addition, there are five Win32 functions used to process the pixel format (pixel formats) and double cache. Since they are extended to Win32 systems, they cannot be applied to other OpenGL platforms.

(2) OpenGL Data Types

In contrast to the C language, OpenGL also has integer, byte, floating point and other data types. To illustrate the relationship between the two, the following table compares OpenGL data types with corresponding C types:

Prefix Data Type C Language OpenGL type
B 8-bit integer Signed Char Glbyte
S 16-bit integer Short Glshort
I 32-bit integer Long Glint, glsizei
F 32-bit floating-point Float Glfloat, glclampf
D 64-bit floating-point Double Gldouble, glclampd
UB 8-bit unsigned integer Unsigned char Glubyte, glboolean
Us 16-bit unsigned integer Unsigned short Glushort
UI 32-bit unsigned integer Unsigned long GLuint, GLenum, GLbitfield

Table 1. OpenGL data type table

In addition, OpenGL also defines the GLvoid type.

You can use it to replace the void type.

(3) Naming rules of OpenGL library functions

After learning about OpenGL data types, let's look back at the naming rules of OpenGL library functions. All OpenGL functions use the following format:

<Database prefix> <root command> <number of optional parameters> <optional parameter types>

The database prefixes include gl, glu, aux, glut, wgl, and glx, indicating that the function belongs to a development library of OpenGL, the number of parameters required and the type of parameters can be seen from the function name. I indicates the int type, f indicates the float type, d indicates the double type, and u indicates the unsigned integer type. Note that some function parameter types are suffixed with numbers 2, 3, and 4. 2 represents two-dimensional, 3 represents three-dimensional, and 4 represents the alpha value (described later ). Some OpenGL functions end with a letter v, indicating that a function parameter can be used as a pointer to a vector (OR array) to replace a series of individual parameter values. The following two formats indicate that the current color is set to red, which is equivalent to the two.

GlColor3f (1.0, 0.0, 0.0); equivalent:

Float color_array [] = {1.0, 0.0, 0.0 };
GlColor3fv (color_array );

In addition to the above basic naming method, there is also a representation with "*" asterisks, such as glColor * (), which indicates that the current color can be set in various methods of the function. Similarly, glVertex * v () indicates that a pointer points to all types of vectors to define a series of vertex coordinate values.

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.