Vertices.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
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     // Utility class.
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     color.apply(gl);
00053     gl.glPointSize(size);
00054     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
00055     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);
00056     gl.glDrawArrays(GL10.GL_POINTS, 0, countVertices(vertices, 3));
00057     gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
00058   }
00059 
00060   public static void drawTriangleFan(GL10 gl, FloatBuffer vertices, Color color) {
00061     color.apply(gl);
00062     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
00063     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);
00064     gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, countVertices(vertices, 3));
00065     gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
00066   }
00067 
00068   private static int countVertices(FloatBuffer vertices, int size) {
00069     // FloatBuffer accounts for the size of each float when calling remaining().
00070     Preconditions.checkArgument(vertices.remaining() % size == 0);
00071     return vertices.remaining() / size;
00072   }
00073 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Aug 27 2015 12:11:33