Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.72 KB | None | 0 0
  1. package southgrove.game.gridball;
  2.  
  3. import android.opengl.GLES20;
  4. import android.opengl.Matrix;
  5. import southgrove.droidgl.DroidGL;
  6. import southgrove.droidgl.core.Material;
  7. import southgrove.droidgl.core.Node;
  8. import southgrove.droidgl.core.NodeBase;
  9. import southgrove.droidgl.math.MathUtils;
  10.  
  11. public class Tetromino extends NodeBase implements Node
  12. {
  13.  
  14.     public Tetromino(Types type, int gridPosX, int gridPosY, int gridPosZ, int gridRotation, Material material)
  15.     {
  16.         super(material);
  17.  
  18.         this.type = type;
  19.         this.gridPosX = gridPosX;
  20.         this.gridPosY = gridPosY;
  21.         this.gridPosZ = gridPosZ;
  22.         this.gridRotation = (int) MathUtils.wrap(gridRotation, 0, 4);
  23.         this.points = getPoints(type, gridRotation);
  24.     }
  25.  
  26.     @Override
  27.     protected void onCreated()
  28.     {
  29.         // DO NOTHING ON GL SURFACE CREATION
  30.     }
  31.  
  32.     @Override
  33.     protected void onDraw()
  34.     {
  35.         final TetrominoMesh mesh = TetrominoMesh.getMesh(type);
  36.  
  37.         rotation.x += 0.34f;
  38.         rotation.y += 0.4f;
  39.         // rotation.z = gridRotation * 45;
  40.  
  41.         positionHandle = GLES20.glGetAttribLocation(material.getShader(), "aPosition");
  42.         checkGlError("glGetAttribLocation aPosition");
  43.         if (positionHandle == -1)
  44.         {
  45.             throw new RuntimeException("Could not get attrib location for aPosition");
  46.         }
  47.  
  48.         textureHandle = GLES20.glGetAttribLocation(material.getShader(), "aTextureCoord");
  49.         checkGlError("glGetAttribLocation aTextureCoord");
  50.         if (textureHandle == -1)
  51.         {
  52.             throw new RuntimeException("Could not get attrib location for aTextureCoord");
  53.         }
  54.  
  55.         mvpMatrixHandle = GLES20.glGetUniformLocation(material.getShader(), "uMVPMatrix");
  56.         checkGlError("glGetUniformLocation uMVPMatrix");
  57.         if (mvpMatrixHandle == -1)
  58.         {
  59.             throw new RuntimeException("Could not get attrib location for uMVPMatrix");
  60.         }
  61.  
  62.         mesh.getVertexBuffer().position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
  63.         GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mesh.getVertexBuffer());
  64.         checkGlError("glVertexAttribPointer maPosition");
  65.  
  66.         mesh.getVertexBuffer().position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
  67.         GLES20.glEnableVertexAttribArray(positionHandle);
  68.         checkGlError("glEnableVertexAttribArray positionHandle");
  69.  
  70.         GLES20.glVertexAttribPointer(textureHandle, 2, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mesh.getVertexBuffer());
  71.         checkGlError("glVertexAttribPointer textureHandle");
  72.  
  73.         GLES20.glEnableVertexAttribArray(textureHandle);
  74.         checkGlError("glEnableVertexAttribArray textureHandle");
  75.  
  76.         Matrix.setIdentityM(modelMatrix, 0);
  77.         Matrix.translateM(modelMatrix, 0, position.x, position.y, position.z);
  78.         Matrix.rotateM(modelMatrix, 0, rotation.x, 1f, 0, 0);
  79.         Matrix.rotateM(modelMatrix, 0, rotation.y, 0, 1f, 0);
  80.         Matrix.rotateM(modelMatrix, 0, rotation.z, 0, 0, 1f);
  81.  
  82.         final float[] mvpMatrix = DroidGL.getActiveCamera().getModelViewProjectionMatrix(modelMatrix);
  83.         GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0);
  84.  
  85.         //mesh.getVertexBuffer().position(0);
  86.         //mesh.getIndexBuffer().position(0);
  87.  
  88.         GLES20.glDrawElements(GLES20.GL_TRIANGLES, mesh.getNumIndices(), GLES20.GL_UNSIGNED_SHORT, mesh.getIndexBuffer());
  89.     }
  90.  
  91.     public void addToGrid(Grid gridBall)
  92.     {
  93.         for (int[] point : points)
  94.         {
  95.             final Cell c = gridBall.cell(gridPosX + point[0], gridPosY + point[1], gridPosZ);
  96.  
  97.             c.setIsTetrominoCenter(point[0] == 0 && point[1] == 0);
  98.             c.setTetromino(this);
  99.         }
  100.     }
  101.  
  102.     public void removeFromGrid(Grid gridBall)
  103.     {
  104.         for (int[] point : points)
  105.         {
  106.             final Cell c = gridBall.cell(gridPosX + point[0], gridPosY + point[1], gridPosZ);
  107.  
  108.             c.setIsTetrominoCenter(false);
  109.             c.setTetromino(null);
  110.         }
  111.     }
  112.  
  113.     public Types getType()
  114.     {
  115.         return type;
  116.     }
  117.  
  118.     public int getGridPosX()
  119.     {
  120.         return gridPosX;
  121.     }
  122.  
  123.     public int getGridPosY()
  124.     {
  125.         return gridPosY;
  126.     }
  127.  
  128.     public int getGridPosZ()
  129.     {
  130.         return gridPosZ;
  131.     }
  132.  
  133.     public int getGridRotation()
  134.     {
  135.         return gridRotation;
  136.     }
  137.  
  138.     private Types               type;
  139.     private int                 gridPosX                            = 0;
  140.     private int                 gridPosY                            = 0;
  141.     private int                 gridPosZ                            = 0;
  142.     private int                 gridRotation                        = 0;
  143.     private int[][]             points;
  144.  
  145.     private float[]             modelMatrix                         = new float[16];
  146.  
  147.     private int                 positionHandle;
  148.     private int                 textureHandle;
  149.     private int                 mvpMatrixHandle;
  150.  
  151.     private static final int    FLOAT_SIZE_BYTES                    = 4;
  152.     private static final int    TRIANGLE_VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES;
  153.     private static final int    TRIANGLE_VERTICES_DATA_POS_OFFSET   = 0;
  154.     private static final int    TRIANGLE_VERTICES_DATA_UV_OFFSET    = 3;
  155.  
  156.     public static enum Types
  157.     {
  158.         I, J, L, O, S, Z, T
  159.     }
  160.  
  161.     protected static int[][] getPoints(Types type, int rotation)
  162.     {
  163.         switch (type)
  164.         {
  165.             case I:
  166.                 return I[(int) MathUtils.wrap(rotation, 0, 2)];
  167.  
  168.             case J:
  169.                 return J[(int) MathUtils.wrap(rotation, 0, 4)];
  170.  
  171.             case L:
  172.                 return L[(int) MathUtils.wrap(rotation, 0, 4)];
  173.  
  174.             case O:
  175.                 return O[0];
  176.  
  177.             case S:
  178.                 return S[(int) MathUtils.wrap(rotation, 0, 2)];
  179.  
  180.             case Z:
  181.                 return Z[(int) MathUtils.wrap(rotation, 0, 2)];
  182.  
  183.             case T:
  184.                 return T[(int) MathUtils.wrap(rotation, 0, 4)];
  185.  
  186.             default:
  187.                 return null;
  188.  
  189.         }
  190.     }
  191.  
  192.     private final static int[][][]  I   = new int[][][] {
  193.                                         { { 0, 1 }, { 0, 0 }, { 0, -1 }, { 0, -2 } },
  194.                                         { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 2, 0 } }
  195.                                         };
  196.  
  197.     private final static int[][][]  J   = new int[][][] {
  198.                                         { { 0, 1 }, { 0, 0 }, { -1, -1 }, { 0, -1 } },
  199.                                         { { -1, 1 }, { -1, 0 }, { 0, 0 }, { 1, 0 } },
  200.                                         { { 0, 1 }, { 1, 1 }, { 0, 0 }, { 0, -1 } },
  201.                                         { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 1, -1 } },
  202.                                         };
  203.  
  204.     private final static int[][][]  L   = new int[][][] {
  205.                                         { { 0, 1 }, { 0, 0 }, { 0, -1 }, { 1, -1 } },
  206.                                         { { -1, 0 }, { 0, 0 }, { 1, 0 }, { -1, -1 } },
  207.                                         { { -1, 1 }, { 0, 1 }, { 0, 0 }, { 0, -1 } },
  208.                                         { { 1, 1 }, { -1, 0 }, { 0, 0 }, { 1, 0 } },
  209.                                         };
  210.  
  211.     private final static int[][][]  O   = new int[][][] {
  212.                                         { { 0, 0 }, { 1, 0 }, { 0, -1 }, { 1, -1 } }
  213.                                         };
  214.  
  215.     private final static int[][][]  S   = new int[][][] {
  216.                                         { { 0, 1 }, { 0, 0 }, { 1, 0 }, { 1, -1 } },
  217.                                         { { 0, 1 }, { 1, 1 }, { -1, 0 }, { 0, 0 } }
  218.                                         };
  219.  
  220.     private final static int[][][]  Z   = new int[][][] {
  221.                                         { { 0, 1 }, { 0, 0 }, { -1, 0 }, { -1, -1 } },
  222.                                         { { -1, 0 }, { 0, 0 }, { 0, -1 }, { 1, -1 } }
  223.                                         };
  224.  
  225.     private final static int[][][]  T   = new int[][][] {
  226.                                         { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 0, -1 } },
  227.                                         { { 0, 1 }, { -1, 0 }, { 0, 0 }, { 0, -1 } },
  228.                                         { { 0, 1 }, { -1, 0 }, { 0, 0 }, { 1, 0 } },
  229.                                         { { 0, 1 }, { 0, 0 }, { 1, 0 }, { 0, -1 } },
  230.                                         };
  231.  
  232.     private final static String     TAG = "Tetromino";
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement