From 2cc12276f087682043755d59190203e3ce9d3b4c Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 21 Jul 2013 22:33:48 -0400 Subject: [PATCH] little bit of code reorganizing --- .../gdx/tilemap3d/ChunkVertexGenerator.java | 173 +++++++++--------- 1 file changed, 89 insertions(+), 84 deletions(-) diff --git a/src/com/blarg/gdx/tilemap3d/ChunkVertexGenerator.java b/src/com/blarg/gdx/tilemap3d/ChunkVertexGenerator.java index a168883..753fca6 100644 --- a/src/com/blarg/gdx/tilemap3d/ChunkVertexGenerator.java +++ b/src/com/blarg/gdx/tilemap3d/ChunkVertexGenerator.java @@ -58,90 +58,10 @@ public class ChunkVertexGenerator { else tmpColor.set(mesh.color); - if (mesh instanceof CubeTileMesh) { - CubeTileMesh cubeMesh = (CubeTileMesh)mesh; - - // determine what's next to each cube face - Tile left = chunk.getWithinSelfOrNeighbourSafe(x - 1, y, z); - Tile right = chunk.getWithinSelfOrNeighbourSafe(x + 1, y, z); - Tile forward = chunk.getWithinSelfOrNeighbourSafe(x, y, z - 1); - Tile backward = chunk.getWithinSelfOrNeighbourSafe(x, y, z + 1); - Tile down = chunk.getWithinSelfOrNeighbourSafe(x, y - 1, z); - Tile up = chunk.getWithinSelfOrNeighbourSafe(x, y + 1, z); - - // evaluate each face's visibility and add it's vertices if needed one at a time - if ((left == null || left.tile == Tile.NO_TILE || !tileMap.tileMeshes.get(left).isOpaque(TileMesh.SIDE_RIGHT)) && cubeMesh.hasFace(TileMesh.SIDE_LEFT)) { - // left face is visible - if (cubeMesh.alpha) - addMesh(alphaBuilder, tile, cubeMesh, chunk, tmpPosition, transform, tmpColor, cubeMesh.leftFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); - else - addMesh(builder, tile, cubeMesh, chunk, tmpPosition, transform, tmpColor, cubeMesh.leftFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); - } - if ((right == null || right.tile == Tile.NO_TILE || !tileMap.tileMeshes.get(right).isOpaque(TileMesh.SIDE_LEFT)) && cubeMesh.hasFace(TileMesh.SIDE_RIGHT)) { - // right face is visible - if (cubeMesh.alpha) - addMesh(alphaBuilder, tile, cubeMesh, chunk, tmpPosition, transform, tmpColor, cubeMesh.rightFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); - else - addMesh(builder, tile, cubeMesh, chunk, tmpPosition, transform, tmpColor, cubeMesh.rightFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); - } - if ((forward == null || forward.tile == Tile.NO_TILE || !tileMap.tileMeshes.get(forward).isOpaque(TileMesh.SIDE_BACK)) && cubeMesh.hasFace(TileMesh.SIDE_FRONT)) { - // front face is visible - if (cubeMesh.alpha) - addMesh(alphaBuilder, tile, cubeMesh, chunk, tmpPosition, transform, tmpColor, cubeMesh.frontFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); - else - addMesh(builder, tile, cubeMesh, chunk, tmpPosition, transform, tmpColor, cubeMesh.frontFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); - } - if ((backward == null || backward.tile == Tile.NO_TILE || !tileMap.tileMeshes.get(backward).isOpaque(TileMesh.SIDE_FRONT)) && cubeMesh.hasFace(TileMesh.SIDE_BACK)) { - // back face is visible - if (cubeMesh.alpha) - addMesh(alphaBuilder, tile, cubeMesh, chunk, tmpPosition, transform, tmpColor, cubeMesh.backFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); - else - addMesh(builder, tile, cubeMesh, chunk, tmpPosition, transform, tmpColor, cubeMesh.backFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); - } - if ((down == null || down.tile == Tile.NO_TILE || !tileMap.tileMeshes.get(down).isOpaque(TileMesh.SIDE_TOP)) && cubeMesh.hasFace(TileMesh.SIDE_BOTTOM)) { - // bottom face is visible - if (cubeMesh.alpha) - addMesh(alphaBuilder, tile, cubeMesh, chunk, tmpPosition, transform, tmpColor, cubeMesh.bottomFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); - else - addMesh(builder, tile, cubeMesh, chunk, tmpPosition, transform, tmpColor, cubeMesh.bottomFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); - } - if ((up == null || up.tile == Tile.NO_TILE || !tileMap.tileMeshes.get(up).isOpaque(TileMesh.SIDE_BOTTOM)) && cubeMesh.hasFace(TileMesh.SIDE_TOP)) { - // top face is visible - if (cubeMesh.alpha) - addMesh(alphaBuilder, tile, cubeMesh, chunk, tmpPosition, transform, tmpColor, cubeMesh.topFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); - else - addMesh(builder, tile, cubeMesh, chunk, tmpPosition, transform, tmpColor, cubeMesh.topFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); - } - } else { - boolean visible = false; - - // visibility determination. we check for at least one - // adjacent empty space / non-opaque tile - Tile left = chunk.getWithinSelfOrNeighbourSafe(x - 1, y, z); - Tile right = chunk.getWithinSelfOrNeighbourSafe(x + 1, y, z); - Tile forward = chunk.getWithinSelfOrNeighbourSafe(x, y, z - 1); - Tile backward = chunk.getWithinSelfOrNeighbourSafe(x, y, z + 1); - Tile down = chunk.getWithinSelfOrNeighbourSafe(x, y - 1, z); - Tile up = chunk.getWithinSelfOrNeighbourSafe(x, y + 1, z); - - // null == empty space (off the edge of the entire map) - if ( - (left == null || left.isEmptySpace() || !tileMap.tileMeshes.get(left).isOpaque(TileMesh.SIDE_RIGHT)) || - (right == null || right.isEmptySpace() || !tileMap.tileMeshes.get(right).isOpaque(TileMesh.SIDE_LEFT)) || - (forward == null || forward.isEmptySpace() || !tileMap.tileMeshes.get(forward).isOpaque(TileMesh.SIDE_BACK)) || - (backward == null || backward.isEmptySpace() || !tileMap.tileMeshes.get(backward).isOpaque(TileMesh.SIDE_FRONT)) || - (up == null || up.isEmptySpace() || !tileMap.tileMeshes.get(up).isOpaque(TileMesh.SIDE_BOTTOM)) || - (down == null || down.isEmptySpace() || !tileMap.tileMeshes.get(down).isOpaque(TileMesh.SIDE_TOP)) - ) - visible = true; - - if (visible) { - if (mesh.alpha) - addMesh(alphaBuilder, tile, mesh, chunk, tmpPosition, transform, tmpColor, 0, mesh.getVertices().count()); - else - addMesh(builder, tile, mesh, chunk, tmpPosition, transform, tmpColor, 0, mesh.getVertices().count()); - } - } + if (mesh instanceof CubeTileMesh) + handleCubeMesh(x, y, z, tile, chunk, (CubeTileMesh)mesh, tmpPosition, transform, tmpColor); + else + handleGenericMesh(x, y, z, tile, chunk, mesh, tmpPosition, transform, tmpColor); } } } @@ -150,6 +70,91 @@ public class ChunkVertexGenerator { chunk.alphaMesh.setMesh(alphaBuilder.end()); } + private void handleCubeMesh(int x, int y, int z, Tile tile, TileChunk chunk, CubeTileMesh mesh, TileCoord tileMapPosition, Matrix4 transform, Color color) { + // determine what's next to each cube face + Tile left = chunk.getWithinSelfOrNeighbourSafe(x - 1, y, z); + Tile right = chunk.getWithinSelfOrNeighbourSafe(x + 1, y, z); + Tile forward = chunk.getWithinSelfOrNeighbourSafe(x, y, z - 1); + Tile backward = chunk.getWithinSelfOrNeighbourSafe(x, y, z + 1); + Tile down = chunk.getWithinSelfOrNeighbourSafe(x, y - 1, z); + Tile up = chunk.getWithinSelfOrNeighbourSafe(x, y + 1, z); + + // evaluate each face's visibility and add it's vertices if needed one at a time + if ((left == null || left.tile == Tile.NO_TILE || !chunk.tileMap.tileMeshes.get(left).isOpaque(TileMesh.SIDE_RIGHT)) && mesh.hasFace(TileMesh.SIDE_LEFT)) { + // left face is visible + if (mesh.alpha) + addMesh(alphaBuilder, tile, mesh, chunk, tileMapPosition, transform, color, mesh.leftFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); + else + addMesh(builder, tile, mesh, chunk, tileMapPosition, transform, color, mesh.leftFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); + } + if ((right == null || right.tile == Tile.NO_TILE || !chunk.tileMap.tileMeshes.get(right).isOpaque(TileMesh.SIDE_LEFT)) && mesh.hasFace(TileMesh.SIDE_RIGHT)) { + // right face is visible + if (mesh.alpha) + addMesh(alphaBuilder, tile, mesh, chunk, tileMapPosition, transform, color, mesh.rightFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); + else + addMesh(builder, tile, mesh, chunk, tileMapPosition, transform, color, mesh.rightFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); + } + if ((forward == null || forward.tile == Tile.NO_TILE || !chunk.tileMap.tileMeshes.get(forward).isOpaque(TileMesh.SIDE_BACK)) && mesh.hasFace(TileMesh.SIDE_FRONT)) { + // front face is visible + if (mesh.alpha) + addMesh(alphaBuilder, tile, mesh, chunk, tileMapPosition, transform, color, mesh.frontFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); + else + addMesh(builder, tile, mesh, chunk, tileMapPosition, transform, color, mesh.frontFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); + } + if ((backward == null || backward.tile == Tile.NO_TILE || !chunk.tileMap.tileMeshes.get(backward).isOpaque(TileMesh.SIDE_FRONT)) && mesh.hasFace(TileMesh.SIDE_BACK)) { + // back face is visible + if (mesh.alpha) + addMesh(alphaBuilder, tile, mesh, chunk, tileMapPosition, transform, color, mesh.backFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); + else + addMesh(builder, tile, mesh, chunk, tileMapPosition, transform, color, mesh.backFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); + } + if ((down == null || down.tile == Tile.NO_TILE || !chunk.tileMap.tileMeshes.get(down).isOpaque(TileMesh.SIDE_TOP)) && mesh.hasFace(TileMesh.SIDE_BOTTOM)) { + // bottom face is visible + if (mesh.alpha) + addMesh(alphaBuilder, tile, mesh, chunk, tileMapPosition, transform, color, mesh.bottomFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); + else + addMesh(builder, tile, mesh, chunk, tileMapPosition, transform, color, mesh.bottomFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); + } + if ((up == null || up.tile == Tile.NO_TILE || !chunk.tileMap.tileMeshes.get(up).isOpaque(TileMesh.SIDE_BOTTOM)) && mesh.hasFace(TileMesh.SIDE_TOP)) { + // top face is visible + if (mesh.alpha) + addMesh(alphaBuilder, tile, mesh, chunk, tileMapPosition, transform, color, mesh.topFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); + else + addMesh(builder, tile, mesh, chunk, tileMapPosition, transform, color, mesh.topFaceVertexOffset, TileMesh.CUBE_VERTICES_PER_FACE); + } + } + + private void handleGenericMesh(int x, int y, int z, Tile tile, TileChunk chunk, TileMesh mesh, TileCoord tileMapPosition, Matrix4 transform, Color color) { + boolean visible = false; + + // visibility determination. we check for at least one + // adjacent empty space / non-opaque tile + Tile left = chunk.getWithinSelfOrNeighbourSafe(x - 1, y, z); + Tile right = chunk.getWithinSelfOrNeighbourSafe(x + 1, y, z); + Tile forward = chunk.getWithinSelfOrNeighbourSafe(x, y, z - 1); + Tile backward = chunk.getWithinSelfOrNeighbourSafe(x, y, z + 1); + Tile down = chunk.getWithinSelfOrNeighbourSafe(x, y - 1, z); + Tile up = chunk.getWithinSelfOrNeighbourSafe(x, y + 1, z); + + // null == empty space (off the edge of the entire map) + if ( + (left == null || left.isEmptySpace() || !chunk.tileMap.tileMeshes.get(left).isOpaque(TileMesh.SIDE_RIGHT)) || + (right == null || right.isEmptySpace() || !chunk.tileMap.tileMeshes.get(right).isOpaque(TileMesh.SIDE_LEFT)) || + (forward == null || forward.isEmptySpace() || !chunk.tileMap.tileMeshes.get(forward).isOpaque(TileMesh.SIDE_BACK)) || + (backward == null || backward.isEmptySpace() || !chunk.tileMap.tileMeshes.get(backward).isOpaque(TileMesh.SIDE_FRONT)) || + (up == null || up.isEmptySpace() || !chunk.tileMap.tileMeshes.get(up).isOpaque(TileMesh.SIDE_BOTTOM)) || + (down == null || down.isEmptySpace() || !chunk.tileMap.tileMeshes.get(down).isOpaque(TileMesh.SIDE_TOP)) + ) + visible = true; + + if (visible) { + if (mesh.alpha) + addMesh(alphaBuilder, tile, mesh, chunk, tileMapPosition, transform, color, 0, mesh.getVertices().count()); + else + addMesh(builder, tile, mesh, chunk, tileMapPosition, transform, color, 0, mesh.getVertices().count()); + } + } + protected void addMesh(MeshBuilder builder, Tile tile, TileMesh sourceMesh, TileChunk chunk, TileCoord position, Matrix4 transform, Color color, int firstVertex, int numVertices) { // adjust position by the tilemesh offset. TileMesh's are modeled using the // origin (0,0,0) as the center and are 1 unit wide/deep/tall. So, their