/* ** Color RotationCubeBody * 1. Define six faces and define the color of each vertex (corresponding to the vertex coordinate) * 2. Enable gl_smooth rendering ** */ # Include <Stdio. h> # Include <GL \ glut. h> Static Float Xrot = 0.0 ; Static Float Yrot = 0.0 ; Static Float Zrt = 0.0 ; Void Cube () {glbegin (gl_quads); glcolor3f ( 1.0 , 1.0 , 0.0 ); Glvertex3f ( 1.0 , 1.0 ,- 1.0 ); Glcolor3f ( 0.0 , 1.0 , 0.0 ); Glvertex3f ( - 1.0 , 1.0 ,- 1.0 ); Glcolor3f ( 0.0 , 1.0 , 1.0 ); Glvertex3f ( - 1.0 , 1.0 , 1.0 ); Glcolor3f ( 1.0 , 1.0 , 1.0 ); Glvertex3f ( 1.0 , 1.0 , 1.0 ); Glcolor3f ( 1.0 , 0.0 , 1.0 ); Glvertex3f ( 1.0 ,- 1.0 , 1.0 ); Glcolor3f ( 0.0 , 0.0 , 1.0 ); Glvertex3f ( - 1.0 ,- 1.0 , 1.0 ); Glcolor3f ( 0.0 ,0.0 , 0.0 ); Glvertex3f ( - 1.0 ,- 1.0 ,- 1.0 ); Glcolor3f ( 1.0 , 0.0 , 0.0 ); Glvertex3f ( 1.0 ,- 1.0 ,- 1.0 ); Glcolor3f ( 1.0 , 1.0 , 1.0 ); Glvertex3f ( 1.0 , 1.0 , 1.0 ); Glcolor3f ( 0.0 , 1.0 , 1.0 ); Glvertex3f ( -1.0 , 1.0 , 1.0 ); Glcolor3f ( 0.0 , 0.0 , 1.0 ); Glvertex3f ( - 1.0 ,- 1.0 , 1.0 ); Glcolor3f ( 1.0 , 0.0 ,1.0 ); Glvertex3f ( 1.0 ,- 1.0 , 1.0 ); Glcolor3f ( 1.0 , 0.0 , 0.0 ); Glvertex3f ( 1.0 ,- 1.0 ,- 1.0 ); Glcolor3f ( 0.0 , 0.0 , 0.0 ); Glvertex3f ( - 1.0 ,- 1.0 ,- 1.0 ); Glcolor3f ( 0.0 , 1.0 , 0.0 ); Glvertex3f ( - 1.0 , 1.0 ,- 1.0 ); Glcolor3f ( 1.0 , 1.0 , 0.0 ); Glvertex3f ( 1.0 , 1.0 ,- 1.0 ); Glcolor3f ( 0.0 , 1.0 ,1.0 ); Glvertex3f ( - 1.0 , 1.0 , 1.0 ); Glcolor3f ( 0.0 , 1.0 , 0.0 ); Glvertex3f ( - 1.0 , 1.0 ,- 1.0 ); Glcolor3f ( 0.0 , 0.0 , 0.0 ); Glvertex3f ( - 1.0 ,- 1.0 ,- 1.0 ); Glcolor3f ( 0.0 , 0.0 , 1.0 ); Glvertex3f ( - 1.0 ,- 1.0 , 1.0 ); Glcolor3f ( 1.0 , 1.0 , 0.0 ); Glvertex3f ( 1.0 , 1.0 ,- 1.0 ); Glcolor3f ( 1.0 , 1.0 , 1.0 ); Glvertex3f ( 1.0 , 1.0 , 1.0 ); Glcolor3f ( 1.0 , 0.0 , 1.0 ); Glvertex3f ( 1.0 ,- 1.0 , 1.0 ); Glcolor3f ( 1.0 ,0.0 , 0.0 ); Glvertex3f ( 1.0 ,- 1.0 ,- 1.0 ); Glend ();} Void Display ( Void ) {Glclear (gl_color_buffer_bit | Gl_depth_buffer_bit); glloadidentity (); gltranslatef ( 0 , 0 ,-5 ); Glrotatef (xrot, 1 , 0 , 0 ); Glrotatef (yrot, 0 , 1 , 0 ); Glrotatef (Zrt, 0 , 0 , 1 ); // Glpolygonmode (gl_front, gl_line ); Cube (); xrot = Xrot + 1 ; Yrot = Yrot + 1 ; Zrt = Zrt + 1 ; Fig ();} Void Reshape ( Int W, Int H ){ If (H = 0 ) H =1 ; Glviewport ( 0 , 0 , (Glsizei) W, (glsizei) H); glmatrixmode (gl_projection); glloadidentity (); gluperspective ( 45.0 , (Glfloat) W/(glfloat) H, 0.1 , 100.0 ); Glmatrixmode (gl_modelview );} Void Init ( Int Width, Int Height ){ If (Height = 0 ) Height = 1 ; Glclearcolor ( 0.0 , 0.0 , 0.0 , 0.0 ); Glcleardepth ( 1.0 ); Gldepthfunc (gl_less); glable (gl_depth_test); glshademodel (gl_smooth); glmatrixmode (gl_projection); glloadidentity (); gluperspective ( 45.0 , (Glfloat) width/(glfloat) height,1 , 100.0 ); Glmatrixmode (gl_modelview );} Void Keyboard (unsigned Char Key, Int W, Int H ){ If (Key = ' F ' ) // Enter full screen Glufullscreen (); If (Key = ' F ' ) // Exit full screen {Glureshapewindow ( 640 , 480 ); // Set window size (initialization function unavailable) Glupositionwindow ( 400 , 100 ); // Set the window location (initialization function is unavailable) } If (Key = 27 ) // Exit ESCProgram Exit ( 0 );} Int Main ( Int Argc, Char ** Argv) {gluinit ( & Argc, argv); gluinitdisplaymode (glu_rgba | Glu_double |Glut_depth); gluinitwindowposition ( 400 , 100 ); Maid ( 640 , 480 ); Fig ( "" ); Fig (Display); glutidlefunc (Display); fig (reshape); fig (keyboard); Init ( 640 , 480 ); Fig (); Return 0 ;}