From 88d784f673282f3eb051aaefe34841cb7f50b720 Mon Sep 17 00:00:00 2001 From: Arkadiusz Buras Date: Sat, 15 Nov 2014 15:14:37 +0100 Subject: [PATCH] Preparing for creating transparent meshes --- .../graphics/builders/TerrainBuilder.java | 47 +++--- .../forge/graphics/mesh/MeshAssembler.java | 4 + .../macbury/forge/systems/TerrainSystem.java | 135 ------------------ .../macbury/forge/terrain/TerrainEngine.java | 14 +- .../forge/editor/windows/MainWindow.java | 3 +- 5 files changed, 47 insertions(+), 156 deletions(-) delete mode 100644 core/src/macbury/forge/systems/TerrainSystem.java diff --git a/core/src/macbury/forge/graphics/builders/TerrainBuilder.java b/core/src/macbury/forge/graphics/builders/TerrainBuilder.java index d56b083..d3677f3 100644 --- a/core/src/macbury/forge/graphics/builders/TerrainBuilder.java +++ b/core/src/macbury/forge/graphics/builders/TerrainBuilder.java @@ -17,13 +17,16 @@ /** * Created by macbury on 16.10.14. */ -public class TerrainBuilder extends VoxelsAssembler { +public class TerrainBuilder { private static final double SHADE_AO_AMPLUTUDE = 10; private static final double SHADE_AO_FREQUENCY = 0.1; private static final float SHADE_AO_FACTOR = 0.35f; + private final VoxelsAssembler solidVoxelAssembler; + private final VoxelsAssembler transparentVoxelAssembler; private float[][][] aoArray; private boolean haveTransparency; + public enum Face { Left(Vector3i.LEFT), Right(Vector3i.RIGHT), Top(Vector3i.TOP), Bottom(Vector3i.BOTTOM), Front(Vector3i.FRONT), Back(Vector3i.BACK); public final Vector3i direction; @@ -49,11 +52,9 @@ public TerrainBuilder(VoxelMap voxelMap) { this.cursor = new TerrainCursor(); this.perlinNoise = new PerlinNoise(System.currentTimeMillis()); this.voxelDef = new VoxelDef(map); - + this.solidVoxelAssembler = new VoxelsAssembler(); + this.transparentVoxelAssembler = new VoxelsAssembler(); generatePrettyShadeArray(); - //perlinNoise.setLacunarity(3); - //perlinNoise.setPersistence(2); - //perlinNoise.setOctaves(9); } @@ -87,33 +88,33 @@ private void buildFace(Vector3i checkTileInDirection, Face face) { voxelDef.voxelPosition.set(x,y,z); voxelDef.size.set(map.voxelSize); - if (map.isEmptyNotOutOfBounds(nextTileToCheck)) { + if (map.isTransparent(nextTileToCheck) || map.isEmptyNotOutOfBounds(nextTileToCheck)) { Block block = map.getBlockForPosition(x,y,z); voxelDef.block = block; voxelDef.calculateAoFor(aoArray[x][y][z], face); switch (face) { case Top: - top(voxelDef); + solidVoxelAssembler.top(voxelDef); break; case Bottom: - bottom(voxelDef); + solidVoxelAssembler.bottom(voxelDef); break; case Front: - front(voxelDef); + solidVoxelAssembler.front(voxelDef); break; case Back: - back(voxelDef); + solidVoxelAssembler.back(voxelDef); break; case Left: - left(voxelDef); + solidVoxelAssembler.left(voxelDef); break; case Right: - right(voxelDef); + solidVoxelAssembler.right(voxelDef); break; } @@ -179,14 +180,19 @@ public void facesForMap() { facesForPart(new Vector3i(0,0,0), new Vector3i(map.getWidth(), map.getHeight(), map.getDepth()), new Vector3()); } - @Override public void begin() { - super.begin(); + solidVoxelAssembler.begin(); + transparentVoxelAssembler.begin(); facesToBuild.clear(); facesToBuild.addAll(Face.values()); haveTransparency = false; } + public void end() { + solidVoxelAssembler.end(); + transparentVoxelAssembler.end(); + } + public boolean next() { return facesToBuild.size > 0; } @@ -196,7 +202,7 @@ public VoxelFaceRenderable buildFaceForChunk(Chunk chunk) { haveTransparency = false; buildFace(face.direction, face); - if (isEmpty()) { + if (solidVoxelAssembler.isEmpty()) { return null; } else { VoxelFaceRenderable renderable = getRenderable(); @@ -216,12 +222,15 @@ private VoxelFaceRenderable getRenderable() { renderable.primitiveType = GL30.GL_TRIANGLES; if (ForgE.config.generateWireframe) - renderable.wireframe = this.wireframe(); - renderable.triangleCount = triangleArrayList.size(); - renderable.mesh = this.mesh(MeshVertexInfo.AttributeType.Position, MeshVertexInfo.AttributeType.Normal, MeshVertexInfo.AttributeType.TextureCord, MeshVertexInfo.AttributeType.Material); + renderable.wireframe = solidVoxelAssembler.wireframe(); + renderable.triangleCount = solidVoxelAssembler.getTriangleCount(); + renderable.mesh = solidVoxelAssembler.mesh(MeshVertexInfo.AttributeType.Position, MeshVertexInfo.AttributeType.Normal, MeshVertexInfo.AttributeType.TextureCord, MeshVertexInfo.AttributeType.Material); return renderable; } - + public void dispose() { + solidVoxelAssembler.dispose(); + transparentVoxelAssembler.dispose(); + } } diff --git a/core/src/macbury/forge/graphics/mesh/MeshAssembler.java b/core/src/macbury/forge/graphics/mesh/MeshAssembler.java index b02b6ac..634419b 100644 --- a/core/src/macbury/forge/graphics/mesh/MeshAssembler.java +++ b/core/src/macbury/forge/graphics/mesh/MeshAssembler.java @@ -166,4 +166,8 @@ public boolean isEmpty() { public boolean haveGeometry() { return !isEmpty(); } + + public int getTriangleCount() { + return this.triangleArrayList.size(); + } } diff --git a/core/src/macbury/forge/systems/TerrainSystem.java b/core/src/macbury/forge/systems/TerrainSystem.java deleted file mode 100644 index 7080927..0000000 --- a/core/src/macbury/forge/systems/TerrainSystem.java +++ /dev/null @@ -1,135 +0,0 @@ -package macbury.forge.systems; - -import com.badlogic.ashley.core.ComponentMapper; -import com.badlogic.ashley.core.Entity; -import com.badlogic.ashley.systems.IntervalSystem; -import com.badlogic.gdx.graphics.GL30; -import com.badlogic.gdx.math.Vector3; -import com.badlogic.gdx.math.collision.BoundingBox; -import com.badlogic.gdx.utils.Array; -import com.badlogic.gdx.utils.Disposable; -import macbury.forge.ForgE; -import macbury.forge.components.Position; -import macbury.forge.components.Renderable; -import macbury.forge.graphics.batch.renderable.BaseRenderable; -import macbury.forge.graphics.batch.renderable.BaseRenderableProvider; -import macbury.forge.graphics.batch.renderable.TerrainChunkRenderable; -import macbury.forge.graphics.builders.Chunk; -import macbury.forge.graphics.builders.TerrainBuilder; -import macbury.forge.graphics.mesh.MeshVertexInfo; -import macbury.forge.voxel.ChunkMap; -import macbury.forge.systems.engine.LevelEntityEngine; - -import java.util.HashMap; - -/** - * Created by macbury on 19.10.14. - */ -public class TerrainSystem extends IntervalSystem implements Disposable, BaseRenderableProvider { - private static final float UPDATE_EVERY = 0.10f;//second - private ComponentMapper rm = ComponentMapper.getFor(Renderable.class); - private ComponentMapper pm = ComponentMapper.getFor(Position.class); - private final LevelEntityEngine engine; - private HashMap tileEntities; - private TerrainBuilder builder; - private ChunkMap map; - private BoundingBox tempBox; - private int cursor = 0; - - public TerrainSystem(LevelEntityEngine engine) { - super(UPDATE_EVERY); - this.engine = engine; - tileEntities = new HashMap(); - tempBox = new BoundingBox(Vector3.Zero, Vector3.Zero); - } - - @Override - protected void updateInterval() { - if (map.chunkToRebuild.size > 0) { - cursor = (int)Math.ceil(map.chunkToRebuild.size * UPDATE_EVERY); - while (map.chunkToRebuild.size > 0) { - Chunk chunk = map.chunkToRebuild.pop(); - buildChunkGeometry(chunk); - cursor--; - if (cursor <= 0) { - break; - } - } - } - } - - private void buildChunkGeometry(Chunk chunk) { - builder.begin(); { - builder.facesForChunk(chunk); - - if (builder.isEmpty()) { - if (tileEntities.containsKey(chunk)) { - removeEntityForChunk(chunk); - tileEntities.remove(chunk); - } - } else { - Entity tileEntity; - Renderable renderableComponent; - Position positionComponent; - if (tileEntities.containsKey(chunk)) { - tileEntity = tileEntities.get(chunk); - renderableComponent = rm.get(tileEntity); - positionComponent = pm.get(tileEntity); - clearMeshForEntity(tileEntity); - } else { - tileEntity = engine.createEntity(); - positionComponent = engine.createComponent(Position.class); - renderableComponent = engine.createComponent(Renderable.class); - renderableComponent.useWorldTransform = false; - renderableComponent.instance = new TerrainChunkRenderable(); - renderableComponent.instance.primitiveType = GL30.GL_TRIANGLES; - tileEntities.put(chunk, tileEntity); - - tileEntity.add(renderableComponent); - tileEntity.add(positionComponent); - engine.addEntity(tileEntity); - } - - if (ForgE.config.generateWireframe) - renderableComponent.instance.wireframe = builder.wireframe(); - renderableComponent.instance.mesh = builder.mesh(MeshVertexInfo.AttributeType.Position, MeshVertexInfo.AttributeType.Normal, MeshVertexInfo.AttributeType.Color); - positionComponent.vector.set(chunk.worldPosition); - positionComponent.size.set(chunk.size); - positionComponent.visible = true; - } - } builder.end(); - } - - public void setMap(ChunkMap map) { - this.map = map; - this.builder = new TerrainBuilder(map); - } - - private void removeEntityForChunk(Chunk chunk) { - Entity tileEntity = tileEntities.get(chunk); - clearMeshForEntity(tileEntity); - engine.removeEntity(tileEntity); - } - - private void clearMeshForEntity(Entity tileEntity) { - Renderable renderableComponent = rm.get(tileEntity); - if (renderableComponent.instance.mesh != null) { - renderableComponent.instance.mesh.dispose(); - } - } - - @Override - public void dispose() { - for (Chunk chunk : tileEntities.keySet()) { - removeEntityForChunk(chunk); - } - map = null; - builder.dispose(); - builder = null; - } - - @Override - public void getRenderables(Array renderables) { - - } -} diff --git a/core/src/macbury/forge/terrain/TerrainEngine.java b/core/src/macbury/forge/terrain/TerrainEngine.java index 16417e2..97d5193 100644 --- a/core/src/macbury/forge/terrain/TerrainEngine.java +++ b/core/src/macbury/forge/terrain/TerrainEngine.java @@ -103,12 +103,24 @@ private void occulsion() { Chunk visibleChunk = (Chunk) tempObjects.pop(); if (visibleChunk.renderables.size > 0) { visibleChunks.add(visibleChunk); - + boolean visibleFront = false; + //TODO: if i can see front face, that means i cant see back face, if i can see right face that means i cant see left face for (int i = 0; i < visibleChunk.renderables.size; i++) { VoxelFaceRenderable renderable = visibleChunk.renderables.get(i); + /*if (renderable.direction.epsilonEquals(TerrainBuilder.Face.Front.direction, 0.1f)) { + visibleFront = true; + } + + if (visibleFront && renderable.direction.equals(TerrainBuilder.Face.Back)) { + + } else { + visibleFaces.add(renderable); + }*/ + if (camera.boundsInFrustum(renderable.boundingBox)) { visibleFaces.add(renderable); } + //visibleFaces.add(renderable); } } } diff --git a/editor/src/macbury/forge/editor/windows/MainWindow.java b/editor/src/macbury/forge/editor/windows/MainWindow.java index e6bbca6..a04657d 100644 --- a/editor/src/macbury/forge/editor/windows/MainWindow.java +++ b/editor/src/macbury/forge/editor/windows/MainWindow.java @@ -149,6 +149,7 @@ public void focusGained(FocusEvent e) { @Override public void focusLost(FocusEvent e) { - //mainContentPane.grabFocus(); + mainContentPane.grabFocus(); + //mainContentPane.setFocusable(true); } }