Android Open Source - TinyVoxel Block Builder






From Project

Back to project page TinyVoxel.

License

The source code is released under:

GNU General Public License

If you think the Android project TinyVoxel listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.toet.TinyVoxel.Renderer;
/*w  w  w.j av  a  2s  . c o m*/

import com.toet.TinyVoxel.Config;
import com.toet.TinyVoxel.Renderer.Bundles.Grid;
import com.toet.TinyVoxel.Renderer.Bundles.TinyGrid;

import java.nio.FloatBuffer;

/**
 * Created by Kajos on 20-1-14.
 */
public class BlockBuilder {
    public static float low = .0001f;
    public static final boolean[] CUBE = {

            // x-
            false,false,false,
            false,false, true,
            false, true, true,

            false,false,false,
            false, true, true,
            false, true,false,

            // x+
            true, true, true,
            true,false,false,
            true, true,false,

            true,false,false,
            true, true, true,
            true,false, true,

            // y-
            true,false, true,
            false,false, true,
            false,false,false,

            true,false, true,
            false,false,false,
            true,false,false,

            // y+
            true, true, true,
            true, true,false,
            false, true,false,

            true, true, true,
            false, true,false,
            false, true, true,

            // z-
            true, true,false,
            false,false,false,
            false, true,false,

            true, true,false,
            true,false,false,
            false,false,false,

            // z+
            true, true, true,
            false, true, true,
            true, false, true,

            false, true, true,
            false, false, true,
            true,false, true,
    };

    public static TinyGrid getNeighbor(Grid grid, int x, int y, int z, int dx, int dy, int dz) {
        if (dx < 0) {
            TinyGrid cont = null;
            if (x == 0) {
                Grid nGrid = grid.owner.getGridSafe(grid.x - 1, grid.y, grid.z);
                if (nGrid != null) {
                    cont = nGrid.getTinyGrid(Config.GRID_SIZE - 1, y, z);
                }
            } else {
                cont = grid.getTinyGrid(x - 1, y, z);
            }
            return cont;

        } else if (dy < 0) {
            TinyGrid cont = null;
            if (y == 0) {
                Grid nGrid = grid.owner.getGridSafe(grid.x, grid.y - 1, grid.z);
                if (nGrid != null) {
                    cont = nGrid.getTinyGrid(x, Config.GRID_SIZE - 1, z);
                }
            } else {
                cont = grid.getTinyGrid(x, y - 1, z);
            }
            return cont;

        } else if (dz < 0) {
            TinyGrid cont = null;
            if (z == 0) {
                Grid nGrid = grid.owner.getGridSafe(grid.x, grid.y, grid.z - 1);
                if (nGrid != null) {
                    cont = nGrid.getTinyGrid(x, y, Config.GRID_SIZE - 1);
                }
            } else {
                cont = grid.getTinyGrid(x, y, z - 1);
            }
            return cont;

        } else if (dx > 0) {
            TinyGrid cont = null;
            if (x >= Config.GRID_SIZE - 1) {
                Grid nGrid = grid.owner.getGridSafe(grid.x + 1, grid.y, grid.z);
                if (nGrid != null) {
                    cont = nGrid.getTinyGrid(0, y, z);
                }
            } else {
                cont = grid.getTinyGrid(x + 1, y, z);
            }
            return cont;

        } else if (dy > 0) {
            TinyGrid cont = null;
            if (y >= Config.GRID_SIZE - 1) {
                Grid nGrid = grid.owner.getGridSafe(grid.x, grid.y + 1, grid.z);
                if (nGrid != null) {
                    cont = nGrid.getTinyGrid(x, 0, z);
                }
            } else {
                cont = grid.getTinyGrid(x, y + 1, z);
            }
            return cont;

        } else if (dz > 0) {
            TinyGrid cont = null;
            if (z >= Config.GRID_SIZE - 1) {
                Grid nGrid = grid.owner.getGridSafe(grid.x, grid.y, grid.z + 1);
                if (nGrid != null) {
                    cont = nGrid.getTinyGrid(x, y, 0);
                }
            } else {
                cont = grid.getTinyGrid(x, y, z + 1);
            }
            return cont;

        }
        return null;
    }

