Reference OpenGL Programming Guide P17
Directly on the code:
<span style= "FONT-SIZE:18PX;" >//doubleBuffer.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <gl/glut.h>static glint spin = 0;void init () {glclearcolor (0.0,0.0,0.0,0.0); Glshademodel (Gl_flat);//Use a single coloring}void display () {glclear (gl_color_buffer_bit); glcolor3f (1.0,0.0,0.0); Glpushmatrix ( ); glrotated (spin,1.0,0.0,1.0);//GLRECTF (25.0,25.0,100.0,100.0); GLRECTF (0,0,1,1);//GLRECTF ( -0.25,- 0.25,0.25,0.25); Glpopmatrix (); Glutswapbuffers ();} void Spindisplay () {spin + = 2;spin = spin% 360;glutpostredisplay ();} void Mouse (int button,int state,int x,int y) {switch (button) {case Glut_left_button:if (state = = Glut_down) {Glutidlefunc ( Spindisplay);} Break;case glut_right_button:if (state = = Glut_down) {glutidlefunc (NULL);} Default:break;}} int _tmain (int argc, _tchar* argv[]) {Glutinit (&ARGC, (char**) argv); Glutinitdisplaymode (glut_double | GLUT_RGB); glutinitwindowsize (500,500); glutinitwindowposition (0,0); Glutcreatewindow ("Double buffering"); Init (); Glutdisplayfunc (display); Glutmousefunc (mouse); Glutmainloop (); return 0;} </span>
Some new functions are encountered during the writing process:
1.glShadeModel (Gl_flat); One more parameter is Gl_smooth (the default value). The difference is that when you assign a different color to the vertex of a polygon, the color in the matrix is single (Gl_flat), or
Smoothing Over (Gl_smooth).
2. glrotated function, rotation function, operation on the current matrix
3. Glpushmatrix ();
Glrotated (spin,1.0,0.0,1.0);
Glpopmatrix ()
The Glpushmatrix function copies a copy of the stack top matrix and then stacks the copied top matrix into the stack.
The Glpopmatrix function stacks the stack top matrix.
The rotation function is placed between the two functions in order to revert to the top matrix state of the stack before rotation.
Can't you read it? Here's an explanation of what's possible: http://blog.sina.com.cn/s/blog_70c3d9ed010122bp.html
4.GLRECTF function, pass two sets of coordinates in, one in the lower left corner, one in the upper right corner, draw a matrix out.
On the parameters of the matrix, the book gives the GLRECTF ( -25.0,-25.0,25.0,25.0) I think is wrong.
The GLRECTF parameter should be between 1 and 1. is the OpenGL world coordinate system coordinate, the origin is in the center of the screen. Do not know can Baidu.
5.glutIdleFunc sets a global callback function that GLUT program functions can perform background processing tasks or continuous animations when no window events arrive . If enabled, this idle function is called continuously until a window event occurs. The callback function has no arguments. The current window and menu will not change until the idle func is executed. It is best not to rely on the current settings when the program relies on multiple windows or menus.
6.
if (state = = Glut_down)//callback is due to a press (of a mouse button) Equivalent to "If a mouse key is pressed" if (state = = glut_up)//callback was due to a release (of a mouse button) Equivalent to "If a mouse key is released (bounce)" if (button== Glut_left_button)//callback was due to a release/press of The left button//is equivalent to "if the mouse button is pressed or released" if (button== glut_right_b Utton)//callback was due to a release/press of the right button Equivalent to "If the right mouse button is pressed or is released" if (button== Glut_middle_button)//callback is due to a release/press of the middle Button// Equivalent to "If the middle mouse button is pressed or released"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
See OpenGL Write code (3) to implement the rotation of the matrix