Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tomasu.game.BuckyBlocks;
- import android.opengl.GLSurfaceView;
- import android.opengl.GLSurfaceView.Renderer;
- import android.util.Log;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.KeyEvent;
- import android.view.MotionEvent;
- import android.os.Parcelable;
- import android.os.Parcel;
- import android.os.Bundle;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import static javax.microedition.khronos.opengles.GL10.*;
- import android.opengl.GLU;
- import java.nio.ByteBuffer;
- import java.nio.FloatBuffer;
- import java.nio.ByteOrder;
- class Vertex {
- private float data[] = { 0, 0, 0 };
- public Vertex(float x, float y, float z)
- {
- data[0] = x;
- data[1] = y;
- data[2] = z;
- }
- public final float[] array()
- {
- return data;
- }
- }
- class Color {
- private float data[] = { 0, 0, 0, 0 };
- public Color(float r, float g, float b, float a)
- {
- data[0] = r;
- data[1] = g;
- data[2] = b;
- data[3] = a;
- }
- public final float[] array()
- {
- return data;
- }
- }
- class TexCoord {
- private float data[] = { 0, 0 };
- public TexCoord(float u, float v)
- {
- data[0] = u;
- data[1] = v;
- }
- public final float[] array()
- {
- return data;
- }
- }
- class BufferPoolObject
- {
- private int id;
- private int vertexCount;
- private BufferPool bufferPool;
- public BufferPoolObject(int _id, int vcount, BufferPool bp)
- {
- id = _id;
- vertexCount = vcount;
- bufferPool = bp;
- }
- public void setVertexArray(Vertex[] array)
- {
- bufferPool.setObjectVertexArray(id, 0, array.length, array);
- }
- public void setVertex(int vtx, Vertex v)
- {
- bufferPool.setObjectVertexArray(id, vtx, 1, new Vertex[] { v });
- }
- public void setVertex(int vtx, float x, float y, float z)
- {
- bufferPool.setObjectVertexArray(id, vtx, 1, new Vertex[] { new Vertex(x, y, z) });
- }
- public void setTexCoordArray(TexCoord[] array)
- {
- bufferPool.setObjectTexCoordArray(id, 0, array.length, array);
- }
- public void setTexCoord(int vtx, float u, float v)
- {
- bufferPool.setObjectTexCoordArray(id, vtx, 1, new TexCoord[] { new TexCoord(u, v) });
- }
- public void setTexCoord(int vtx, TexCoord tc)
- {
- bufferPool.setObjectTexCoordArray(id, vtx, 1, new TexCoord[] { tc });
- }
- public void setColorArray(Color[] array)
- {
- bufferPool.setObjectColorArray(id, 0, array.length, array);
- }
- public void setColor(int vtx, float r, float g, float b, float a)
- {
- bufferPool.setObjectColorArray(id, vtx, 1, new Color[] { new Color(r, g, b, a) });
- }
- public void setColor(int vtx, Color c)
- {
- bufferPool.setObjectColorArray(id, vtx, 1, new Color[] { c });
- }
- }
- // need to be able to remove objects, and re-pack
- // probably give each object its own ID
- class BufferPool {
- private boolean bufferSizeChanged;
- private int bufferSize;
- private static final int OBJECT_VERTEX_SIZE = 3;
- private static final int OBJECT_VERTEX_OFFSET = 0;
- private static final int OBJECT_COLOR_SIZE = 4;
- private static final int OBJECT_COLOR_OFFSET = 3;
- private static final int OBJECT_TEXCOORD_SIZE = 2;
- private static final int OBJECT_TEXCOORD_OFFSET = 7;
- private static final int OBJECT_SIZE = (OBJECT_VERTEX_SIZE + OBJECT_COLOR_SIZE + OBJECT_TEXCOORD_SIZE) * 4;
- private int currentIndex;
- private int indexMap[]; // key is the value returned from startPut, value is index into buffer
- private int vertexCount;
- private static final int BUFFER_DEFAULT_START_SIZE = OBJECT_SIZE * 1000;
- ByteBuffer data;
- FloatBuffer vertexBuffer;
- FloatBuffer colorBuffer;
- FloatBuffer texCoordBuffer;
- public BufferPool(int num_objects)
- {
- data = ByteBuffer.allocateDirect(num_objects * 30 * OBJECT_SIZE);
- indexMap = new int[num_objects];
- data.position(OBJECT_VERTEX_OFFSET);
- vertexBuffer = data.slice().asFloatBuffer();
- data.position(OBJECT_COLOR_OFFSET);
- colorBuffer = data.slice().asFloatBuffer();
- //data.position(OBJECT_TEXCOORD_OFFSET);
- //texCoordBuffer = data.slice().asFloatBuffer();
- data.clear();
- }
- public BufferPoolObject allocateObject(int vertices)
- {
- if((currentIndex+1) * OBJECT_SIZE > data.capacity()) {
- // TODO: resize bufferpool
- Log.e("BufferPool", "BufferPool is full >:(");
- return null; // will likely crash after this \o/
- }
- int object_id = currentIndex;
- currentIndex++;
- vertexCount += vertices;
- return new BufferPoolObject(object_id, vertices, this);
- }
- public void setObjectVertexArray(int obj, int start, int count, Vertex[] array)
- {
- vertexBuffer.position(start * OBJECT_SIZE + OBJECT_VERTEX_OFFSET);
- for(int i = 0; i < count; i++) {
- vertexBuffer.put(array[i].array());
- }
- vertexBuffer.clear();
- }
- public void setObjectColorArray(int obj, int start, int count, Color[] array)
- {
- colorBuffer.position(start * OBJECT_SIZE + OBJECT_COLOR_OFFSET);
- for(int i = 0; i < count; i++) {
- colorBuffer.put(array[i].array());
- }
- colorBuffer.clear();
- }
- public void setObjectTexCoordArray(int obj, int start, int count, TexCoord[] array)
- {
- texCoordBuffer.position(start * OBJECT_SIZE + OBJECT_TEXCOORD_OFFSET);
- for(int i = 0; i < count; i++) {
- texCoordBuffer.put(array[i].array());
- }
- texCoordBuffer.clear();
- }
- public void render(GL10 gl)
- {
- gl.glVertexPointer(3, GL_FLOAT, OBJECT_SIZE, vertexBuffer);
- gl.glColorPointer(4, GL_FLOAT, OBJECT_SIZE, colorBuffer);
- //gl.glTexCoordPointer(2, GL_FLOAT, OBJECT_SIZE, texCoordBuffer);
- gl.glColor4f(0,0,0,1);
- gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexCount);
- }
- }
- public class GLBoardView extends GLSurfaceView implements GLSurfaceView.Renderer
- {
- BufferPoolObject rect;
- private BufferPool pool;
- public GLBoardView(Context context, AttributeSet attrubuteSet)
- {
- super(context, attrubuteSet);
- setRenderer(this);
- pool = new BufferPool(1000); // 1000 objects! (30k vertices)
- rect = pool.allocateObject(4); // draw a nice rectangle!
- rect.setVertex(0, new Vertex(-100,100,-4));
- rect.setColor(0, new Color(1,0,0,1));
- rect.setVertex(1, new Vertex(-100,-100,-4));
- rect.setColor(1, new Color(0,1,0,1));
- rect.setVertex(2, new Vertex(100,100,-4));
- rect.setColor(2, new Color(0,0,1,1));
- rect.setVertex(3, new Vertex(100,-100,-4));
- rect.setColor(3, new Color(1,0,1,1));
- }
- public GLBoardView(Context ctx)
- {
- super(ctx);
- }
- public void start()
- {
- }
- public void onSaveInstanceState(Bundle bundle)
- {
- //bundle.putParcelable("rect", rect);
- }
- public void onRestoreInstanceState(Bundle bundle)
- {
- //rect = bundle.getParcelable("rect");
- }
- /* input handling */
- public boolean onKeyDown(int keyCode, KeyEvent event)
- {
- return false;
- }
- public boolean onKeyUp(int keyCode, KeyEvent event)
- {
- return false;
- }
- public boolean onTouchEvent(MotionEvent event)
- {
- //rect.x = event.getX();
- //rect.y = event.getY();
- //Log.d("GLBoardView", "onTouchEvent: " + rect.x + ", " + rect.y);
- requestRender();
- return true;
- }
- /* Renderer interface */
- private int boardWidth;
- private int boardHeight;
- @Override
- public void onDrawFrame(GL10 gl)
- {
- gl.glMatrixMode(GL_MODELVIEW);
- gl.glLoadIdentity();
- // Log.d("GLBoardView", "onDrawFrame");
- gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- gl.glEnableClientState(GL_VERTEX_ARRAY);
- gl.glEnableClientState(GL_COLOR_ARRAY);
- //gl.glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- gl.glTranslatef(boardWidth/2, boardHeight/2, 0);
- pool.render(gl);
- }
- @Override
- public void onSurfaceChanged(GL10 gl, int width, int height)
- {
- Log.d("GLBoardView", "onSurfaceChanged");
- boardWidth = width;
- boardHeight = height;
- // Sets the current view port to the new size.
- gl.glViewport(0, 0, width, height);
- // Select the projection matrix
- gl.glMatrixMode(GL_PROJECTION);
- // Reset the projection matrix
- gl.glLoadIdentity();
- // Calculate the aspect ratio of the window
- gl.glFrustumf(0, width, height, 0, 1, 10);
- // Select the modelview matrix
- gl.glMatrixMode(GL_MODELVIEW);
- // Reset the modelview matrix
- gl.glLoadIdentity();
- }
- @Override
- public void onSurfaceCreated(GL10 gl, EGLConfig config)
- {
- Log.d("GLSurfaceView", "onSurfaceCreated");
- // Set the background color to black ( rgba ).
- gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- // Enable Smooth Shading, default not really needed.
- // gl.glShadeModel(GL_SMOOTH);
- // Depth buffer setup.
- gl.glClearDepthf(1.0f);
- // Enables depth testing.
- //gl.glEnable(GL_DEPTH_TEST);
- gl.glDisable(GL_DEPTH_TEST);
- gl.glDisable(GL_CULL_FACE);
- // The type of depth testing to do.
- //gl.glDepthFunc(GL_LEQUAL);
- // Really nice perspective calculations.
- gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement