Above
Step: First, transform the model perspective; Then, change the color; Finally, draw a cube with a vertex array.
The source code is as follows:
#include<GL/glut.h>//Draw a cube//save eight vertices of a cube into an arrayStatic Const floatvertex_list[][3] = { -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f,0.5f, -0.5f, 0.5f,0.5f, -0.5f, -0.5f, -0.5f,0.5f, 0.5f, -0.5f,0.5f, -0.5f,0.5f,0.5f, 0.5f,0.5f,0.5f,};//Save the ordinal of the vertex you want to use in an arrayStatic ConstGlint index_list[][2] = { {0,1}, {2,3}, {4,5}, {6,7}, {0,2}, {1,3}, {4,6}, {5,7}, {0,4}, {1,5}, {7,3}, {2,6}}; //Draw a cubevoidDrawcube (void){ inti,j; Glbegin (Gl_lines); for(i=0; i< A; ++i)//12 Line Segments { for(j=0; j<2; ++J)//2 vertices per line segment{GLVERTEX3FV (vertex_list[index_list[i][j]); }} glend ();}Static floatRotate =0;Static intTimes =0;voidRenderscene (void) {glclear (Gl_color_buffer_bit| Gl_depth_buffer_bit);//Clean up color buffers and depth buffersGlmatrixmode (gl_modelview);//operations on the Model viewGlloadidentity ();//resets the currently specified matrix to the unit matrixGlpushmatrix ();//Press Stack//Gltranslatef ( -0.2, 0, 0);//panning//Glscalef (1, 1, 1); //Zoom Times++; if(Times > -) { times=0; } if(Times% -==0)//[0, +]{Rotate+=0.5;//[0, a)} glrotatef (rotate,0,1,0);//RotateGlrotatef (rotate,1,0,0); //Dynamic Color Transform-- red, green, red if(Rotate = =0) glcolor3f (1,0,0); if(Rotate = = -) glcolor3f (0,1,0); if(Rotate = = the) glcolor3f (0,0,1); if(Rotate = = the) glcolor3f (1,1,0); if(Rotate = = the) Rotate=0; Drawcube (); //Draw a cubeGlpopmatrix ();//out of the stackglutswapbuffers ();} intMainintargcChar*argv[]) {Glutinit (&ARGC, argv);//Initialize GLUTGlutinitdisplaymode (glut_depth | glut_double |Glut_rgba); Glutinitwindowposition ( -, -);//display the relative position of the window on the screenGlutinitwindowsize ( -, -);//set the display window sizeGlutcreatewindow (argv[0]);//Create window with captionGlutdisplayfunc (renderscene);//functions for registering the displayGlutidlefunc (Renderscene);//Registration of idle functionsGlutmainloop ();//GLUT State Machine return 0; }
opengl+vs2010 routines-Rotating color cube (three-dimensional)