From 8b2c7452faeae87ee3c6467fdde3654cb24b39c0 Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 16 Jul 2013 18:35:06 -0400 Subject: [PATCH] add a bounding volume to TileChunkMesh and check this against the frustum when rendering This is to ensure that we still render chunks when they contain tile meshes which may be bigger then the 1x1x1 unit size and could then potentially cause the chunk mesh bounds to be larger then the chunk tile grid bounds. --- .../blarg/gdx/tilemap3d/ChunkVertexGenerator.java | 6 ++---- src/com/blarg/gdx/tilemap3d/TileChunkMesh.java | 15 +++++++++++++++ src/com/blarg/gdx/tilemap3d/TileMapRenderer.java | 11 ++++------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/com/blarg/gdx/tilemap3d/ChunkVertexGenerator.java b/src/com/blarg/gdx/tilemap3d/ChunkVertexGenerator.java index 031b94f..163b6f2 100644 --- a/src/com/blarg/gdx/tilemap3d/ChunkVertexGenerator.java +++ b/src/com/blarg/gdx/tilemap3d/ChunkVertexGenerator.java @@ -145,10 +145,8 @@ public class ChunkVertexGenerator { } } - chunk.mesh.mesh = builder.end(); - chunk.mesh.meshPartSize = chunk.mesh.mesh.getNumVertices(); - chunk.alphaMesh.mesh = alphaBuilder.end(); - chunk.alphaMesh.meshPartSize = chunk.alphaMesh.mesh.getNumVertices(); + chunk.mesh.setMesh(builder.end()); + chunk.alphaMesh.setMesh(alphaBuilder.end()); } private void addMesh(MeshBuilder builder, TileMesh sourceMesh, TileChunk chunk, TileCoord position, Matrix4 transform, Color color, int firstVertex, int numVertices) { diff --git a/src/com/blarg/gdx/tilemap3d/TileChunkMesh.java b/src/com/blarg/gdx/tilemap3d/TileChunkMesh.java index 5d323d4..46d4a4e 100644 --- a/src/com/blarg/gdx/tilemap3d/TileChunkMesh.java +++ b/src/com/blarg/gdx/tilemap3d/TileChunkMesh.java @@ -1,13 +1,17 @@ package com.blarg.gdx.tilemap3d; import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.Mesh; import com.badlogic.gdx.graphics.g3d.Renderable; import com.badlogic.gdx.graphics.g3d.materials.Material; import com.badlogic.gdx.graphics.g3d.materials.TextureAttribute; +import com.badlogic.gdx.math.collision.BoundingBox; import com.badlogic.gdx.utils.Disposable; import com.blarg.gdx.graphics.TextureAtlas; public class TileChunkMesh extends Renderable implements Disposable { + public final BoundingBox bounds; + public TileChunkMesh(TileChunk chunk) { meshPartOffset = 0; meshPartSize = 0; @@ -19,6 +23,17 @@ public class TileChunkMesh extends Renderable implements Disposable { TextureAtlas tileMapAtlas = chunk.tileMap.tileMeshes.atlas; material = new Material(TextureAttribute.createDiffuse(tileMapAtlas.texture)); + + bounds = new BoundingBox(); + } + + public void setMesh(Mesh mesh) { + this.mesh = mesh; + this.meshPartSize = mesh.getNumVertices(); + if (this.meshPartSize > 0) + mesh.calculateBoundingBox(bounds); + else + bounds.clr(); } @Override diff --git a/src/com/blarg/gdx/tilemap3d/TileMapRenderer.java b/src/com/blarg/gdx/tilemap3d/TileMapRenderer.java index fb0c5ca..c48f8fe 100644 --- a/src/com/blarg/gdx/tilemap3d/TileMapRenderer.java +++ b/src/com/blarg/gdx/tilemap3d/TileMapRenderer.java @@ -12,13 +12,10 @@ public class TileMapRenderer { for (int x = 0; x < tileMap.widthInChunks; ++x) { TileChunk chunk = tileMap.getChunk(x, y, z); - if (camera.frustum.boundsInFrustum(chunk.getBounds())) - { - if (chunk.mesh.mesh.getNumVertices() > 0) - modelBatch.render(chunk.mesh); - if (chunk.alphaMesh.mesh.getNumVertices() > 0) - modelBatch.render(chunk.alphaMesh); - } + if (chunk.mesh.mesh.getNumVertices() > 0 && camera.frustum.boundsInFrustum(chunk.mesh.bounds)) + modelBatch.render(chunk.mesh); + if (chunk.alphaMesh.mesh.getNumVertices() > 0 && camera.frustum.boundsInFrustum(chunk.alphaMesh.bounds)) + modelBatch.render(chunk.alphaMesh); } } }