OpenGL ES Tutorial for Android – Part IV – Adding colors

I have started a new updated serie of tutorials on OpenGL ES 2.0 for android. Check them out at: OpenGL ES 2.0

Last tutorial was about transformations. This tutorial will be a short one. I’m going to talk about adding color to your mesh. I will continue with the source code from tutorial II.

Adding color

3D models with no colors are pretty boring so let’s add some color to it. In general colors need no explanation. OpenGL ES uses a color model called RGBA ( Red, Green, Blue and Alpha ). The first three are self explained. The fourth is transparency, how solid the color should be. If you like to read more about colors go to: RGB color model – Wikipedia, the free encyclopedia

You might be familiar with defining colors with hex (#FF00FF) or with decimals (255, 0, 255) we will use 0..1 where 0 map to 0 (#00) and 1 map against 255 (#FF).

The easiest way of coloring meshes is called vertex coloring and I am going to show you two different ways of doing that. Flat coloring that gives one solid color and smooth coloring that will blend colors specified for each vertex. Texturing is also a way of giving your mesh colors but it is not vertex coloring so I will show you how to do that in a later tutorial.

Flat coloring

Flat ColoringFlat coloring is really easy just tell OpenGL ES what color to use when it is going to render. One thing to remember is that when you set the color OpenGL ES uses this color until you change the color. This means that if you have two different squares and you tell OpenGL ES to change the color right before the second square the first frame the two squares will have different color but the next rendered frame both squares will have the same color.

To tell OpenGL ES what color to work with you use this command:

The default values are: red = 1, green = 1, blue = 1 and alpha = 1. Those values are white, and that’s why all the squares we previous made has a white color.

Create a new class called FlatColoredSquare it should be identical to the Square class. Then in the FlatColoredSquare function draw, add this line:

I usually add a comment like the one above ( // 0x8080FFFF ) because I am used to read that. It makes it easier for me when reviewing the code.

It should now look like this:

Then change in the renderer so it uses the FlatColoredSquare instead of the Square.

Remember that anything rendered after you set a color uses the same color and that this spans over frames and will not be reset in-between.

If you compile and run the application you will see one big flat colored blue square.

Just to give place to the smooth colored square coming up we move the flat square up.

Notice that with flat coloring you don’t need to tell OpenGL ES to turn it on or off. OpenGL ES uses flat coloring as a default way of coloring the meshes.

Smooth coloring

Smooth ColoringSmooth coloring is gained when you give each vertex its own color. OpenGL ES will interpolate the colors between the vertices and you will gain a smooth coloring effect. Just as with the flat coloring you tell OpenGL ES what to work with and it will be used as long as you don’t say anything else.

Create a new class called SmoothColoredSquare it should be identical to the Square class just as you did with the FlatColoredSquare. Modify the new class with this:

Define the colors you like your vertices to have.

The order of defining the colors are important since they map against the vertices so in this example above the first color (1f, 0f, 0f, 1f ) map against the top left vertex ( -1.0f, 1.0f, 0.0f ) the green against the bottom left vertex and the rest you can figure out. Hint: Look at the image above.

And put them in a buffer just as we did with the vertices and indices.

Don’t forget to add colorBuffer as a variable to the class as well.

We also need to enable the color buffer and tell openGL where it is.

Don’t forget to disable the use of the color array. If you don’t disable the color array both squares will be smooth colored. Try it.

Let’s use this new smooth square as well. Start by adding it to your renderer.

We need to move the square down a bit so they don’t collide.

Now if you compile and run the application you will see two squares, one solid blue and one smooth with different colors.


The info used in this tutorial is collected from:
Android Developers
OpenGL ES 1.1 Reference Pages

You can download the source for this tutorial here: Tutorial_Part_IV
You can also checkout the code from:

Previous tutorial: OpenGL ES Tutorial for Android – Part III – Transformations
Next tutorial: OpenGL ES Tutorial for Android – Part V – More on Meshes

This Post Has 26 Comments

  1. Thank you verry much. This is the best 3d tutorials I’ve found.

    There’s something I would like to know. Hope you will help me.

    Is there any way to capture touch events on 3D objects drawn in a space?

    I just want to select a object or know where I have clicked.

    Is it possible?


  2. Jen: Glad you like my tutorial. About face picking or mesh picking it’s a bit much to share as a comment. But there is two basic ways of doing this;
    1. Sending a ray into the screen and calculate which mesh you clicked on,
    2. Rendering all meshes with a unique color and when you pick just check for the color. I recommend for beginners since it’s a bit easier.

  3. Excellent tutorials, they have explained a lot of things for me, looking forward to the next one!

  4. Mr Bergman , after seeing your tutorials I want to say , this is the best I have ever seen , even better than NeHe’s. If some day you’ve written a book , I will certainly buy one .

  5. All tutorials are really awesome.By now you must have started preparing the next tutorial, can you please tell which topics will be covered in next tutorial.

  6. Yes, I have started on the next tutorial. It will be about more meshes, but 3D meshes so that we get some real 3D objects on the screen.

  7. Per-Erik:

    Very nice tutorials; I have followed all of them up to this point. I just wanted to say thanks for the nice documentation on the fundamentals of 3D programming using OpenGL. The illustrations are a very nice touch to understanding the whole idea of a matrix.

    One thing I found helpful when completing this tutorial was the use of glPushMatrix() and glPopMatrix() to make both squares appear:

    public void onDrawFrame(GL10 gl) {
    // Clear the screen and depth buffer.
    // Replace current matrix with the identity matrix.

    // Save current matrix.
    // Translate -10 units into screen, 1.5 units up.
    gl.glTranslatef(0, 1.5f, depth);
    // Draw our flat square.
    // ...then restore previous matrix.

    // Save current matrix.
    // Translate smoothSquare to end up under flatSquare.
    gl.glTranslatef(0, -1.5f, depth);
    // Draw our smooth square.
    // ...then restore previous matrix.


    Again, many thanks! I will continue to read through the rest of your posts and look forward to many more! : )


  8. Great tutorial pages !

    Could you add another tutorial : how to display bitmaps ? as texture, or (for me needs :-) as 2D bitmap displayed only once, without stretching. For instance to display something in 2D for an AR application.


  9. Great Tutorial,
    When can we expect the 2d texturing tutorial?

  10. I’m enjoying and learning your tutorials, Per-Erik.

    Reading this article regarding colors, I got an idea that I’d like to share with all of you, since you might find it useful. I added a new functionality to my site, so now it is possible to get float RGB colors from the color picker.

    I really don’t want to do SPAM with this. But if we need to use float colors in OpenGL ES, my conversion tool will be useful for you.

  11. Great tutorial. Have you any plans on one for 2d collision detection with opengl es or do you have any advice on how I would try this?

  12. Thanks for great tutorials, i have a question , how can i color different faces of a cube with different colors ?

  13. Now I’ve got colors in tha hood! CP Gangstaz represening the red ones, aight. No flue trespass here. Them crabs better stay away…

  14. Question is how do I use the previous sample where the 3 boxes are rotating around but make square A a Square(), square B a FlatColoredSquare() and square c a SmoothColoredSquare()? I tried just initiating the classes and then replace square.draw(gl) with flatSquare.draw(gl) and smoothSquare.draw(gl)…only problem is all the squares end up being rainbow colored.

  15. A Great Tutorial!
    Thank you so much!!!!

  16. You must disable color array to apply colors to all vertex with “glColor4f”
    like this:
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

  17. Excellent tutorial, read line by line and understood like a teacher teaching the content in person. Hats off!

  18. Hats Off Sir …. Very very easy to understand specially for Beginners.. I Really like your all Six Tutorial on OpenGL .

  19. The tutorial is great but how can you convert rgb/hex to the floats?

Leave a Reply

Close Menu