OpenGL official website (English) Http://www.opengl.org The following describes OpenGL programming in windows. Preparations before learning OpenGL Step 1: select a compiling environment Currently, mainstream windows compiling environments include Visual Studio, broland C ++ builder, Dev-C ++, and so on. They all support OpenGL. I chose Visual Studio 2008 and vc6 ++ as the learning environment for OpenGL. Step 2: Install the glut Toolkit Glut is not required by OpenGL, but it will bring some convenience to our learning. We recommend that you install it. Glut in Windows: (the size is about 150 K) Http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip If you cannot download from the above address, use the following connection: Http://upload.programfan.com/upfile/200607311626279.zip To install glut in Windows: 1. Unpack the downloaded package and obtain 5 files. 2, For VC ++ 6Copy glut. h to "X: \ Program Files \ microsoftvisualstudio \ vc98 \ include \ GL folder "). For visualstudio2008Copy glut. H to the X: \ Program Files \ Microsoft \ Visual Studio 9.0 \ Vc \ include \ GL folder. If there is no GL folder, you can create one by yourself. (X is the disk symbol for installing vs. If VC ++ is installed, there is a GL file in it, and Visual Studio 2008 needs to be created on its own) 3. decompress the extractedGLUT. LibAndGlu32.libPut it in the folder where the static function library is located (that isLib folder). 4. decompress the extractedGLUT. dllAndGlu32.dllPut it under the Operating System DirectoryIn the System32 folder. (The typical location is:C: \ windows \ system32)(This corresponds to the above question!) Step 3: Create an OpenGL Project Whether visualstudio2008 or VC ++ 6: Select File-> New-> Project, select Win32 console application (not Win32 application), select a name, and then press OK. Click application settings on the left of the displayed dialog box, locate the empty project, and click Finish. Add a code file named "OpenGL. c" to the project, and use. C as the end of the file. It's just like the normal project. The first OpenGL program A simple OpenGL program is as follows: (Note that if you need to compile and run the program, you must install glut correctly. The installation method is described above) Note: includes header files:
# Include <Gl/glut. h> # Pragma comment (Lib, "opengl32.lib") // You can also add "opengl32.lib" "glu32.lib" "glu32.lib" "glu32.lib" # Pragma comment (Lib, "glu32.lib") // you do not need to add these three lines of code here. Add method: # Pragma comment (Lib, "glu32.lib") // project-properties-configuration properties-linker-input-Additional dependency-> edit and add Void mydisplay (void) { Glclear (gl_color_buffer_bit ); Glrectf (-0.5f,-0.5f, 0.5f, 0.5f ); Glflush (); } Int main (INT argc, char * argv []) { Gluinit (& argc, argv ); Fig ); Gluinitwindowposition (100,100 ); Gluinitwindowsize (400,400 ); Valley createwindow ("first OpenGL program "); Gludisplayfunc (& mydisplay ); Glumainloop (); Return 0; } |
The function of this program is to draw a White Rectangle in the center of a black window. Each row of statements is described below. First, you need to include the header file # include <Gl/glut. h>, which is the header file of glut. Originally, OpenGL programs generally contain <Gl/Gl. h> and <Gl/Glu. h>, but the glut header file automatically contains these two files and does not need to be included again. Then look at the main function. Int main (INT argc, char * argv []). This is a main function with command line parameters. Have you ever seen it? If you have never seen any other comrades, please read more books and wait until you understand them. Note that all the statements in the main function, except the last return, start with glut. The functions starting with glut are all functions provided by the glut toolkit. The following describes several functions used. 1. gluinit: Initialize the glut. This function must be called once before other gluts are used. The format is relatively rigid. You can copy this keyword according to the format of gluinit (& argc, argv. 2. Set the display mode of the glutinitdisplaymode. In the display mode, the ng_rgb color indicates that the RGB color is used, and the corresponding value is the ng_index (indicating that the index color is used ). Glu_single indicates that a single buffer is used, and the corresponding value is glu_double (dual buffering is used ). For more information, Google. Of course, there will be some explanations in future tutorials. 3. Set the position of the window on the screen. 4. It is also simple to set the window size. 5. Create a window based on the information set above. The parameter is used as the title of the window. Note: The window is not displayed on the screen immediately after it is created. You need to call the glumainloop function to view the window. 6. Set a function to map a graph. The function is called. (This statement is not accurate enough, but it may be difficult for beginners to understand it. Let's talk about it for the time being ). 7. A message loop is implemented using the glumainloop. (This may not be quite understandable for beginners. Now you only need to know that this function can display a window and will return it only after the window is closed .) In the gludisplayfunc function, we set "Call mydisplay function when drawing is required ". The mydisplay function is used for drawing. Observe the three function calls in mydisplay and find that they all start with GL. These functions starting with GL are all OpenGL standard functions, which are described below. 1. glclear: Clear. Gl_color_buffer_bit indicates to clear the color. The glclear function can also clear other things, but it is not described here. 2. glrectf: Draw a rectangle. The four parameters represent the horizontal and vertical coordinates of the two points on the diagonal line. 3. glflush: ensure that the previous OpenGL commands are executed immediately (instead of waiting them in the buffer ). Its role is similar to that of fflush (stdout. |