First compile and run a simple example so we can have an intuitive impression. From this example we can see what OpenGL can do, and of course This example does just one simple thing-draw a colored triangle. In addition, we can see the typical OpenGL program structure and the operation sequence of OpenGL.
Example 1: This example draws a colored triangle in the background of black, as shown in figure one.
#include <stdlib.h> #include <GL/glut.h> void background (void) { Glclearcolor (0.0,0.0,0.0,0.0);/set background color to black } void Mydisplay (void) { Glclear (gl_color_buffer_bit);//buffer set to Color writable Glbegin (gl_triangles)/start to draw a triangle Glshademodel (Gl_smooth);//set to smooth shading mode glcolor3f (1.0,0.0,0.0);/Set the first vertex to red GLVERTEX2F ( -1.0,-1.0)//Set the coordinates of the first vertex ( -1.0,-1.0) glcolor3f (0.0,1.0,0.0);//Set the second vertex to green GLVERTEX2F (0.0,-1.0);//Set the coordinates of the second vertex (0.0,-1.0) glcolor3f (0.0,0.0,1.0);/set third vertex to blue GLVERTEX2F ( -0.5,1.0);//Set the coordinates of the third vertex ( -0.5,1.0) Glend ()//triangle End Glflush ()//force OpenGL function to run in a limited time } void Myreshape (Glsizei w,glsizei h) { Glviewport (0,0,W,H)//Set viewport Glmatrixmode (gl_projection)//indicates that the current matrix is gl_projection Glloadidentity ()///To replace the current matrix with a unit array if (w <= h) Gluortho2d ( -1.0,1.5,-1.5,1.5* (glfloat) h/(glfloat) w);//define two-dimensional face projection matrix Else Gluortho2d ( -1.0,1.5* (glfloat) w/(glfloat) h,-1.5,1.5); Glmatrixmode (Gl_modelview)//indicates that the current matrix is Gl_modelview } int main (int argc,char * * argv) { /* Initialize * * Glutinit (&ARGC,ARGV); Glutinitdisplaymode (glut_single| GLUT_RGB); Glutinitwindowsize (400,400); Glutinitwindowposition (200,200); /* Create window * * Glutcreatewindow ("triangle"); /* Draw and display * * Background (); Glutreshapefunc (Myreshape); Glutdisplayfunc (Mydisplay); Glutmainloop (); return (0); } |
Figure I: A colored triangle |
The first step is to create the project, which is as follows:
1 Create a Win32 Console application.
2) Link OpenGL libraries. In Visual C + +, click Project First, click Settings, then locate link click, and finally add OpenGL32.lib GLu32.lib to the front of the Object/library modules GLaux.lib
3) In Project settings, click the C + + tag to change the _console in the preprocessor definitions to __windows. Last Click OK.
Now you can copy the example below to the project and compile and run it. You can see a colored triangle.
Let's look at the main function first. Functions that begin with glut are included in the glut.h. The functions of the glut library mainly perform tasks such as dealing with multiple window drawing, handling callback-driven events, generating cascading pop-up menus, drawing bitmap fonts and stroke fonts, and various window management.
The glutinit is used to initialize the glut library and negotiate with the Window System dialog.
Glutinitdisplaymode is used to determine the display mode of the window being created. In this example, the parameter glut_single refers to the Order Cache window, which is also the default mode, and the corresponding mode is glut_double Dual Cache window. The parameter Glut_rgb specifies the color RGBA mode, which is also the default mode, which corresponds to the Glut_index Color Index mode window.
glutinitwindowsize initializes the window's size, the first parameter is the width of the window, and the second parameter is the height of the window, in pixels.
Glutinitwindowposition Sets the position of the initial window, the first parameter is the coordinates of x in the upper-left corner of the window, and the second parameter is the coordinates of y in the upper-left corner of the window, in pixels. The coordinates of the upper left corner of the screen are (0,0), the horizontal axis increases to the right, and the ordinate downward increases gradually.
Glutcreatewindow creates the top-level window, whose name is the parameter in the expansion number.
background () This is the function you wrote, setting the background. In fact, the statements in this function can be written in the display function, but in order to make the function block clearer, this part of the background is presented separately.
Glutreshapefunc registers the shape change callback function for the current window. When you change the window size, the shape of the window changes the callback function to be invoked. In this case, the Myreshape specifies the shape change function.
Glutdisplayfunc Registers the display callback function for the current window. When the image layer of a window needs to be redrawn, glut invokes the display callback function for that window. The Mydisplay in this example shows a callback function that shows the callback function with no arguments, and it is responsible for drawing the entire image layer. Most of our work will be concentrated in this function.
Glutmainloop into the glut event-handling loop. The Glutmainloop function can only be invoked at most once in the glut program, and it no longer returns once it is invoked, and calls the registered callback function. So this function must be placed behind the registration callback function, in this case Glutreshapefunc, Glutdisplayfunc.