split up chunk VBO generation into two parts for future async changes

This commit is contained in:
Gered 2014-03-16 10:17:23 -04:00
parent 73e81949ab
commit 3a2bfb8cdc

View file

@ -27,7 +27,16 @@ public class ChunkVertexGenerator {
final Color tmpColor = new Color(); final Color tmpColor = new Color();
final Vector3 tmpOffset = new Vector3(); final Vector3 tmpOffset = new Vector3();
public GeneratedChunkMeshes generate(TileChunk chunk) { boolean areVerticesGenerated;
// for async generation of TileMap VBO's... generateVertices() can be called on another thread, but
// createMeshFromVertices() must be called on the render thread (the OpenGL context's thread) because
// it is creating VBOs. thus, why these two methods are now split up from how they were before.
public void generateVertices(TileChunk chunk) {
if (areVerticesGenerated)
throw new IllegalStateException("Previously generated vertices have not yet been turned into meshes.");
TileMap tileMap = chunk.tileMap; TileMap tileMap = chunk.tileMap;
builder.begin( builder.begin(
@ -79,12 +88,29 @@ public class ChunkVertexGenerator {
} }
} }
areVerticesGenerated = true;
}
public GeneratedChunkMeshes createMeshFromVertices() {
if (!areVerticesGenerated)
throw new IllegalStateException("Vertices have not yet been generated. Cannot create a mesh.");
GeneratedChunkMeshes output = new GeneratedChunkMeshes(); GeneratedChunkMeshes output = new GeneratedChunkMeshes();
output.opaqueMesh = builder.end(); output.opaqueMesh = builder.end();
output.alphaMesh = alphaBuilder.end(); output.alphaMesh = alphaBuilder.end();
areVerticesGenerated = false;
return output; return output;
} }
// if you're not doing async map generation, then you can (and probably should) still call this as before
public GeneratedChunkMeshes generate(TileChunk chunk) {
generateVertices(chunk);
return createMeshFromVertices();
}
private void handleCubeMesh(int x, int y, int z, Tile tile, TileChunk chunk, CubeTileMesh mesh, TileCoord tileMapPosition, Matrix4 transform, Color color) { 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 // determine what's next to each cube face
Tile left = chunk.getWithinSelfOrNeighbourSafe(x - 1, y, z); Tile left = chunk.getWithinSelfOrNeighbourSafe(x - 1, y, z);