OpenGL Game Development Tutorials

OpenGL Book: A OpenGL Tutorial Reference Book
Easily understand OpenGL and learn how to create 3D worlds and games from scratch.

OpenGL Gems is the authorative guide on OpenGL programming published by Learning Curve / Education for software developers! This book in 3D C++ programming series clearly, and in easy to follow language, explains fundamental principles used behind programming 3D games using the OpenGL cross-platform framework.

Preorder Now in PDF format for Kindle and get a discount.
OpenGL Gems is coming out on April 15th, 2017.
The paperback version will be available via this Amazon page, please bookmark it!

OpenGL IDE Tutorial - Microsoft Visual Studio C++ 6.0
Compiler Setup for OpenGL

In my previous OpenGL tutorial we learned how to draw OpenGL primitives with glVertex. This tutorial introduces you to OpenGL color using glColor command. As with the previous tutorial I will only use a single triangle to demonstrate the use of color.

The same rules still apply to all other types of 3D primitives. It is possible to draw plain non-textured triangles of any color as well as triangles with a different color specified for each vertex. But lets begin with the function we will use to specify color which is glColor.

Since I, like many people, prefer using floating-point variables when dealing with OpenGL parameters we will use the function glColor3f which takes the R, G and B components of the color in the following manner.

glColor3f(R, G, B);

Each color component can range from 0.0f to 1.0f where 0.0f is no color and 1.0f is full color. For example to specify pure red you would do the following:

glColor3f(1.0f, 0.0f, 0.0f);

How can this function be used to draw colored primitives? This requires a little explanation on how OpenGL deals with colors. OpenGL doesn't have a specific function to assign a color to a specific vertex for instance, or a triangle at the time of the function call.

However, OpenGL has a CURRENT color which is always stored somewhere and all you can do is modify that current color. To explain this in more detail in contrast to something else, imagine a different, non-OpenGL program (most people including yourself might have worked on before), which requires drawing pixels of different colors on the screen.

You specify the address someplace in the video memory and place a value of your color into that address, instantly the pixel lights up on the screen.

OpenGL, in contrast, doesn't work that way. It always stores a "current" color value at some place else that you don't need to worry about. What you do need to worry about is actually changing that value. All vertices of any primitive are drawn using the current color.

Just remember that that value is always stored somewhere and you need to modify it every time you need a new color. Initially OpenGL sets that current color value to white (R = 1.0f, G = 1.0f, B = 1.0f). That's exactly the reason why without specifying the color in previous tutorial the triangles you drew appeared white.

Lets practice by drawing a red triangle. Remember that all of this should be typed inside the RenderFrame function just like in previous tutorial.

glColor3f(1.0f, 0.0f, 0.0f);


                glVertex3f(-1.0f, -0.5f, -4.0f);    // A
                glVertex3f( 1.0f, -0.5f, -4.0f);    // B
                glVertex3f( 0.0f,  0.5f, -4.0f);    // C


This will result in a red triangle rendered on the screen. Note how the current color is assigned to red, before drawing the triangle. It is possible to assign color outside the glBegin-glEnd block as well as inside. The following code assigns color to each vertex which results in a smoothly shaded triangle as illustrated below.


                glColor3f(1.0f, 0.0f, 0.0f);
                glVertex3f(-1.0f, -0.5f, -4.0f);    // A

                glColor3f(0.0f, 1.0f, 0.0f);
                glVertex3f( 1.0f, -0.5f, -4.0f);    // B

                glColor3f(0.0f, 0.0f, 1.0f);
                glVertex3f( 0.0f,  0.5f, -4.0f);    // C


And here is a graphical example of the outcome:

Drawing a triangle using glVertex3f and glColor3f, with red, blue and green colors applied to each of the 3 vertices. Smooth blending rendering is done automatically by OpenGL.

As you can see, you can change current color between calls to glVertex which in return assigns that color to the following vertex. This technique is called smooth-shading and can be somehwat considered the next step after flat-shading (when a triangle has only one color).

Smooth shading is only possible if the smooth shade model is enabled by OpenGL which is accomplished by a call to glShadeModel(GL_SMOOTH). Please take your time and find that call in my base code, it is located in the InitOpenGL function. This call is what makes it possible to draw smoothly shaded triangles.

Try changing GL_SMOOTH to GL_FLAT and recompile the same code; you will end up with a blue triangle. Why? Because when we use the flat shading model to draw a triangle, if colors are specified for each or some vertices of that triangle, OpenGL picks the LAST specified color for the last vertex (in our case it is the vertex C) to color the whole triangle. And since vertex C was assigned blue color, the whole triangle is colored blue.

That's how all primitives will work (with flat shading), the only EXCEPTION is when drawing polygons (e.g. using the GL_POLYGONS flag). When drawing polygons (as opposed to any other primitive, lines, quads, etc.), the FIRST vertex color will be used. You can as well write an e-mail to the OpenGL folks and ask them why!

Luckily, you don't have to worry about how light and shading works at this time because I will cover everything in one of the following tutorials. This tutorial was is here only to introduce you to how to draw colored or "shaded" polygons. Well, this rounds up our discussion of color.

It is really trivial to use after just a few minutes of practice. At this point you can draw colored primitives of any shape in your 3D world. But that's not enough; you need to light them, move them, rotate them... My next tutorial will cover movement in 3D space and only then I will cover the OpenGL light model.

It would be silly to write source code for this tutorial since it's extremely easy. Just follow the code explanation!

OpenGL Book: A OpenGL Tutorial Reference Book
OpenGL Book - A OpenGL Tutorial Reference Book

If tutorials on this site are not enough, or you simply like reading from a physical book or a digital device (Kindle, iPad, tablets, etc.) check out OpenGL Book. Written by the author of tutorials on this site.

This book is a OpenGL tutorial and a reference that guides the reader through the process of setting up and initializing OpenGL, drawing 3D primitives and creating 3D computer games.

Preorder Here
© 2017 Copyright OpenGL Tutorials (

All content and graphics on this website are the property of - please provide a back link when referencing on other sites.