Lesson 01st OpenGL window (1)

Source: Internet
Author: User

This section of the tutorial was completely rewritten in January 2000. This will teach you how to set up an OpenGL window. It can be a window or full screen, can be any size, any color depth. The code here is stable and powerful. You can use it in all your OpenGL projects. All my tutorials will be based on the code in this section! All errors are reported. Therefore, there should be no memory leakage and the code can be easily read and modified. Thanks to Fredric Echols for making changes to the code!

 

Now let's start with the code. The first thing is to open the VC and create a new project. If you do not know how to create it, you may not have to learn OpenGL. Instead, you should first learn VC. In some versions of VC, you need to change bool to bool, true to true, and false to false. Modify it by yourself.

 

After you create a new Win32 Program (not a console Program), you also need to link the OpenGL library file. In VC, perform the following operations: Project-> Settings, and then click the link tag. Add opengl32.lib glu32.lib and Glaux. Lib at the beginning of the "Object/library modules" option (before kernel32.lib), and click OK. Now you can start writing your OpenGL program.

 

The first four lines of the Code include the header files of each library file we use. As follows:

# Include <windows. h> // header file # include <Gl/GL. h> // header file for the opengl32 library contains the latest GL. h, Glu. h library # include <Gl/glut. h> // header file for the glu32 library contains the OpenGL utility library // # include <GL \ Glaux. h> // header file for the Glaux Library

Next, you need to set all the variables you plan to use in your program. The routine in this section will create an empty OpenGL window, so we do not need to set a large number of variables for the moment. There are not many variables to be set, but they are very important. You will use them in every OpenGL program you write in the future.

The variable set in the first row is rendering context (coloring description table ). Each OpenGL is connected to a coloring description table. The coloring description table connects all OpenGL calling commands to the device context (device description table. I define the OpenGL coloring description table as HRC. To allow your program to draw a window, you also need to create a device description table, that is, the content of the second row. The device description table for Windows is defined as HDC. DC connects the window to the GDI (Graphics Device Interface graphical device interface ). RC connects OpenGL to DC. The variable hwnd in the third line will save the handle assigned by windows to our window. Finally, the fourth action is to create an instance for our program ).

Hglrc HRC = NULL; // window coloring description table handle HDC = NULL; // OpenGL rendering description table handle hwnd = NULL; // save our window handle hinstance; // Save the program instance

The first line below sets an array to monitor keyboard movements. There are many ways to monitor the movements of the keyboard, but the method here is very reliable and can process the simultaneous press of multiple keys.

The active variable is used to indicate whether the program window is in the minimized state. If the window has been minimized, we can do anything from suspending code execution to exiting the program. I like to pause the program. This prevents the program from running in the background. The fullscreen variable has obvious functions. If our program runs in full screen mode, the value of fullscreen is true; otherwise, the value is false. The setting of this global variable is very important. It makes every process know whether the program is running in full screen status.

Bool keys [256]; // The bool active array for saving keyboard keys = true; // The activity flag of the window. The default value is truebool fullscreen = true; // The default value of the full screen flag is true, the default mode is full screen.

Now we need to define wndproc () first (). The reason for this is that createglwindow () has a reference to wndproc (), but wndproc () only appears after createglwindow. In C language, if we want to access the process and segment after the current program segment, we must declare the program segment to be accessed at the beginning of the program. Therefore, the following code first defines wndproc (), so that createglwindow () can reference wndproc ().

Lresult callback wndproc (hwnd, uint, wparam, lparam); // wndproc Definition

The purpose of the following code is to reset the size of the OpenGL scenario, regardless of whether the window size has changed (assuming that you do not use full screen mode ). Even if you cannot change the window size (for example, in full screen mode), it will run at least once-set our perspective at the beginning of the program. The size of the OpenGL scenario is set to the size of the window in which it is displayed.

Glvoid resizeglscene (glsizei width, glsizei height) // reset the size of the OpenGL window {If (Height = 0) // prevent division by zero {Height = 1; // set height to 1} glviewport (0, 0, width, height); // reset the current view