    public static void fillInPalette(FloatBuffer vertices, int start, int end, int palette) {
        for (int i = start + 4; i < end; i+=5) {
            vertices.put(i, palette);
        }
    }

    static boolean[] skipArray = new boolean[6];
    static int skipCount;
    public static boolean generateBox(FloatBuffer vertices, int x, int y, int z, Grid grid) {
        TinyGrid cont = grid.getTinyGrid(x, y, z);
        if (cont == null)
            return false;

        TinyGrid gCont;
        for(int i = 0; i < skipArray.length; i++) {
            skipArray[i] = false;
        }
        skipCount = 0;

        if (cont.minX == 0) {
            gCont = getNeighbor(grid, x, y, z, -1, 0, 0);
            if (gCont != null) {
                if (gCont.fullSides[1]) {
                    skipArray[0] = true;
                    skipCount++;
                }
            }
        }

        if (cont.maxX == Config.TINY_GRID_SIZE) {
            gCont = getNeighbor(grid, x, y, z, +1, 0, 0);
            if (gCont != null) {
                if (gCont.fullSides[0]) {
                    skipArray[1] = true;
                    skipCount++;
                }
            }
        }

        if (cont.minY == 0) {
            gCont = getNeighbor(grid, x, y, z, 0, -1, 0);
            if (gCont != null) {
                if (gCont.fullSides[3]) {
                    skipArray[2] = true;
                    skipCount++;
                }
            }
        }

        if (cont.maxY == Config.TINY_GRID_SIZE) {
            gCont = getNeighbor(grid, x, y, z, 0, +1, 0);
            if (gCont != null) {
                if (gCont.fullSides[2]) {
                    skipArray[3] = true;
                    skipCount++;
                }
            }
        }

        if (cont.minZ == 0) {
            gCont = getNeighbor(grid, x, y, z, 0, 0, -1);
            if (gCont != null) {
                if (gCont.fullSides[5]) {
                    skipArray[4] = true;
                    skipCount++;
                }
            }
        }

        if (cont.maxZ == Config.TINY_GRID_SIZE) {
            gCont = getNeighbor(grid, x, y, z, 0, 0, +1);
            if (gCont != null) {
                if (gCont.fullSides[4]) {
                    skipArray[5] = true;
                    skipCount++;
                }
            }
        }

        FloatBuffer write = vertices;

        float divide = (float)(Config.TINY_GRID_SIZE);

        float minXf = (float)cont.minX / divide;
        float minYf = (float)cont.minY / divide;
        float minZf = (float)cont.minZ / divide;

        minXf += low;
        minYf += low;
        minZf += low;

        float maxXf = (float)cont.maxX / divide;
        float maxYf = (float)cont.maxY / divide;
        float maxZf = (float)cont.maxZ / divide;

        maxXf -= low;
        maxYf -= low;
        maxZf -= low;

        minXf += x;
        minYf += y;
        minZf += z;
        maxXf += x;
        maxYf += y;
        maxZf += z;

        boolean hasWritten = false;

        if (skipCount != 6) {
            int side;

            for (int i = 0; i < CUBE.length; i += 3) {
                side = i / (3 * 6);
                if (skipArray[side])
                    continue;

                if (CUBE[i])
                    write.put(maxXf);
                else
                    write.put(minXf);

                if (CUBE[i + 1])
                    write.put(maxYf);
                else
                    write.put(minYf);

                if (CUBE[i + 2])
                    write.put(maxZf);
                else
                    write.put(minZf);

                if (side < 2) { // x
                    write.put(.5f);
                } else if (side < 4) { // y
                    write.put(1f);
                } else { // z
                    write.put(1.5f);
                }

                // palette is filled in later
                write.put(0f);

                hasWritten = true;
            }
        }

        return hasWritten;
    }
}




