I’m playing around with a voxel-based world, but instead of simply rendering a huge amount of boxes i want to build a new mesh from a group/chunk of voxels/boxes.
I’m having some problems with that though as i can’t seem to get the vertexes right.
The code is Scala, not Java (sorry) and i’ve tried to clean out as much unnecessary parts as possible.
[java]
// visibleFaces = current voxels visible sides (if any)
// x,y,z = current voxels coordinates
// Voxel.size = the size of a single voxel/cube (currently 0.75u)
// addVertexes & addIndexes adds points and indexes to lists thats later used for setBuffer in a Mesh
visibleFaces.foreach(face => {
vertexMesh.addVertexes(Face.buildNormalFaceVertextes(face).map(point => point.add(x, y, z).mult(Voxel.size)) : _)
vertexMesh.addIndexes(Array(0,1,2, 2,3,0).map(index => index + vertexMesh.vertices.length) : _)
})
//--------------------------------------
def buildNormalFaceVertextes(faceDirection: Vector3f): Array[Vector3f] = {
(faceDirection match {
case Top => Array(0,1,0, 0,1,1, 1,1,1, 1,1,0)
case Bottom => Array(0,0,0, 0,0,1, 1,0,1, 1,0,0)
case Right => Array(1,0,1, 1,0,0, 1,1,0, 1,1,1)
case Left => Array(0,0,1, 0,0,0, 0,1,0, 0,1,1)
case Front => Array(0,1,1, 0,0,1, 1,0,1, 1,1,1)
case Back => Array(0,1,0, 0,0,0, 1,0,0, 1,1,0)
}).sliding(3).map(coords => new Vector3f(coords(0), coords(1), coords(2))).toArray
}
//-----------------------------------
// In a class that extends Mesh:
def buildMesh() {
setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices : _))
setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(textureCoords : _))
setBuffer(Type.Index, 3, BufferUtils.createIntBuffer(indexes : _*))
updateBound()
}
[/java]
For those of you not familiar with Scala, : _* “flattens” an array/list into a list of parameters for a method.