Android GL10 Draw drawSquare(GL10 gl, float r, float g, float b, float a)

Here you can find the source of drawSquare(GL10 gl, float r, float g, float b, float a)

Description

draw Square

Declaration

public static final void drawSquare(GL10 gl, float r, float g, float b,
            float a) 

Method Source Code


import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Main {
    public static final void drawSquare(GL10 gl, float r, float g, float b,
            float a) {

        /*w  w w .  ja  v a 2  s . co m*/
        drawSquare(gl, 0.0f, 0.0f, r, g, b, a);
        
    }

    public static final void drawSquare(GL10 gl, float x, float y, float r,
            float g, float b, float a) {

        
        float[] vertices = { -0.5f + x, -0.5f + y, 0.5f + x, -0.5f + y,
                -0.5f + x, 0.5f + y, 0.5f + x, 0.5f + y, };
        float[] colors = { r, g, b, a, r, g, b, a, r, g, b, a, r, g, b, a, };

        
        FloatBuffer squareVertices = makeFloatBuffer(vertices);
        FloatBuffer squareColors = makeFloatBuffer(colors);

        
        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, squareVertices);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, squareColors);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
    }

    public static final FloatBuffer makeFloatBuffer(float[] arr) {

        ByteBuffer bb = ByteBuffer.allocateDirect(arr.length * 4);
        bb.order(ByteOrder.nativeOrder());
        FloatBuffer fb = bb.asFloatBuffer();
        fb.put(arr);
        fb.position(0);

        return fb;
    }
}

Related

  1. drawSquare(GL10 gl, float x, float y, float r, float g, float b, float a)
  2. drawScreenSpaceQuad(GL10 gl)