Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.01 KB | None | 0 0
  1. package com.ecu.tetrify;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.nio.ByteOrder;
  5. import java.nio.FloatBuffer;
  6.  
  7. import javax.microedition.khronos.opengles.GL10;
  8.  
  9. public class Cube {
  10.     private FloatBuffer vertexBuffer; // Buffer for vertex-array
  11.     private FloatBuffer outlineBuffer;
  12.  
  13.     private int numFaces = 6;
  14.     private float[] vertices;
  15.     private float[] outlines;
  16.    
  17.     // Constructor - Set up the buffers
  18.     public Cube() {
  19.     }
  20.  
  21.     // Draw the shape
  22.     public void draw(GL10 gl, int x, int y, int z, int color) {
  23.  
  24.         outlines = new float[] { // Vertices of the 6 faces
  25.         // FRONT
  26.                 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f),
  27.                 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f),
  28.                 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f),
  29.                 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f),
  30.                
  31.                 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f),
  32.                 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f),
  33.                 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f),
  34.                 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f),
  35.                
  36.                 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f),
  37.                 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f),
  38.                 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f),
  39.                 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f),
  40.                
  41.                 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f),
  42.                 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f),
  43.                 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f),
  44.                 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f),
  45.                
  46.                 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f),
  47.                 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f),
  48.                 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f),
  49.                 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f),
  50.        
  51.                 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f),
  52.                 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f),
  53.                 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f),
  54.                 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f),
  55.                
  56.  
  57.         };
  58.  
  59.        
  60.         vertices = new float[] { // Vertices of the 6 faces
  61.         // FRONT
  62.                 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 0.
  63.                                                                             // left-bottom-front
  64.                 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 1.
  65.                                                                             // right-bottom-front
  66.                 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 2.
  67.                                                                             // left-top-front
  68.                 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 3.
  69.                                                                             // right-top-front
  70.                 // BACK
  71.                 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 6.
  72.                                                                             // right-bottom-back
  73.                 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 4.
  74.                                                                             // left-bottom-back
  75.                 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 7.
  76.                                                                             // right-top-back
  77.                 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 5.
  78.                                                                             // left-top-back
  79.                 // LEFT
  80.                 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 4.
  81.                                                                             // left-bottom-back
  82.                 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 0.
  83.                                                                             // left-bottom-front
  84.                 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 5.
  85.                                                                             // left-top-back
  86.                 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 2.
  87.                                                                             // left-top-front
  88.                 // RIGHT
  89.                 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 1.
  90.                                                                             // right-bottom-front
  91.                 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 6.
  92.                                                                             // right-bottom-back
  93.                 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 3.
  94.                                                                             // right-top-front
  95.                 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 7.
  96.                                                                             // right-top-back
  97.                 // TOP
  98.                 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 2.
  99.                                                                             // left-top-front
  100.                 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 3.
  101.                                                                             // right-top-front
  102.                 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 5.
  103.                                                                             // left-top-back
  104.                 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 7.
  105.                                                                             // right-top-back
  106.                 // BOTTOM
  107.                 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 4.
  108.                                                                             // left-bottom-back
  109.                 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 6.
  110.                                                                             // right-bottom-back
  111.                 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 0.
  112.                                                                             // left-bottom-front
  113.                 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f) // 1.
  114.                                                                         // right-bottom-front
  115.         };
  116.         // Setup vertex-array buffer. Vertices in float. An float has 4 bytes
  117.         ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
  118.         vbb.order(ByteOrder.nativeOrder()); // Use native byte order
  119.         vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
  120.         vertexBuffer.put(vertices); // Copy data into buffer
  121.         vertexBuffer.position(0); // Rewind
  122.  
  123.         ByteBuffer pbb = ByteBuffer.allocateDirect(outlines.length * 4);
  124.         pbb.order(ByteOrder.nativeOrder()); // Use native byte order
  125.         outlineBuffer = pbb.asFloatBuffer(); // Convert from byte to float
  126.         outlineBuffer.put(outlines); // Copy data into buffer
  127.         outlineBuffer.position(0); // Rewind
  128.        
  129.        
  130.      
  131.         gl.glEnable(GL10.GL_POLYGON_OFFSET_FILL);
  132.         gl.glPolygonOffset(1f, 1f);
  133.          
  134.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  135.        
  136.         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
  137.         for (int face = 0; face < numFaces; face++) {
  138.             gl.glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
  139.             gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, face * 4, 4);
  140.         }
  141.        
  142.        
  143.        
  144.         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, outlineBuffer);
  145.         gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  146.         gl.glDrawArrays(GL10.GL_LINES, 0, 24);
  147.        
  148.         gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  149.        
  150.         gl.glDisable(GL10.GL_POLYGON_OFFSET_FILL);
  151.            
  152.        
  153.     }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement