OpenGL path (7) add rich colors to cubes

Source: Internet
Author: User

Place different colors on the six sides of the cube. To achieve the mixed color effect, you can add a color before each line by referring to the Code in front of the cube.

The Code is as follows:

# Include <Gl/glut. h> # pragma comment (Lib, "opengl32.lib") # pragma comment (Lib, "glu32.lib") # pragma comment (Lib, "glu32.lib") # pragma comment (Lib, "glut. lib ") void box (float X, float y, float Z) // rectangular {glpushmatrix (); glscalef (x, y, z); glbegin (gl_quads ); glcolor3f (1, 0, 0); gltexcoord2f (0.0f, 0.0f); glvertex3f (-1.0f,-1.0f, 1.0f); // front glcolor3f (0, 1, 0 ); gltexcoord2f (1.0f, 0.0f); glvertex3f (1.0f,-1.0f, 1.0f ); Glcolor3f (0, 0, 1); gltexcoord2f (1.0f, 1.0f); glvertex3f (1.0f, 1.0f, 1.0f); glcolor3f (0, 1, 1); gltexcoord2f (0.0f, 1.0f); glvertex3f (-1.0f, 1.0f, 1.0f); glcolor3f (1, 0, 0); gltexcoord2f (1.0f, 0.0f); glvertex3f (-1.0f,-1.0f, -1.0f); // post-gltexcoord2f (1.0f, 1.0f); glvertex3f (-1.0f, 1.0f,-1.0f); gltexcoord2f (0.0f, 1.0f); glvertex3f (1.0f, 1.0f, -1.0f); gltexcoord2f (0.0f, 0.0f); glvertex3f (1.0f,-1.0f,-1.0f ); Glcolor3f (0, 1, 0); gltexcoord2f (0.0f, 1.0f); glvertex3f (-1.0f, 1.0f,-1.0f); // gltexcoord2f (0.0f, 0.0f ); glvertex3f (-1.0f, 1.0f, 1.0f); gltexcoord2f (1.0f, 0.0f); glvertex3f (1.0f, 1.0f, 1.0f); then (1.0f, 1.0f); glvertex3f (1.0f, 1.0f, -1.0f); glcolor3f (0, 0, 1); gltexcoord2f (1.0f, 1.0f); glvertex3f (-1.0f,-1.0f,-1.0f); // gltexcoord2f (0.0f, 1.0f); glvertex3f (1.0f,-1.0f,-1.0f); gltexcoord2f (0.0f, 0.0f); glvertex3f (1.0f,-1.0f, 1.0f); gltexcoord2f (1.0f, 0.0f); glvertex3f (-1.0f,-1.0f, 1.0f); glcolor3f (1, 1, 0); gltexcoord2f (1.0f, 0.0f); glvertex3f (1.0f,-1.0f,-1.0f); // left gltexcoord2f (1.0f, 1.0f); glvertex3f (1.0f, 1.0f, -1.0f); gltexcoord2f (0.0f, 1.0f); glvertex3f (1.0f, 1.0f, 1.0f); gltexcoord2f (0.0f, 0.0f); glvertex3f (1.0f,-1.0f, 1.0f ); glcolor3f (0, 1, 1); gltexcoord2f (0.0f, 0.0f); glvertex 3f (-1.0f,-1.0f,-1.0f); // right gltexcoord2f (1.0f, 0.0f); glvertex3f (-1.0f,-1.0f, 1.0f); gltexcoord2f (1.0f, 1.0f ); glvertex3f (-1.0f, 1.0f, 1.0f); gltexcoord2f (0.0f, 1.0f); glvertex3f (-1.0f, 1.0f,-1.0f); glend (); glpopmatrix ();} void renderscene (void) {static float I = 0; I + = 0.01f; if (I> 360) I = 0; glclear (gl_color_buffer_bit | gl_depth_buffer_bit); glloadidentity (); gltranslatef (0, 0,-1.5); glrotatef (I, 1, 1, 1); box (0.3f, 0.3f, 0.3f); fig ();} void changesize (int w, int h) {If (H = 0) H = 1; float ratio = 1.0 * W/H; glmatrixmode (gl_projection); // unitized projection matrix. Glloadidentity (); glviewport (0, 0, W, H); // you can specify the window size as gluperspective (45, ratio, 1, 1000 ); // set the correct projection matrix glmatrixmode (gl_modelview); // set the Model View matrix glloadidentity (); glulookat (0.0, 0.0, 5.0, 0.0, 0.0,-1.0, 0.0f, 1.0f, 0.0f); // set the observation point} int main (INT argc, char * argv []) {gluinit (& argc, argv ); fig ); // call the function glureshapefunc (changesize) when the specified program is idle; // specify the callback function glable (gl_depth_test) when the window shape changes; glumainloop (); Return 0 ;}

Effect


OpenGL path (7) add rich colors to cubes

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.