Add color:
As an extension of Lesson 2, I will ask you how to use colors. You will understand the two coloring modes. In the middle, the triangle uses smooth coloring and the Quadrilateral uses plane coloring.
In the previous lesson, I taught you how to draw triangles and quadrants. In this lesson, I will teach you how to add two different types of coloring methods for triangles and quadrilateral. Use flat coloring to apply a fixed color to the Quadrilateral. Use smooth coloring to mix different colors of the Three triangle vertices to create a beautiful color mix.
Continue to modify the drawglscene routine in the previous lesson. The entire routine is rewritten below. If you plan to modify the code of the previous lesson, you only need to overwrite the original drawglscene () with the following code.
Int drawglscene (glvoid) // This process includes all the drawing code {glclear (gl_color_buffer_bit | gl_depth_buffer_bit); // clear the screen and the depth cache glloadidentity (); // reset the model observation matrix gltranslatef (-1.5f, 0.0f,-6.0f); // move 1.5 units left and 6.0 glbegin (gl_triangles) into the screen; // draw a triangle
If you still remember the content of the last lesson, this Code draws a triangle in the left half of the screen. The next line of code is the first time we use the glcolor3f (R, G, B) command ). The three parameters in brackets are red, green, and blue. The value range is from 0, 0f to 1.0f. Similar to the previous command to clear the screen background.
We set the color to red (pure red, no green, no blue ). The following code sets the first vertex of the triangle (the top vertex of the triangle) and draws it using the current color (red. From now on, the colors of all drawn objects are red until we change the red color to another color.
Glcolor3f (1.0f, 0.0f, 0.0f); // set the current color to Red glvertex3f (0.0f, 1.0f, 0.0f); // top Vertex
The first red vertex has been set. Next we will set the second green vertex. The lower left vertex of the triangle is set to green.
Glcolor3f (0.0f, 1.0f, 0.0f); // set the current color to green glvertex3f (-1.0f,-1.0f, 0.0f); // lower left
Set the third vertex, that is, the last vertex. Set the color to blue before you start painting. This will be the right bottom vertex of the triangle. When glend () appears, the triangle is filled. However, because each vertex has a different color, it seems that the color is sprayed out of each corner, and it is in the center of the triangle. The three colors are mixed with each other. This is smooth coloring.
Glcolor3f (0.0f, 0.0f, 1.0f); // set the current color to Blue glvertex3f (1.0f,-1.0f, 0.0f); // glend () in the lower right corner (); // After the triangle is drawn, gltranslatef (3.0f, 0.0f, 0.0f); // shifts the value 3 to the right.
Now we draw a monotonic colored-blue square. The most important thing is to remember that all the items drawn after setting the current color are the current color. Color will be used for each project you create in the future. Glcolor3f can be used to adjust texture tones even when texture textures are fully used. Wait... let's talk about it later.
All we have to do is set the color one-time to the color we want to use (in this example, we use blue), and then draw the scene. Every vertex is blue, because we didn't tell OpenGL to change the color of the vertex. The final result is a square in blue. Again, the clockwise square means we see the back of the Quadrilateral.
Glcolor3f (0.5f, 0.5f, 1.0f); // set the current color to Blue glbegin (gl_quads) at a time; // draw a square glvertex3f (-1.0f, 1.0f, 0.0f ); // upper left glvertex3f (1.0f, 1.0f, 0.0f); // upper right glvertex3f (1.0f,-1.0f, 0.0f); // lower left glvertex3f (-1.0f,-1.0f, 0.0f ); // glend () in the lower right corner; // return true when the square painting ends; // continue to run}
Finally, change the title content in window mode.
// Recreate the OpenGL window if (! Createglwindow ("nehe's color instance", 640,480, 16, fullscreen ))
In this lesson, I try to explain in detail how to add monotonous and smooth coloring effects to your OpenGL polygon. Change the red, green, and blue component values in the code to see what the final result of Y is. If you have any comments or suggestions, please email me. If you think there is something wrong or can be improved, please let me know. I want to do the best OpenGL tutorial and be interested in your feedback.
Lesson 03rd add color to OpenGL