Java Source Code List

com.badlogic.gdx.backends.gwt.GwtApplicationConfiguration.java
com.badlogic.gdx.backends.gwt.GwtApplication.java
com.badlogic.gdx.backends.gwt.GwtGL20.java
com.badlogic.gdx.backends.gwt.GwtInput.java
com.badlogic.gdx.backends.gwt.GwtNet.java
com.badlogic.gdx.graphics.Pixmap.java
com.toet.TinyVoxel.Config.java
com.toet.TinyVoxel.Game.java
com.toet.TinyVoxel.IOSConfig.java
com.toet.TinyVoxel.IOSLauncher.java
com.toet.TinyVoxel.OuyaController.java
com.toet.TinyVoxel.Time.java
com.toet.TinyVoxel.Character.Character.java
com.toet.TinyVoxel.Debug.LogHandler.java
com.toet.TinyVoxel.GameControllers.CharacterController.java
com.toet.TinyVoxel.GameControllers.CustomTouchPad.java
com.toet.TinyVoxel.GameControllers.KeyBoardController.java
com.toet.TinyVoxel.GameControllers.TouchPadController.java
com.toet.TinyVoxel.Importer.BinvoxImporter.java
com.toet.TinyVoxel.Importer.DataInputStream.java
com.toet.TinyVoxel.Importer.MeshImporter.java
com.toet.TinyVoxel.Renderer.BlockBuilder.java
com.toet.TinyVoxel.Renderer.Floor.java
com.toet.TinyVoxel.Renderer.Manager.java
com.toet.TinyVoxel.Renderer.Bundles.ArrayBundle.java
com.toet.TinyVoxel.Renderer.Bundles.Bundle.java
com.toet.TinyVoxel.Renderer.Bundles.GridBundle.java
com.toet.TinyVoxel.Renderer.Bundles.GridInterface.java
com.toet.TinyVoxel.Renderer.Bundles.Grid.java
com.toet.TinyVoxel.Renderer.Bundles.GroundBundle.java
com.toet.TinyVoxel.Renderer.Bundles.SingleBundle.java
com.toet.TinyVoxel.Renderer.Bundles.TinyGrid.java
com.toet.TinyVoxel.Renderer.Tools.BrushUtils.java
com.toet.TinyVoxel.Renderer.Tools.GridUtils.java
com.toet.TinyVoxel.Renderer.Wrapped.WrappedBoolean.java
com.toet.TinyVoxel.Renderer.Wrapped.WrappedInteger.java
com.toet.TinyVoxel.Screens.GUI.java
com.toet.TinyVoxel.Screens.Menu.java
com.toet.TinyVoxel.Shaders.ShaderManager.java
com.toet.TinyVoxel.Shadow.ShadowManager.java
com.toet.TinyVoxel.Util.Box.java
com.toet.TinyVoxel.Util.FullscreenQuad.java
com.toet.TinyVoxel.Util.JobManager.java
com.toet.TinyVoxel.Util.NonBackedTexture.java
com.toet.TinyVoxel.Util.Position.java
com.toet.TinyVoxel.Util.RLEInputStream.java
com.toet.TinyVoxel.Util.RLEOutputStream.java
com.toet.TinyVoxel.Util.SimpleMath.java
com.toet.TinyVoxel.Util.StreamUtil.java
com.toet.TinyVoxel.android.AndroidConfig.java
com.toet.TinyVoxel.android.AndroidConfig.java
com.toet.TinyVoxel.android.AndroidLauncher.java
com.toet.TinyVoxel.android.AndroidLauncher.java
com.toet.TinyVoxel.client.GwtConfig.java
com.toet.TinyVoxel.client.HtmlLauncher.java
com.toet.TinyVoxel.desktop.DesktopConfig.java
com.toet.TinyVoxel.desktop.DesktopLauncher.java