Java Cube cubeVertexArray(float x, float y, float z, float size)

Here you can find the source of cubeVertexArray(float x, float y, float z, float size)

Description

Calculate a GL_QUAD compatible representation of a given cube.

License

Open Source License

Parameter

Parameter Description
x The x-position of the cube.
y The y-position of the cube.
z The z-position of the cube.
size The size length of the cube.

Return

An array of vertex coordinates that should be rendered 4 vertices at a time to form a cube.

Declaration

public static float[] cubeVertexArray(float x, float y, float z, float size) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from   w ww . ja  va2  s  .  com
     * Calculate a GL_QUAD compatible representation of a given cube.
     * 
     * @param x
     *            The x-position of the cube.
     * @param y
     *            The y-position of the cube.
     * @param z
     *            The z-position of the cube.
     * @param size
     *            The size length of the cube.
     * @return An array of vertex coordinates that should be rendered 4 vertices
     *         at a time to form a cube.
     */
    public static float[] cubeVertexArray(float x, float y, float z, float size) {
        float[][][] cube = createCubeFaces(createCubeVertices(x, y, z, size));

        // 6 faces * 4 vertices / face * 3 coordinates / vertex
        float[] result = new float[6 * 4 * 3];

        for (int f = 0; f < 6; f++) {
            for (int v = 0; v < 4; v++) {
                for (int c = 0; c < 3; c++) {
                    result[3 * 4 * f + 3 * v + c] = cube[f][v][c];
                }
            }
        }

        return result;
    }

    /**
     * Given the vertices of a cube, calculate its faces.
     * 
     * @param vertices
     *            An array of vertices from createCubeVertices.
     * @return An array of faces.
     */
    public static float[][][] createCubeFaces(float[][] vertices) {
        float[][] fc1 = new float[][] { vertices[3], vertices[2], vertices[1], vertices[0] }; // y- 4,3,2,1

        float[][] fc2 = new float[][] { vertices[4], vertices[5], vertices[1], vertices[0] }; // z+ 5,6,2,1
        float[][] fc3 = new float[][] { vertices[5], vertices[6], vertices[2], vertices[1] }; // x+ 6,7,3,2
        float[][] fc4 = new float[][] { vertices[6], vertices[7], vertices[3], vertices[2] }; // z- 7,8,4,3
        float[][] fc5 = new float[][] { vertices[7], vertices[4], vertices[0], vertices[3] }; // x- 8,5,1,4

        float[][] fc6 = new float[][] { vertices[4], vertices[5], vertices[6], vertices[7] }; // y+ 5,6,7,8

        float[][][] result = new float[][][] { fc1, fc2, fc3, fc4, fc5, fc6 };

        return result;
    }

    /**
     * Calculates the vertices of a given cube.
     * 
     * @param x
     *            The x-position of the cube.
     * @param y
     *            The y-position of the cube.
     * @param z
     *            The z-position of the cube.
     * @param size
     *            The size length of the cube.
     * @return An array of vertices.
     */
    public static float[][] createCubeVertices(float x, float y, float z, float size) {
        float offset = size / 2;

        return createCuboidVertices(x - offset, y - offset, z - offset, x + offset, y + offset, z + offset);
    }

    public static float[][] createCuboidVertices(float x1, float y1, float z1, float x2, float y2, float z2) {
        float[] vx1 = new float[] { x1, y1, z2 }; // --+
        float[] vx2 = new float[] { x2, y1, z2 }; // +-+
        float[] vx3 = new float[] { x2, y1, z1 }; // +--
        float[] vx4 = new float[] { x1, y1, z1 }; // ---

        float[] vx5 = new float[] { vx1[0], y2, vx1[2] }; // -++
        float[] vx6 = new float[] { vx2[0], y2, vx2[2] }; // +++
        float[] vx7 = new float[] { vx3[0], y2, vx3[2] }; // ++-
        float[] vx8 = new float[] { vx4[0], y2, vx4[2] }; // -+-

        float[][] result = new float[][] { vx1, vx2, vx3, vx4, vx5, vx6, vx7, vx8 };

        return result;
    }
}

Related

  1. cubed(final int input)
  2. cubeIntersectsSphere(int x1, int y1, int z1, int x2, int y2, int z2, int sX, int sY, int sZ, int radius)
  3. cubeTextureArray(float x0, float y0, float x1, float y1)
  4. cubeTextureFace(float x0, float y0, float x1, float y1)
  5. cubeToBounds(float[] target, float x, float y, float z, float w, float d, float h)