A simple program for drawing squares: # include <Gl/glut. h> void display () {glclear (gl_color_buffer_bit); glbegin (gl_polygon); glvertex2f (-0.5,-0.5); glvertex2f (-0.5, 0.5); glvertex2f (0.5, 0.5); glvertex2f (0.5,-0.5); glend (); glflush ();} int main (INT argc, char ** argv) {gluinit (& argc, argv ); the following result is displayed after the ngcreatewindow ("test"); ngdisplayfunc (Display); ngmainloop (); Return 0;} is run.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/8C/wKioL1P8RIbTAALXAAByBAZJdGI928.jpg "Title =" qq20140826161925.png "alt =" wkiol1p8ribtaalxaabybazjdgi928.jpg "/>
In the above program, main () is responsible for OpenGL initialization. The display () function defines the object to be drawn.
First, let's look at the main () function.
Void gluinit (int * argc, char ** argv)
Initialize glut, which should be called before other glut and OpenGL functions. Accept the parameters of the main function.
Int glucreatewindow (char * title)
Create a window on the screen. The title of the window is given by title.
Void gludisplayfunc (void (* func (void )))
The function func () is called every time the window needs to be repainted.
Void glumainloop ();
Enable the program into an infinite event processing loop.
Let's look at the content in the display () function.
Void glclear (glbitfield mask)
Clear all the bits in the cache indicated by the mask to obtain the expected results.
Void glbegin (glenum Mode)
Specify the vertex to be drawn between the two. Mode can be gl_points, gl_lines, or gl_polygon.
Void glend ()
Glvertex <2, 3, 4> <B I f d> () describes the vertex. There can be multiple forms, or vectors,
Void glflush ()
Execute all OpenGL commands. This sentence must be added after the drawing operation.
Modify the default value in glut:
Void gluinitdisplaymode (unsigned int Mode)
Set the window attribute to mode. The mode value options are as follows: color mode (glu_rgb, glu_index) and color cache slow storage mode (glu_single, glu_double). The default values are RGB and single.
Void gluinitwindowsize (int w, int H) is used to set the width and height of the window.
Void gluinitwindowposition (int x, int Y) is used to set the upper-left corner of the window to the upper-left corner of the screen.
Color settings:
Void glcolor3 <B I f d...> (R, G, B)
Void glcolor3 <B I f d...> V (color) // set the painting color
Void glclearcolor (R, G, B, A) // specify the screen clearing color. Call glclear () to make the settings take effect.
Differences between OpenGL and glut Coordinate Systems
In OpenGL, the positive directions of the X axis and Y axis are upward and right, respectively, and in glut, they are downward and right.
Two-dimensional framing
Void gluortho2d (gldouble left, gldouble right, gldouble bottom, gldouble top)
Specify a two-dimensional cropping area. The coordinates in the lower left corner are (left, bottom), and those in the upper right corner are (right, top ). Objects located in the window are visible, and objects falling outside the window cannot be "clipped ".
View
Use the viewport to restrict OpenGL from painting only in any part of the screen window.
Void glviewport (glint X, glint y, glsizei W, glsizei H)
Set the width to W, the height to h, and the coordinate of the sitting angle to (x, y ).
Coordinate System and Transformation
Object coordinate system or world coordinate system: this coordinate system is used by developers. Which unit can be selected by yourself?
Window coordinate system or screen coordinate system: in pixels. It is determined by the properties of the physical Monitor and the area of the monitor.
Coordinate System Transformation in OpenGL is determined by two matrices:
1. Fuzzy View Matrix 2. projection view Matrix
The gluortho2d () function is used to specify a projection matrix for a two-dimensional application.
Typical steps for setting these two matrices include:
1. Specify the matrix we want to modify.
2. Reset the Matrix to the unit matrix,
3. modify the current matrix to the expected matrix.
Glmatrixmode (gl_projection)
Glloadindentity ();
Gluortho2d (-1.0, 1.0,-1.0, 1.0 );
Complete the procedure at the beginning:
# Include <Gl/glut. h> void Init () {glclearcolor (0.0, 0.0, 0.0, 0.0); // specify that the screen background is black glcolor3f (1.0, 1.0, 1.0 ); // set the painting color to white glmatrixmode (gl_projection); // specify the matrix to be modified as the projection matrix glloadidentity (); // reset to the unit matrix gluortho2d (-1.0, 1.0, -1.0, 1.0); // set the two-dimensional cropping area. The coordinates in the lower left corner are (-), and the coordinates in the upper right corner are ()} void display () {glclear (gl_color_buffer_bit ); // clear the screen color as our specified glbegin (gl_polygon); // The draw mode is convex polygon glvertex2f (-0.5,-0.5); glvertex2f (-0.5, 0.5 ); glvertex2f (0.5, 0.5); glvertex2f (0.5,-0.5); // specify the four drawn vertices glend (); glflush (); // force the preceding plotting operation to execute} int main (INT argc, char ** argv) {gluinit (& argc, argv); // initialize the maid mode (glu_single | glu_rgb ); // set the window mode to single-buffer and RGB mode to gluttwindowsize (500,500); // set the window size to gluttbucket ("test"); // set the window title to gluttfunc (Display ); // set the plot callback function: FIG (); // starts the loop and waits for the response Init (); Return 0 ;}
The following result is displayed:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/8C/wKiom1P8VRODsICHAACjNjb3bX0222.jpg "Title =" qq20140826173353.png "alt =" wkiom1p8vrodsichaacjnjb3bx0222.jpg "/>
OpenGL Learning (1) A simple rectangular Program