continue
In the previous section I taught you how to draw triangles and quadrilaterals. This section will teach you how to add 2 different types of shading to triangles and quadrilaterals. Use flat coloring to paint the quadrilateral with a fixed color. Use Smooth coloring to blend the different colors of the three vertices of the triangle together to create a nice blend of colors.
Continue to modify glDraw in the previous section.
PRocedure glDraw();
Begin
glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT); // Clear screen and depth buffer
glLoadIdentity(); //Reset the current model observation matrix
glTranslatef(-1.5, 0.0, -6.0); // Move left 1.5 units and into screen 6.0
glBegin(GL_TRIANGLES); // Draw triangles
//glColor3f(r,g,b). The three parameters in parentheses are the red, green, and blue color components in order.
//The value range can be from 0,0 to 1.0. Similar to the clear screen background command mentioned before.
//We set the color to red (pure red, no green, no blue).
//The next line of code sets the first vertex of the triangle (the upper vertex of the triangle),
//And use the current color (red) to draw. From now on all drawn objects will be colored red,
//Until we change red to something else.
glColor3f(1.0, 0.0, 0.0); //Set the current color to red
glVertex3f(0.0, 1.0, 0.0); // Upper vertex
//The first red vertex has been set.
//Next we set the second green vertex. The lower left vertex of the triangle is colored green.
glColor3f(0.0, 1.0, 0.0); //Set the current color to green
glVertex3f(-1.0, -1.0, 0.0); // Lower left
//The lower right vertex of the triangle. Set color to blue
//After glEnd() appears, the triangle will be filled.
//But because each vertex has a different color, it looks like the color is squirting out from each corner,
//And meet exactly in the center of the triangle, the three colors mix with each other. This is smooth shading.
glColor3f(0.0, 0.0, 1.0); //Set the current color to blue
glVertex3f(1.0, -1.0, 0.0); // Lower right
glEnd(); // End of triangle drawing
glTranslatef(3.0, 0.0, 0.0); // Shift right by 3 units
//Now we draw a square colored monotonically - purple.
//The most important thing to remember is that everything drawn after setting the current color is the current color.
//Every project you create from now on will use color.
//Even when texture mapping is fully used,
//glColor3f can still be used to adjust the color tone of the texture.
//Wait..., let’s talk about it later.
//(Haha, the original book is blue, but I like purple)
glBegin(GL_QUADS); // Draw a square
glColor3f(0.6, 0.2, 2.0); //Set the current color to purple
glVertex3f(-1.0, 1.0, 0.0); // Upper left
glVertex3f(1.0, 1.0, 0.0); // Upper right
glVertex3f(1.0, -1.0, 0.0); // Lower left
glVertex3f(-1.0, -1.0, 0.0); // Lower right
glEnd(); // End of square drawing
End;