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