OpenGL color rotating cube

Source: Internet
Author: User

 

 /*  ** 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  ;} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.