Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ecu.tetrify;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import javax.microedition.khronos.opengles.GL10;
- public class Cube {
- private FloatBuffer vertexBuffer; // Buffer for vertex-array
- private FloatBuffer outlineBuffer;
- private int numFaces = 6;
- private float[] vertices;
- private float[] outlines;
- // Constructor - Set up the buffers
- public Cube() {
- }
- // Draw the shape
- public void draw(GL10 gl, int x, int y, int z, int color) {
- outlines = new float[] { // Vertices of the 6 faces
- // FRONT
- 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f),
- 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f),
- 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f),
- 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f),
- 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f),
- 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f),
- 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f),
- 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f),
- 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f),
- 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f),
- 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f),
- 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f),
- 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f),
- 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f),
- 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f),
- 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f),
- 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f),
- 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f),
- 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f),
- 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f),
- 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f),
- 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f),
- 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f),
- 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f),
- };
- vertices = new float[] { // Vertices of the 6 faces
- // FRONT
- 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 0.
- // left-bottom-front
- 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 1.
- // right-bottom-front
- 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 2.
- // left-top-front
- 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 3.
- // right-top-front
- // BACK
- 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 6.
- // right-bottom-back
- 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 4.
- // left-bottom-back
- 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 7.
- // right-top-back
- 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 5.
- // left-top-back
- // LEFT
- 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 4.
- // left-bottom-back
- 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 0.
- // left-bottom-front
- 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 5.
- // left-top-back
- 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 2.
- // left-top-front
- // RIGHT
- 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 1.
- // right-bottom-front
- 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 6.
- // right-bottom-back
- 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 3.
- // right-top-front
- 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 7.
- // right-top-back
- // TOP
- 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 2.
- // left-top-front
- 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 3.
- // right-top-front
- 0.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 5.
- // left-top-back
- 1.0f + (x * 1.0f), 1.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 7.
- // right-top-back
- // BOTTOM
- 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 4.
- // left-bottom-back
- 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 0.0f + (z * 1.0f), // 6.
- // right-bottom-back
- 0.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f), // 0.
- // left-bottom-front
- 1.0f + (x * 1.0f), 0.0f + (y * 1.0f), 1.0f + (z * 1.0f) // 1.
- // right-bottom-front
- };
- // Setup vertex-array buffer. Vertices in float. An float has 4 bytes
- ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
- vbb.order(ByteOrder.nativeOrder()); // Use native byte order
- vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
- vertexBuffer.put(vertices); // Copy data into buffer
- vertexBuffer.position(0); // Rewind
- ByteBuffer pbb = ByteBuffer.allocateDirect(outlines.length * 4);
- pbb.order(ByteOrder.nativeOrder()); // Use native byte order
- outlineBuffer = pbb.asFloatBuffer(); // Convert from byte to float
- outlineBuffer.put(outlines); // Copy data into buffer
- outlineBuffer.position(0); // Rewind
- gl.glEnable(GL10.GL_POLYGON_OFFSET_FILL);
- gl.glPolygonOffset(1f, 1f);
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
- for (int face = 0; face < numFaces; face++) {
- gl.glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
- gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, face * 4, 4);
- }
- gl.glVertexPointer(3, GL10.GL_FLOAT, 0, outlineBuffer);
- gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
- gl.glDrawArrays(GL10.GL_LINES, 0, 24);
- gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glDisable(GL10.GL_POLYGON_OFFSET_FILL);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement