The principle of OpenGL programming under Win 95/nt

Source: Internet
Author: User
Tags error code thread windows support

----The visualization of Scientific computing, computer animation and virtual reality are now three hot spots in computer graphics. The core of these three hotspots is the drawing of three-dimensional realistic graphics. Because OpenGL (opengraphicslibrary) is cross-platform, simple, efficient and functional, it has become the de facto industrial standard in the three-dimensional graphic production method. Since WindowsNT3.51 supports OpenGL on a microcomputer platform, Microsoft is now providing an OpenGL development environment in WINDOWS95OSR2 and WindowsNT4.0. Visualc++ has fully supported the OpenGL API since version 4.2, making the "civilian" of the three-dimensional world inevitable.

----The Windows operating system support for OpenGL

----with Windows programming experience, it is known that using GDI to draw in Windows requires the corresponding function to be called through the device context (Devicecontext shorthand DC); The OpenGL function is similar to the rendering context. (Renderingcontext abbreviated RC) to complete the drawing of three-dimensional graphics. window and device contexts under Windows support the bitmap format (PixelFormat) attribute, which is consistent with the bitmap structure of the RC. As long as the RC to create a connection with a DC (RC can only be created through the bitmap format of the DC to create), the function of OpenGL can be through the RC corresponding DC painting to the corresponding display device. Here are the following areas to be noted:

----1. A thread can only have one rendering context (RC), that is, if the user works on a single line agenda the different devices, it can only be done by replacing the DC corresponding to the RC, while the RC threads remain unchanged. (Of course, delete the old RC and then create a new one is OK) with this, an RC can only belong to one thread and cannot be shared by different threads at the same time.

----2. Setting the DC bitmap format is equal to the bitmap format of the corresponding window, and the bitmap format of the DC and window can no longer be changed once it is determined. This can only be expected to improve on later versions of Windows.

----3. Although a RC can be replaced by a DC, at any time only one DC (which is called the current DC of the RC) can be used, but because a window allows multiple DCs to be plotted so that multiple threads can use multiple RC to perform OpenGL operations on the window.

----4. The OpenGL version under Windows now has some limitations on how OpenGL and GDI are plotted on the same DC. When using the dual cache to animate with OpenGL, you cannot use GDI functions to draw to the DC.

----5. It is not recommended to use Ansic to write OpenGL programs under Windows. While such programs have cross-platform portability (such as many SGI example programs), they cannot take advantage of many of the features of Windows operating systems and are of little practical value.

----Use VC to write OpenGL program

----Through the above analysis, using VC to invoke OpenGL mapping method is very obvious. The steps are as follows:

----1. Set the bitmap format (PixelFormat) property of the display device DC first. This is done by populating a pixelformatdescriptor structure (the meaning of the data in the Pixelformatdescriptor, please refer to the help of VC), which determines the properties of the physical device for OpenGL drawing. For example, in the structure of the data items in the dwflags pfd_doublebuffer bit if not set (1), through the device's DC on the mapping of OpenGL command can not use double buffering to do animation. Some of the bitmap formats (PixelFormat) are supported by DCs, and some DCs are not supported. So the program must first use Choosepixelformat to select the bitmap format that the DC supports closest to the specified bitmap format, and then use Setpixelformat to set the bitmap format for the DC.

----2. Use the device DC to establish a rendering context RC (Wglcreatecontext) to enable RC to connect with DCS (wglmakecurrent).

----3. Call the OpenGL function to draw the graph. Because the thread corresponds to the RC one by one, the parameters of the OpenGL function do not indicate the handle of this thread RC (handle)

----4. After the completion of the mapping, first by placing the current thread's RC to NULL (:: Wglmakecurrent (Null,null);), disconnect the current thread from the render context, thereby disconnecting the DC from the connection. The validity of the RC handle is not clearly stated in Microsoft's own document, so the validity of the following RC handle should be judged first when the RC is deleted (if (M_HRC):: Wgldeletecontext (M_HRC);). Release (RELEASEDC) or delete (DELETEDC) DC based on conditions

Description of the procedures attached----

----the attached program with MFC to complete a simple OpenGL mapping, using OpenGL Auxiliary Library to draw a solid sphere with light. The function of OpenGL itself is not explained here, only the content that needs attention when using MFC to weave OpenGL is made a brief explanation:

----1. Once a bitmap format is set for a DC, the bitmap format of the window associated with the DC is set. If the window contains a child window or a sibling window, the bitmap format of these sibling/child windows is not set to correspond to the corresponding RC format, OpenGL is easy to make mistakes in the drawing. Therefore OpenGL drawing window must have the Ws_clipchildren and the ws_clipsiblings style, in the program in the main frame window constructor function uses the LoadFrame (idr_mainframe,ws_overlappedwindow| ws_clipchildren| Ws_clipsiblings,null,null); Specifies the style of the main window.

----2. In Ansic OpenGL programming, a callback function for setting OpenGL viewport size and drawing size is defined by Auxreshapefunc. In MFC, it should be done by the processing function of the WM_SIZ message. In Ansic OpenGL programming, a callback function that is defined by the Eauxmainloop. In MFC, it should be handled by the processing function of the WM_PAINT message. Accordingly, the keyboard and mouse handling functions defined by OpenGL should be responded to by the corresponding Windows processing functions.

----3.OpenGL has a function glclear that refreshes the background, you should prevent Windows from refreshing the window background. Otherwise, Windows automatically sends WM_ERASEBKGND when the window needs to be repainted, and the default handler uses a white background brush. When OpenGL uses a background color that is not white, there is a frame of white flicker in the drawing. This phenomenon is particularly noticeable when animating. In the program only need to prohibit the parent window class message processing in the WM_ERASEBKGND message processing function, simply returns a true.

----4. Because of the cross-platform nature of OpenGL, it must use the palette of the operating system. So if gl_index_mode, you have to use VC to define your own palette. However, in general, it is convenient to use Gl_rgba_mode mode, rarely used in Gl_index_mode mode.

----5. During OpenGL mapping, the RC's corresponding DC cannot be deleted or released.

----6. It is advisable to set the drawing window class to CS_OWNDC as OpenGL takes up a long time to occupy the DC. MFC default window class style does not set this property, the program in the main window C + + class PreCreateWindow method of registering a window class, in addition to setting the CS_OWNDC attribute, but also set the Cs_hredraw, Cs_vredraw and Cs_ Savebits. Set Cs_hredraw, Cs_vredraw is to make the window zoom generated WM_PAINT message, correction OpenGL viewport and drawing dimensions; Because OpenGL mapping requires a lot of calculation, set Cs_ Savebits is to be displayed when the OpenGL window is obscured, does not produce WM_PAINT message, uses the memory to store the image to fill, thus uses the space consumption in exchange for the computation time.

----7. This procedure does not have the OpenGL function error situation to deal with. OpenGL error returned error code, does not throw an exception, and after a function error, the successor function will generally not be abnormal, but return the error code, inadvertently may ignore some errors. And for each OpenGL function is to make a mistake or not to judge more trouble, so the process of editing the OpenGL function should be very careful.

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.