Adding colors with OpenGL ES in Android

In the post Drawing a triangle with OpenGL ES in Android I explained how to draw a triangle without setting the colors, indeed, the triangle is gray.
In this post I add a few lines of code that allow you to change the color of the triangle starting from the code in Drawing a triangle with OpenGL ES in Android.

You can edit the same project created in Drawing a triangle with OpenGL ES in Android or create a new one and follow its first 3 steps.

  1. edit the file eu/lucazanini/opengl/shape/ShapeRenderer.java

    the only difference with Drawing a triangle with OpenGL ES in Android is the lines 29 and 34
  2. edit the file eu/lucazanini/opengl/shape/Triangle.java

    in this class at the row 26 I add the variable colors that contains the information about colors to be assigned in correspondence of the 3 vertices;
    in the draw method at the row 43 I link the colors stored in the buffer mColorBuffer to the vertices of the triangle
  3. run the app
    colored triangle

References:
GLSurfaceView.Renderer
OpenGL ES 1.1 Reference Pages

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.