Advertisement
Guest User

Untitled

a guest
Jan 21st, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.78 KB | None | 0 0
  1. package com.tomasu.game.BuckyBlocks;
  2.  
  3. import android.opengl.GLSurfaceView;
  4. import android.opengl.GLSurfaceView.Renderer;
  5. import android.util.Log;
  6. import android.content.Context;
  7. import android.util.AttributeSet;
  8. import android.view.KeyEvent;
  9. import android.view.MotionEvent;
  10. import android.os.Parcelable;
  11. import android.os.Parcel;
  12. import android.os.Bundle;
  13.  
  14. import javax.microedition.khronos.egl.EGLConfig;
  15. import javax.microedition.khronos.opengles.GL10;
  16. import static javax.microedition.khronos.opengles.GL10.*;
  17. import android.opengl.GLU;
  18.  
  19. import java.nio.ByteBuffer;
  20. import java.nio.FloatBuffer;
  21. import java.nio.ByteOrder;
  22.  
  23.  
  24. class Vertex {
  25.    private float data[] = { 0, 0, 0 };
  26.    
  27.    public Vertex(float x, float y, float z)
  28.    {
  29.       data[0] = x;
  30.       data[1] = y;
  31.       data[2] = z;
  32.    }
  33.    
  34.    public final float[] array()
  35.    {
  36.       return data;
  37.    }
  38. }
  39.  
  40. class Color {
  41.    private float data[] = { 0, 0, 0, 0 };
  42.    
  43.    public Color(float r, float g, float b, float a)
  44.    {
  45.       data[0] = r;
  46.       data[1] = g;
  47.       data[2] = b;
  48.       data[3] = a;
  49.    }
  50.    
  51.    public final float[] array()
  52.    {
  53.       return data;
  54.    }
  55. }
  56.  
  57. class TexCoord {
  58.    private float data[] = { 0, 0 };
  59.    
  60.    public TexCoord(float u, float v)
  61.    {
  62.       data[0] = u;
  63.       data[1] = v;
  64.    }
  65.    
  66.    public final float[] array()
  67.    {
  68.       return data;
  69.    }
  70. }
  71.  
  72.  
  73. class BufferPoolObject
  74. {
  75.    private int id;
  76.    private int vertexCount;
  77.    private BufferPool bufferPool;
  78.  
  79.    public BufferPoolObject(int _id, int vcount, BufferPool bp)
  80.    {
  81.       id = _id;
  82.       vertexCount = vcount;
  83.       bufferPool = bp;
  84.    }
  85.    
  86.    public void setVertexArray(Vertex[] array)
  87.    {
  88.       bufferPool.setObjectVertexArray(id, 0, array.length, array);
  89.    }
  90.    
  91.    public void setVertex(int vtx, Vertex v)
  92.    {
  93.       bufferPool.setObjectVertexArray(id, vtx, 1, new Vertex[] { v });
  94.    }
  95.    
  96.    public void setVertex(int vtx, float x, float y, float z)
  97.    {
  98.       bufferPool.setObjectVertexArray(id, vtx, 1, new Vertex[] { new Vertex(x, y, z) });
  99.    }
  100.    
  101.    public void setTexCoordArray(TexCoord[] array)
  102.    {
  103.       bufferPool.setObjectTexCoordArray(id, 0, array.length, array);
  104.    }
  105.    
  106.    public void setTexCoord(int vtx, float u, float v)
  107.    {
  108.       bufferPool.setObjectTexCoordArray(id, vtx, 1, new TexCoord[] { new TexCoord(u, v) });
  109.    }
  110.    
  111.    public void setTexCoord(int vtx, TexCoord tc)
  112.    {
  113.       bufferPool.setObjectTexCoordArray(id, vtx, 1, new TexCoord[] { tc });
  114.    }
  115.    
  116.    public void setColorArray(Color[] array)
  117.    {
  118.       bufferPool.setObjectColorArray(id, 0, array.length, array);
  119.    }
  120.    
  121.    public void setColor(int vtx, float r, float g, float b, float a)
  122.    {
  123.       bufferPool.setObjectColorArray(id, vtx, 1, new Color[] { new Color(r, g, b, a) });
  124.    }
  125.    
  126.    public void setColor(int vtx, Color c)
  127.    {
  128.       bufferPool.setObjectColorArray(id, vtx, 1, new Color[] { c });
  129.    }
  130.    
  131. }
  132.  
  133. // need to be able to remove objects, and re-pack
  134. // probably give each object its own ID
  135. class BufferPool {
  136.    private boolean bufferSizeChanged;
  137.    private int bufferSize;
  138.    
  139.    private static final int OBJECT_VERTEX_SIZE = 3;
  140.    private static final int OBJECT_VERTEX_OFFSET = 0;
  141.    
  142.    private static final int OBJECT_COLOR_SIZE = 4;
  143.    private static final int OBJECT_COLOR_OFFSET = 3;
  144.    
  145.    private static final int OBJECT_TEXCOORD_SIZE = 2;
  146.    private static final int OBJECT_TEXCOORD_OFFSET = 7;
  147.    
  148.    private static final int OBJECT_SIZE = (OBJECT_VERTEX_SIZE + OBJECT_COLOR_SIZE + OBJECT_TEXCOORD_SIZE) * 4;
  149.    
  150.    private int currentIndex;
  151.    private int indexMap[]; // key is the value returned from startPut, value is index into buffer
  152.    
  153.    private int vertexCount;
  154.    private static final int BUFFER_DEFAULT_START_SIZE = OBJECT_SIZE * 1000;
  155.    ByteBuffer data;
  156.    FloatBuffer vertexBuffer;
  157.    FloatBuffer colorBuffer;
  158.    FloatBuffer texCoordBuffer;
  159.    
  160.    public BufferPool(int num_objects)
  161.    {
  162.       data = ByteBuffer.allocateDirect(num_objects * 30 * OBJECT_SIZE);
  163.       indexMap = new int[num_objects];
  164.      
  165.       data.position(OBJECT_VERTEX_OFFSET);
  166.       vertexBuffer = data.slice().asFloatBuffer();
  167.       data.position(OBJECT_COLOR_OFFSET);
  168.       colorBuffer = data.slice().asFloatBuffer();
  169.    
  170.       //data.position(OBJECT_TEXCOORD_OFFSET);
  171.       //texCoordBuffer = data.slice().asFloatBuffer();
  172.    
  173.       data.clear();
  174.    }
  175.    
  176.    public BufferPoolObject allocateObject(int vertices)
  177.    {
  178.       if((currentIndex+1) * OBJECT_SIZE > data.capacity()) {
  179.          // TODO: resize bufferpool
  180.          Log.e("BufferPool", "BufferPool is full >:(");
  181.          return null; // will likely crash after this \o/
  182.       }
  183.      
  184.       int object_id = currentIndex;
  185.       currentIndex++;
  186.      
  187.       vertexCount += vertices;
  188.      
  189.       return new BufferPoolObject(object_id, vertices, this);
  190.    }
  191.    
  192.    public void setObjectVertexArray(int obj, int start, int count, Vertex[] array)
  193.    {
  194.       vertexBuffer.position(start * OBJECT_SIZE + OBJECT_VERTEX_OFFSET);
  195.  
  196.       for(int i = 0; i < count; i++) {
  197.          vertexBuffer.put(array[i].array());
  198.       }
  199.      
  200.       vertexBuffer.clear();
  201.    }
  202.    
  203.    public void setObjectColorArray(int obj, int start, int count, Color[] array)
  204.    {
  205.       colorBuffer.position(start * OBJECT_SIZE + OBJECT_COLOR_OFFSET);
  206.      
  207.       for(int i = 0; i < count; i++) {
  208.          colorBuffer.put(array[i].array());
  209.       }
  210.      
  211.       colorBuffer.clear();
  212.    }
  213.    
  214.    public void setObjectTexCoordArray(int obj, int start, int count, TexCoord[] array)
  215.    {
  216.       texCoordBuffer.position(start * OBJECT_SIZE + OBJECT_TEXCOORD_OFFSET);
  217.      
  218.       for(int i = 0; i < count; i++) {
  219.          texCoordBuffer.put(array[i].array());
  220.       }
  221.      
  222.       texCoordBuffer.clear();
  223.    }
  224.    
  225.    public void render(GL10 gl)
  226.    {
  227.       gl.glVertexPointer(3, GL_FLOAT, OBJECT_SIZE, vertexBuffer);
  228.       gl.glColorPointer(4, GL_FLOAT, OBJECT_SIZE, colorBuffer);
  229.      
  230.       //gl.glTexCoordPointer(2, GL_FLOAT, OBJECT_SIZE, texCoordBuffer);
  231.      
  232.       gl.glColor4f(0,0,0,1);
  233.       gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexCount);
  234.    }
  235. }
  236.  
  237. public class GLBoardView extends GLSurfaceView implements GLSurfaceView.Renderer
  238. {
  239.    BufferPoolObject rect;
  240.    private BufferPool pool;
  241.    
  242.    public GLBoardView(Context context, AttributeSet attrubuteSet)
  243.    {
  244.       super(context, attrubuteSet);
  245.       setRenderer(this);
  246.       pool = new BufferPool(1000); // 1000 objects! (30k vertices)
  247.      
  248.       rect = pool.allocateObject(4); // draw a nice rectangle!
  249.       rect.setVertex(0, new Vertex(-100,100,-4));
  250.       rect.setColor(0, new Color(1,0,0,1));
  251.      
  252.       rect.setVertex(1, new Vertex(-100,-100,-4));
  253.       rect.setColor(1, new Color(0,1,0,1));
  254.      
  255.       rect.setVertex(2, new Vertex(100,100,-4));
  256.       rect.setColor(2, new Color(0,0,1,1));
  257.      
  258.       rect.setVertex(3, new Vertex(100,-100,-4));
  259.       rect.setColor(3, new Color(1,0,1,1));
  260.  
  261.    }
  262.    
  263.    public GLBoardView(Context ctx)
  264.    {
  265.       super(ctx);
  266.    }
  267.    
  268.    public void start()
  269.    {
  270.  
  271.    }
  272.    
  273.    public void onSaveInstanceState(Bundle bundle)
  274.    {
  275.       //bundle.putParcelable("rect", rect);
  276.    }
  277.    
  278.    public void onRestoreInstanceState(Bundle bundle)
  279.    {
  280.       //rect = bundle.getParcelable("rect");
  281.    }
  282.    
  283.    /* input handling */
  284.    
  285.    public boolean onKeyDown(int keyCode, KeyEvent event)
  286.    {
  287.       return false;
  288.    }
  289.    
  290.    public boolean onKeyUp(int keyCode, KeyEvent event)
  291.    {
  292.       return false;
  293.    }
  294.    
  295.    public boolean onTouchEvent(MotionEvent event)
  296.    {
  297.       //rect.x = event.getX();
  298.       //rect.y = event.getY();
  299.       //Log.d("GLBoardView", "onTouchEvent: " + rect.x + ", " + rect.y);
  300.       requestRender();
  301.       return true;
  302.    }
  303.    
  304.    /* Renderer interface */
  305.    
  306.    private int boardWidth;
  307.    private int boardHeight;
  308.    
  309.    @Override
  310.    public void onDrawFrame(GL10 gl)
  311.    {
  312.       gl.glMatrixMode(GL_MODELVIEW);
  313.       gl.glLoadIdentity();
  314. //      Log.d("GLBoardView", "onDrawFrame");
  315.       gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  316.       gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  317.      
  318.       gl.glEnableClientState(GL_VERTEX_ARRAY);
  319.       gl.glEnableClientState(GL_COLOR_ARRAY);
  320.       //gl.glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  321.      
  322.       gl.glTranslatef(boardWidth/2, boardHeight/2, 0);
  323.  
  324.       pool.render(gl);
  325.    }
  326.    
  327.    @Override
  328.    public void onSurfaceChanged(GL10 gl, int width, int height)
  329.    {
  330.       Log.d("GLBoardView", "onSurfaceChanged");
  331.      
  332.       boardWidth = width;
  333.       boardHeight = height;
  334.      
  335.       // Sets the current view port to the new size.
  336.       gl.glViewport(0, 0, width, height);
  337.       // Select the projection matrix
  338.       gl.glMatrixMode(GL_PROJECTION);
  339.       // Reset the projection matrix
  340.       gl.glLoadIdentity();
  341.       // Calculate the aspect ratio of the window
  342.       gl.glFrustumf(0, width, height, 0, 1, 10);
  343.       // Select the modelview matrix
  344.       gl.glMatrixMode(GL_MODELVIEW);
  345.       // Reset the modelview matrix
  346.       gl.glLoadIdentity();
  347.    }
  348.    
  349.    @Override
  350.    public void onSurfaceCreated(GL10 gl, EGLConfig config)
  351.    {
  352.       Log.d("GLSurfaceView", "onSurfaceCreated");
  353.       // Set the background color to black ( rgba ).
  354.       gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  355.       // Enable Smooth Shading, default not really needed.
  356.      // gl.glShadeModel(GL_SMOOTH);
  357.       // Depth buffer setup.
  358.       gl.glClearDepthf(1.0f);
  359.       // Enables depth testing.
  360.       //gl.glEnable(GL_DEPTH_TEST);
  361.       gl.glDisable(GL_DEPTH_TEST);
  362.       gl.glDisable(GL_CULL_FACE);
  363.       // The type of depth testing to do.
  364.       //gl.glDepthFunc(GL_LEQUAL);
  365.       // Really nice perspective calculations.
  366.       gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  367.      
  368.    }
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement