Nehe OpenGL Lesson 3: Color Rendering

Source: Internet
Author: User

Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://yarin.blog.51cto.com/1130898/380273

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 left graph, 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 depth Cache
Glloadidentity (); // resets the model observation matrix.
Gltranslatef (-1.5f, 0.0f,-6.0f); // move 1.5 units left and 6.0 to the screen

Glbegin (gl_triangles); // 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); // bottom 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); // bottom right
Glend (); // draw the triangle

Gltranslatef (3.0f, 0.0f, 0.0f); // shifts right to 3 units

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 at a time.
Glbegin (gl_quads); // draw a square
Glvertex3f (-1.0f, 1.0f, 0.0f); // top left
Glvertex3f (1.0f, 1.0f, 0.0f); // upper right
Glvertex3f (1.0f,-1.0f, 0.0f); // bottom left
Glvertex3f (-1.0f,-1.0f, 0.0f); // bottom right
Glend (); // draw the square
Return true; // continue running
}

Finally, change the title content in window mode.

// Recreate the OpenGL window
If (! Createglwindow ("nehe's color instance", 640,480, 16, fullscreen ))

Download the source code of the original text and its versions:

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

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.