Android Open Source - touchgloid Buffer






From Project

Back to project page touchgloid.

License

The source code is released under:

MIT License

If you think the Android project touchgloid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package rucamzu.opengl;
//w  ww  . j ava  2 s .  c o  m
import static android.opengl.GLES20.glBindBuffer;
import static android.opengl.GLES20.glBufferData;
import static android.opengl.GLES20.glDeleteBuffers;
import static android.opengl.GLES20.glGenBuffers;
import static android.opengl.GLES20.glIsBuffer;

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

public abstract class Buffer extends Object {

  private int capacity = 0;

  protected static int generateBufferId() {
    int[] id = { 0 };

    glGenBuffers(1, id, 0);

    return id[0];
  }

  protected Buffer(int id) {
    super(id);
  }

  @Override
  public boolean ok() {
    if(!super.ok())
      return false;
    
    return glIsBuffer(id);
  }
  
  public void bind() {
    glBindBuffer(target(), id);
  }

  public void unbind() {
    glBindBuffer(target(), 0);
  }

  protected void buffer(FloatBuffer buffer, int usage) {
    glBufferData(target(), buffer.capacity() * 4, buffer, usage);
    
    capacity = buffer.capacity();
  }

  public static FloatBuffer buildFloatBuffer(float[] vertices) {
    FloatBuffer buffer = ByteBuffer
        .allocateDirect(vertices.length * 4)
        .order(ByteOrder.nativeOrder())
        .asFloatBuffer();
    
    buffer.put(vertices);
    buffer.position(0);
    
    return buffer;
  }

  protected void buffer(ShortBuffer buffer, int usage) {
    glBufferData(target(), buffer.capacity() * 2, buffer, usage);
    
    capacity = buffer.capacity();
  }
  
  public static ShortBuffer buildShortBuffer(short[] indices) {
    ShortBuffer buffer = ByteBuffer
        .allocateDirect(indices.length * 2)
        .order(ByteOrder.nativeOrder())
        .asShortBuffer();
    
    buffer.put(indices);
    buffer.position(0);
    
    return buffer;
  }

  public int capacity() {
    return capacity;
  }
  
  @Override
   public void delete() {
    int[] id = { this.id };
    
    glDeleteBuffers(1, id, 0);
  }
  
  protected abstract int target();
}




Java Source Code List

rucamzu.opengl.Buffer.java
rucamzu.opengl.FragmentShader.java
rucamzu.opengl.IndexBuffer.java
rucamzu.opengl.Object.java
rucamzu.opengl.Program.java
rucamzu.opengl.Shader.java
rucamzu.opengl.Transform.java
rucamzu.opengl.Vector.java
rucamzu.opengl.VertexBuffer.java
rucamzu.opengl.VertexShader.java
rucamzu.touchgloid.TouchgloidActivity.java
rucamzu.touchgloid.TouchgloidRenderer.java
rucamzu.touchgloid.TouchgloidView.java