The following are all converted from the android game programming entry-level classic. For more information, see the source.
Use glcolor4f () to set a global default color for all vertices. But sometimes more fine-grained control is needed (for example, you need to set the color for each vertex ).
OpenGL ES provides this function. All you need is to add the rgba floating point component to each vertex and tell OpenGL ES where to find the color of each vertex.
int VERTEX_SIZE = (2 + 4) * 4;ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * VERTEX_SIZE);byteBuffer.order(ByteOrder.nativeOrder());vertices = byteBuffer.asFloatBuffer();vertices.put(new float[]{ 0.0f, 0.0f, 1, 0, 0, 1,319.0f, 0.0f, 0, 1, 0, 1,160.0f, 479.0f, 0, 0, 1, 1});
Each vertex requires two coordinates and four (rgba) color components. Therefore, a total of six floating point numbers are required. Each floating point occupies 4 bytes, that is, a vertex needs 24 bytes. When you call the bytebuffer. allocatedirect () method, you only need to multiply the number of vertices by vertex_size to obtain the required bytebuffer space.
If you need to render it, you must tell OpenGL ES that these vertices not only have location information, but also have color attributes.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
Now OpenGL ES knows that it can get the position and color information of each vertex, and also needs to tell it where to get the information:
vertices.position(0);gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);vertices.position(2);gl.glColorPointer(4, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
First, set the position of floatbuffer. Floatbuffer stores These vertices starting from 0. This position points to the X coordinate of the first vertex in the buffer zone. Next, call the glvertexpointer () method. The only difference from the previous instance is that the vertex size (in bytes) needs to be specified here ). OpenGL ES reads the vertex position from the starting position of the specified buffer. Add vertex_size to the address of the first vertex to obtain the address of the second vertex, and so on.
Next, set the r color component of the first vertex in the buffer and call the glcolorpointer () method. This method tells OpenGL ES where to find the vertex color. The first parameter is the number of components of each color. The number of components is always 4. The second parameter specifies the type of each component. Use gl10.gl _ float to declare that each color component is a float type and the value range is 0 ~ 1. The third parameter is the step size between vertex colors. The last parameter is the vertex buffer.
As long as vertices. Position (2) is called before glcolorpointer () is called, OpenGL ES will know that the color of the first vertex starts from the third floating point in the buffer. If the position is not set to 2, OpenGL ES reads the color value from position 0.
package org.example.androidgames.glbasics;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.nio.FloatBuffer;import javax.microedition.khronos.opengles.GL10;import org.example.androidgames.framework.Game;import org.example.androidgames.framework.Screen;import org.example.androidgames.framework.impl.GLGame;import org.example.androidgames.framework.impl.GLGraphics;public class ColoredTriangleTest extends GLGame{@Overridepublic Screen getStartScreen() {// TODO Auto-generated method stubreturn new ColoredTriangleScreen(this);}class ColoredTriangleScreen extends Screen {final int VERTEX_SIZE = (2 + 4) * 4;GLGraphics glGraphics;FloatBuffer vertices;public ColoredTriangleScreen(Game game) {super(game);glGraphics = ((GLGame)game).getGLGraphics();ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * VERTEX_SIZE);byteBuffer.order(ByteOrder.nativeOrder());vertices = byteBuffer.asFloatBuffer();vertices.put(new float[]{ 0.0f, 0.0f, 1, 0, 0, 1,319.0f, 0.0f, 0, 1, 0, 1,160.0f, 479.0f, 0, 0, 1, 1});}@Overridepublic void update(float deltaTime) {// TODO Auto-generated method stub}@Overridepublic void present(float deltaTime) {// TODO Auto-generated method stubGL10 gl = glGraphics.getGL();gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());gl.glClear(GL10.GL_COLOR_BUFFER_BIT);gl.glMatrixMode(GL10.GL_PROJECTION);gl.glLoadIdentity();gl.glOrthof(0, 320, 0, 480, 1, -1);gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glEnableClientState(GL10.GL_COLOR_ARRAY);vertices.position(0);gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);vertices.position(2);gl.glColorPointer(4, GL10.GL_FLOAT, VERTEX_SIZE, vertices);gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);}@Overridepublic void pause() {// TODO Auto-generated method stub}@Overridepublic void resume() {// TODO Auto-generated method stub}@Overridepublic void dispose() {// TODO Auto-generated method stub}}}
The running effect is as follows: