Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.android.view.visualization;
00018
00019 import com.google.common.base.Preconditions;
00020
00021 import java.nio.ByteBuffer;
00022 import java.nio.ByteOrder;
00023 import java.nio.FloatBuffer;
00024
00025 import javax.microedition.khronos.opengles.GL10;
00026
00030 public class Vertices {
00031
00032 private static final int FLOAT_BYTE_SIZE = Float.SIZE / 8;
00033
00034 private Vertices() {
00035
00036 }
00037
00038 public static FloatBuffer allocateBuffer(int size) {
00039 ByteBuffer byteBuffer = ByteBuffer.allocateDirect(size * FLOAT_BYTE_SIZE);
00040 byteBuffer.order(ByteOrder.nativeOrder());
00041 return byteBuffer.asFloatBuffer();
00042 }
00043
00044 public static FloatBuffer toFloatBuffer(float[] floats) {
00045 FloatBuffer floatBuffer = allocateBuffer(floats.length);
00046 floatBuffer.put(floats);
00047 floatBuffer.position(0);
00048 return floatBuffer;
00049 }
00050
00051 public static void drawPoints(GL10 gl, FloatBuffer vertices, Color color, float size) {
00052 vertices.mark();
00053 color.apply(gl);
00054 gl.glPointSize(size);
00055 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
00056 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);
00057 gl.glDrawArrays(GL10.GL_POINTS, 0, countVertices(vertices, 3));
00058 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
00059 vertices.reset();
00060 }
00061
00062 public static void drawTriangleFan(GL10 gl, FloatBuffer vertices, Color color) {
00063 vertices.mark();
00064 color.apply(gl);
00065 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
00066 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);
00067 gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, countVertices(vertices, 3));
00068 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
00069 vertices.reset();
00070 }
00071
00072 public static void drawLineLoop(GL10 gl, FloatBuffer vertices, Color color, float width) {
00073 vertices.mark();
00074 color.apply(gl);
00075 gl.glLineWidth(width);
00076 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
00077 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);
00078 gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, countVertices(vertices, 3));
00079 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
00080 vertices.reset();
00081 }
00082
00083 public static void drawLines(GL10 gl, FloatBuffer vertices, Color color, float width) {
00084 vertices.mark();
00085 color.apply(gl);
00086 gl.glLineWidth(width);
00087 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
00088 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);
00089 gl.glDrawArrays(GL10.GL_LINES, 0, countVertices(vertices, 3));
00090 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
00091 vertices.reset();
00092 }
00093
00094 private static int countVertices(FloatBuffer vertices, int size) {
00095
00096 Preconditions.checkArgument(vertices.remaining() % size == 0,
00097 "Number of vertices: " + vertices.remaining());
00098 return vertices.remaining() / size;
00099 }
00100 }