Android OpenGL ES concise development tutorial 05

Source: Internet
Author: User

The square shown in the previous example is white, which is not very attractive. This article describes how to add colors to mesh. OpenGL ES uses color as the well-known rgba mode (red, green, blue, transparency ).

The definition of color usually uses the hex format 0xff00ff or the decimal format (255, 0, 255), but uses 0 in OpenGL... A floating point between 1. 0 is 0, 1 is equivalent to 255 (0xff ).

The simplest coloring method is vertxt coloring. You can use a monochrome color, a color gradient, or a material (similar to a variety of brush types in a two-dimensional image ).

Flat coloring (monochrome)

It notifies OpenGL to use a single color for rendering. OpenGL will always use the specified color for rendering until you specify other colors.

The method for specifying the color is

Public abstract void glcolor4f (float
Red, float green, float blue, float alpha ).

The default Red, green, and Blue values are 1, indicating white. This is why the square shown above is white.

We create a new class named flatcoloredsquare. As a subclass of sequare, we redefine its draw as follows:

public void draw(GL10 gl) { gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f); super.draw(gl);}

Change the Square type of openglrenderer to flatcoloredsquare.

private FlatColoredSquare square=new FlatColoredSquare();

Compile and run. The color of the square turns blue:

Smooth
Coloring (smooth color transition)

When a color is defined for each vertex, OpenGL automatically generates an intermediate transition color (gradient) between different vertex colors ).

Add an smoothcoloredsquare class to the project and define a color value for each vertex as the square subclass.

// 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 color sequence is consistent with that of the vertex. To improve the performance, we also put the color array in the buffer like the vertex coordinates:

// float has 4 bytes, colors (RGBA) * 4 bytesByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);cbb.order(ByteOrder.nativeOrder());colorBuffer = cbb.asFloatBuffer();colorBuffer.put(colors);colorBuffer.position(0);

Finally, modify the draw method as follows:

public void draw(GL10 gl) {gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);// Enable the color array buffer to be//used during rendering.gl.glEnableClientState(GL10.GL_COLOR_ARRAY);// Point out the where the color buffer is.gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);super.draw(gl);// Disable the color buffer.gl.glDisableClientState(GL10.GL_COLOR_ARRAY);}

Change the Square type in openglrenderer to smoothcoloredsquare. The compilation result is as follows:

Download local sample code

Related Article

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.