opengl+vs2010 routines-Rotating color cube (three-dimensional)

Source: Internet
Author: User

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 (&AMP;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)

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.