OpenGL ES tutorial IV: coloring)

Source: Internet
Author: User

 

OpenGL ES tutorial for Android-Part IV-adding colors

January 14th, 2010 by Per-Erik Bergman-

Android,
Embedded

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.

This tutorial is about coloring.

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 shocould be. If you like to read more about colors go:
RGB color model-Wikipedia, the free encyclopedia

If the 3D model is not colored, it does not seem interesting at all. So we add colors to them. Generally, you do not need to explain the color. OpenGL ES uses the color model RGBA (red, green, blue, transparency ). The first three do not need to be explained. The 4th Alpha represents transparency. (0, completely transparent, 1, completely opaque)

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 ).

You may be familiar with hexadecimal or 10 hexadecimal color notation. But in OpenGL, 0… is used... 1, 0 ing is #255, 1 ing is # 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.

The old method is easy and difficult. The simplest method is to color the vertex. I will introduce the two methods below. Fill mode sets the same color for each vertex; smooth gradient mode sets a gradient for each vertex. Texture ing is also a coloring method, but not within the vertex coloring range. For more information about texture ing, see the following tutorial.

Flat coloring

Flat 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.

Filling and coloring is too simple. You only need to tell OpenGL ES the colors to use for rendering. Keep in mind that the set color remains valid until you change the color. For example, if you have two squares and you have changed the color before drawing the second square, the color of the first frame and the two squares is different, but the color of the subsequent frames is the same.

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

Use the following function to set the color:

Public abstract void
GlColor4f (float red, float green, float blue, float alpha)

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.

The default value of the function parameter is 1, which is why the square above is white.

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

Create a new class named FlatColoredSquare to distinguish it from the Square class. Add the following line to the painting function:

Gl. glColor4f (0.5f, 0.5f, 1.0f, 1.0f); // 0x8080FFFF

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.

I personally suggest writing hexadecimal color values in the comments, which is not only easy to read, but also more intuitive in the code review.

It shoshould now look like this:

Public void draw (GL10 gl ){

Gl. glColor4f (0.5f, 0.5f, 1.0f, 1.0f );

...

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

Public class OpenGLRenderer implements Renderer {

Private FlatColoredSquare flatSquare; // CHANGED

 

Public OpenGLRenderer (){

// Initialize our square.

FlatSquare = new FlatColoredSquare (); // CHANGED

}

 

Public void onDrawFrame (GL10 gl ){

...

FlatSquare. draw (gl); // Don't forget to change this one.

...

}

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-.

Note: After the color is set, you do not need to set the color again, even for subsequent frames.

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.

Public void onDrawFrame (GL10 gl ){

Gl. glLoadIdentity ();

// Translates 7 units into the screen and 1.5 units up.

Gl. glTranslatef (0, 1.5f,-7 );

// Draw our flat square.

FlatSquare. draw (gl );

}

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.

Note: Do not tell OpenGL ES to enable or disable this function for filling and coloring. OpenGL ES uses the filling mode by default.

Smooth coloring

Smooth 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.

Smooth gradient coloring uses its own color for each vertex. OpenGL ES inserts a color between vertices for a smooth (gradient) color effect. Like fill coloring, the set color can be used for a long time.

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

Create a new class named SmoothColoredSquare

Define the colors you like your vertices to have.

Define vertex color

Public class SmoothColoredSquare {

...

// The colors mapped to the vertices.

Float [] colors = {

1f, 0f, 0f, 1f, // vertex 0 red

0f, 1f, 0f, 1f, // vertex 1 green

0f, 0f, 1f, 1f, // vertex 2 blue

1f, 0f, 1f, 1f, // vertex 3 magenta

};

...

The order of defining the colors are important since they map against
Vertices
So in this example abve 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.

The order in which colors are defined is very important. It corresponds to vertices one by one. In this example, red (1f, 0f, 0f, 1f) corresponds to vertex 0 (-1.0f, 1.0f, 0.0f ).

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

Like the vertex and vertex sequence arrays, place them in the byte buffer.

Public SmoothColoredSquare (){

...

 

// Float has 4 bytes, colors (RGBA) * 4 bytes

ByteBuffer cbb = ByteBuffer. allocateDirect (colors. length * 4 );

Cbb. order (ByteOrder. nativeOrder ());

ColorBuffer = cbb. asFloatBuffer ();

ColorBuffer. put (colors );

ColorBuffer. position (0 );

}

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

Don't forget to add colorBuffer Variables

// Our color buffer.

Private FloatBuffer colorBuffer;

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

We need to tell OpenGL to enable color byte buffering.

Public void draw (GL10 gl ){

...

Gl. glVertexPointer (3, GL10.GL _ FLOAT, 0, vertexBuffer );

 

// Enable the color array buffer to be used during rendering.

Gl. glableclientstate (GL10.GL _ COLOR_ARRAY); // new line added.

// Point out the where the color buffer is.

Gl. glColorPointer (4, GL10.GL _ FLOAT, 0, colorBuffer); // new line added.

 

Gl. glDrawElements (GL10.GL _ TRIANGLES, indices. length,

GL10.GL _ UNSIGNED_SHORT, indexBuffer );

...

// Disable the color buffer.

Gl. glDisableClientState (GL10.GL _ COLOR_ARRAY );

...

}

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.

Note: Do not forget to disable color buffering at last. If you forget it, the filled colored blocks in front will also become gradient coloring.

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

Public class OpenGLRenderer implements Renderer {

Private FlatColoredSquare flatSquare;

Private SmoothColoredSquare smoothSquare; // new line added.

 

Public OpenGLRenderer (){

// Initialize our squares.

FlatSquare = new FlatColoredSquare ();

SmoothSquare = new SmoothColoredSquare (); // new line added.

}

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

Public void onDrawFrame (GL10 gl ){

...

// Translate to end up under the flat square.

Gl. glTranslatef (0,-3f, 0 );

// Draw our smooth square.

SmoothSquare. draw (gl );

}

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

References

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:
Code.google.com

Previous Tutorial:
OpenGL ES tutorial for Android-Part III-Transformations
Next Tutorial:
OpenGL ES tutorial for Android-Part V-more on meshes

Per-Erik Bergman
Consultant at jayway

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.