The following behavior perspective settings screen. It means that the farther the object looks smaller. This creates a realistic appearance scenario. In this example, the perspective is calculated based on the 45-degree view of the window width and height. 0.1f and 1001_f are the starting and ending points of depth that can be drawn in the scene.

Glmatrixmode (gl_projection) indicates that the next two lines of code will affect projection matrix (projection matrix ). The projection matrix is responsible for adding perspective for our scenarios. Glloadidentity () is similar to resetting. It restores the selected matrix state to its original state. After calling glloadidentity (), we can set a perspective for the scenario. Glmatrixmode (gl_modelview) indicates that any new transformation will affect modelview matrix ). The model observation matrix stores our object information. Finally, we reset the model observation matrix. If you still cannot understand the meaning of these terms, don't worry. I will explain it to you in future tutorials. You only need to know if you want to get a wonderful perspective scenario.

Glmatrixmode (gl_projection); // select the projection matrix glloadidentity (); // reset the projection matrix // set the size of the PV gluperspective (45.0f, (glfloat) width/(glfloat) height, 0.1f, 100366f); glmatrixmode (gl_modelview); // select the model observation matrix glloadidentity (); // reset the model observation matrix}

In the next code segment, we will make all the settings for OpenGL. We will set the color used to clear the screen, enable the deep cache, enable smooth shading, and so on. This routine will not be called until the OpenGL window is created. This process will return values. However, the initialization here is not that complex, and you do not need to worry about the return value.

Int initgl (glvoid) // All OpenGL settings are started here {

Enable smooth shading in the next line ). Shadow is smoothed by the fine-grained polygon mixed colors and the external light is smoothed. I will explain shadow smoothing in more detail in another tutorial.

Glshademodel (gl_smooth); // enables shadow smoothing.

The next line sets the color used to clear the screen. If you do not know how the color works, I will explain it quickly. The color value ranges from 0.0f to 1.0f. 0.0f indicates the worst case, while 1.0f indicates the brightest case. The first parameter after glclearcolor is red intensity (Red weight), the second is green, and the third is blue. The maximum value is 1.0f, indicating the brightest condition of a specific color component. The last parameter is the Alpha value. When it is used to clear the screen, we do not need to care about the fourth number. Set it to 0.0f. I will use another tutorial to explain this parameter.

By mixing three primary colors (red, green, and blue), you can get different colors. Hope you have learned this in school. Therefore, when you use glclearcolor (0.0f, 0.0f, 1.0f, 0.0f), you will clear the screen in bright blue. If you use glclearcolor (0.5f, 0.0f, 0.0f, 0.0f), you can clear the screen in red. It is neither the brightest (1.0f) nor the lowest (0.0f ). To get a white background, set all colors to the brightest (1.0f ). To use a black background, set all colors to the lowest (0.0f ).

Glclearcolor (0.0f, 0.0f, 0.0f, 0.0f); // black background

The next three lines must be about the depth buffer (deep cache. Imagine the deep cache as the layer behind the screen. The deep cache keeps tracking the depth of the object into the screen. Our programs in this section do not actually use deep caching, but almost all OpenGL programs that display 3D scenes on the screen use deep caching. Its sorting determines that the object is first painted. In this way, you will not draw a square behind a circle to a circle. Deep cache is an important part of OpenGL.

Glcleardepth (1.0f); // sets the deep cache glable (gl_depth_test); // enable the gldepthfunc (gl_lequal); // type of the deep Test

Then we will tell OpenGL that we want to make the best perspective correction. This will slightly affect performance. But it makes the perspective look better.

Glhint (gl_perspective_correction_hint, gl_nicest); // notify the system to correct the perspective.

Finally, we return true. If we want to check whether the initialization is OK, we can check whether the returned value is true or false. If an error occurs, you can add your own code to return false. Currently, we don't care about it.

Return true; // initialize OK}

 

Lesson 01st OpenGL window (1)

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.