Nehe OpenGL Tutorial Lesson Three: Color rendering

Source: Internet
Author: User

Go from "translation" Nehe OpenGL tutorial

Objective

Statement, this Nehe OpenGL tutorial series of articles by 51 blog Yarin translation (2010-08-19), this blog for reprint and a little collation and modification. Thank you for compiling Nehe's OpenGL pipeline tutorial, as well as yarn translation finishing.

Lesson Three: Color rendering

Add Color:

As an extension of lesson two, I will call you how to use color. You will understand the two shading modes, in the left image, the triangle is smoothed, and the quadrilateral is in plane shading.

In the last lesson I taught you how to draw triangles and quads. In this lesson I will teach you to add 2 different kinds of coloring methods to triangles and quads. Use flat coloring (monotone coloring) to apply a fixed color to the quadrilateral. Use smooth coloring (smooth shading) to blend the different colors of the three vertices of the triangle together to create a beautiful color blend.
Continue to be modified on the Drawglscene routine of the previous lesson. The entire routine is rewritten again below. If you plan to modify the code for the previous lesson, simply overwrite the original drawglscene () with the following code.

int Drawglscene (glvoid)//This procedure includes all drawing codes
{
Glclear (Gl_color_buffer_bit |  Gl_depth_buffer_bit); Clear screen and depth cache
Glloadidentity (); Resetting the Model observation matrix
Gltranslatef ( -1.5f,0.0f,-6.0f); Move left 1.5 units and move into screen 6.0

Glbegin (Gl_triangles); Draw triangles

If you remember 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 Command glcolor3f (r,g,b). The three parameters in parentheses are red, green, and blue three-color components in turn. The range of values can be from 0,0f to 1.0f. Similar to the previously spoken clear screen background command.
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 top vertex of the triangle) and draws it using the current color (red). From now on all the painted objects are colored red until we change the red color to something else.

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 is already set. Next we set a 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

Now set a third and last vertex. Set the color to blue before you start drawing. This will be the lower right vertex of the triangle. When Glend () appears, the triangle is filled. But because each vertex has a different color, it appears that the color is sprayed from each corner and is just converging in the center of the triangle, with three colors blending with each other. This is the smoothing coloring.

glcolor3f (0.0f,0.0f,1.0f); Set the current color to blue
glvertex3f (1.0f,-1.0f, 0.0f); Lower right
Glend (); End of Triangle drawing

Gltranslatef (3.0f,0.0f,0.0f); Move right 3 units

Now we draw a monotone coloring-blue square. The most important thing to remember is that all the things drawn after the current color are set are the current color. You will use color for each project you create later. Even with the full texture map, the glcolor3f can still be used to adjust the tone of the texture. Wait a minute.
All we have to do is set the color once to the color we want to use (in this case, blue) and then draw the scene. Each vertex is blue because we don't tell OpenGL to change the color of the vertex. The final result is ..... The whole Blue Square. Again, the square drawn clockwise means that what we see is the back of the quadrilateral.

glcolor3f (0.5f,0.5f,1.0f); Set the current color to blue at once
Glbegin (gl_quads); Draw a square
glvertex3f ( -1.0f, 1.0f, 0.0f); Upper left
glvertex3f (1.0f, 1.0f, 0.0f); Top Right
glvertex3f (1.0f,-1.0f, 0.0f); Lower left
glvertex3f ( -1.0f,-1.0f, 0.0f); Lower right
Glend (); Square Draw End
return TRUE; Continue running
}

Finally replace the title in the window mode

Rebuilding the OpenGL window
if (! Createglwindow ("Nehe ' s Color instance", 640,480,16,fullscreen))

Original source code and version of the download:

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=03

Nehe OpenGL Tutorial Lesson Three: Color rendering

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.