Basic OpenGL technology lecture-Architecture

Source: Internet
Author: User

OpenGL is a set of graphics standards. It is designed in strict accordance with the principles of computer graphics and conforms to the optical and visual principles, and is very suitable for visual simulation systems.

First, OpenGL allows visual objects to be expressed in graphics, such as geometric models composed of coordinate sets of object surface vertices. Such graphic data contains rich geometric information, the simulated image can fully express its physical features. in OpenGL, there is a geometric transformation for the vertices represented by three-dimensional coordinates, through this transformation, the vertex can be translated and rotated in three dimensional space. For objects expressed by the vertex set, the movement in space can be realized.

Secondly, OpenGL can express the three-dimensional characteristics of objects through illumination. Its Illumination Model is the overall illumination model, it takes parameters such as the distance from vertex to the light source, the direction vector from vertex to the light source, and the direction vector from vertex to the viewpoint into this model to calculate the vertex color. Therefore, the color of the visual simulation image reflects the spatial location relationship between the object and the viewpoint and the light source, and has a strong three-dimensional effect.

In addition, OpenGL provides a method to use image data, that is, to directly read, write, and copy image data, or define image data as a combination of texture and graphic methods to generate visual images to enhance the effect. To enhance the computing capability of 3D graphics in computer systems, related manufacturers have developed a 3D Graphics Accelerator card dedicated to OpenGL acceleration. Its effect is comparable to that of graphics workstations.
The OpenGL Graphic Processing System of a complete window system is structured as: the bottom layer is the graphic hardware, the second layer is the operating system, the third layer is the window system, and the fourth layer is OpenGL, the fifth layer is the application software. See Figure 1.1.

Figure 1.1 level of OpenGL graphics processing system

OpenGL is network-transparent. In the client/server architecture, OpenGL can be called locally or remotely. Therefore, in the network system, OpenGL can appear in an independent graphical window in X Windows, windows, or other Windows systems.
Since OpenGL is a platform-independent 3D graphic interface, the operating system must provide pixel format management and rendering environment management. The following uses Windows NT as an example to describe the architecture of OpenGL.
OpenGL is implemented in the Client/Server mode on Windows NT. The application sends OpenGL commands, which are received and packaged by the dynamic link library opengl32.dll and sent to the winsrv on the server. DLL, which is then sent to the video display driver through the DDI layer. If the system has a hardware accelerator installed, it is handled by the hardware-related DDI.
The architecture 1.2 of OpenGL/NT is shown in.
From the programmer's point of view, we must clear two obstacles before writing Windows-based OpenGL applications. OpenGL itself is a complex system, this can be learned and mastered through the simplified OpenGL auxiliary library functions; the other is that you must have a clear understanding of the windows and OpenGL interfaces.

Figure 1.2 OpenGL/NT Architecture

1.3.2 rendering context (RC)
The drawing method of OpenGL is different from that of Windows. The differences are mainly manifested in the following three aspects:
(1) Windows uses the GDI plot;
(2) OpenGL uses rendering context RC (render context, also known as rendering description table) plotting;
(3) OpenGL uses a special pixel format.
When using GDI for drawing in Windows, you must specify the device context (device context, also known as the device description table) in which the drawing is performed. Similarly, when using OpenGL functions, you must specify a so-called rendering context. Just as the device context DC needs to store the drawing environment information of GDI, such as pen, brush, and font, the rendering context RC must also store the rendering information required by OpenGL, such as the pixel format.
The rendering context is mainly managed by the following six WGL functions, which are described below.
L hglrc wglcreatecontext (HDC)
This function is used to create a rendering context RC available for OpenGL. HDC must be a valid handle that supports at least 16 colors of the DC or memory device description table. Before calling this function, you must set the appropriate pixel format in the device description table. After a rendering context is created, HDC can be released or deleted. If the function returns a null value, it indicates a failure. Otherwise, the return value is the handle of the rendering context.
2 bool wgldeletecontext (hglrc)
This function deletes an RC. Generally, an application should make it a non-current RC before deleting the RC. However, you can also delete an existing RC. In this case, the OpenGL system clears the waiting drawing command and turns it into a non-current RC, and then deletes it. Note: an attempt to delete an RC belonging to another thread may cause a failure.
3 hglrc wglgetcurrentcontext (void)
This function returns the current RC of the thread. If the thread does not have the current RC, null is returned.
4 HDC wglgetcurrentdc (void)
This function returns the DC associated with the current RC of the thread. If the thread does not have the current RC, null is returned.
5 bool wglmakecurrent (HDC, hglrc)
This function associates HDC with hglrc and makes hglrc the current RC of the calling thread. If the value passed to hglrc is null, the function is unassociated, and the current RC of the concurrent thread is not the current RC. In this case, the HDC parameter is ignored.
The HDC passed to this function can not be the value used when wglcreatecontext is called. However, the devices they are associated with must have the same pixel format. NOTE: If hglrc is the current RC of another thread, the call fails.
6 bool wglusefontbitmaps (HDC, DWORD dwfirst, DWORD dwcount, DWORD dwbase)
This function uses the current HDC font to create a series of display tables with specified range characters. You can use these display tables to draw GDI text in the OpenGL window. If the OpenGL window is double-buffered, this is the only way to draw the GDI text in the buffer.
Generally, in an application that uses a single RC, the RC is created in the corresponding wm_create message and deleted when wm_close or wm_destroy arrives. Before drawing in a window using OpenGL commands, you must create an RC and make it the current RC. The OpenGL command does not need to provide RC and will automatically use the current RC. Without the current RC, OpenGL simply ignores all the drawing commands.
An RC refers to the current RC, which is for the calling thread. When a thread has the current RC for plotting, other threads cannot draw at the same time. A thread can have only one current RC at a time, but can have multiple RC; an RC can also be shared by multiple threads, but it can only be the current RC in one thread at a time.
When using the current RC, the associated DC should not be released or deleted. If an application maintains an existing RC throughout its life cycle, the application also occupies a DC resource. Note that Windows only has limited DC resources.
The following describes two methods for managing RC and DC.
Method 1: RC is created when the wm_create message responds. After the message is created, the DC is released immediately. When the wm_paint message arrives, the program obtains the DC handle and associates it with the RC. After the drawing is complete, immediately remove the association between RC and DC and release the DC. When the wm_destroy message arrives, the program simply deletes the RC. 1.3.

Figure 1.3 RC and DC management method 1

Method 2: RC is created at the beginning of the program and becomes the current RC. It will remain as the current RC until the program ends. Correspondingly, getdc is called at the beginning of the program, and releasedc is called at the end of the program. The advantage of this method is that when responding to the wm_paint message, you do not need to call the very time-consuming wglmakecurrent function. Generally, it consumes thousands of clock cycles. 1.4.
If the application requires animation or real-time graphics, the second method is used for creation.

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.