I have a segment of code here that’s supposed to cull faces of voxels that should be invisible.
private FloatBuffer voxelData(int x, int y, int z) {
float r = rand.nextFloat(), g = rand.nextFloat(), b = rand.nextFloat();
float xscale = x * scale, yscale = y * scale, zscale = z * scale;
int numfaces = 6;
boolean top = true, bottom = false, left = true, right = true, front = true, back = true;
if (x != width-- && voxels[x + 1][y][z].isActive) {right = false; numfaces--;}
if (x != 0 && voxels[x - 1][y][z].isActive) {left = false; numfaces--;}
if (y != height-- && voxels[x][y + 1][z].isActive) {top = false; numfaces--;}
if (y != 0 && voxels[x][y - 1][z].isActive) {bottom = false; numfaces--;}
if (z != length-- && voxels[x][y][z + 1].isActive) {front = false; numfaces--;}
if (z != 0 && voxels[x][y][z - 1].isActive) {back = false; numfaces--;}
if (numfaces == 0) return null;
FloatBuffer data = BufferUtils.createFloatBuffer(numfaces * 24);
if (top) { data.put(new float[] {
this.x + xscale + 0.5f * scale, this.y + yscale + 0.5f * scale, this.z + zscale - 0.5f * scale, r, g, b,
this.x + xscale - 0.5f * scale, this.y + yscale + 0.5f * scale, this.z + zscale - 0.5f * scale, r, g, b,
this.x + xscale - 0.5f * scale, this.y + yscale + 0.5f * scale, this.z + zscale + 0.5f * scale, r, g, b,
this.x + xscale + 0.5f * scale, this.y + yscale + 0.5f * scale, this.z + zscale + 0.5f * scale, r, g, b,
});}
if (bottom) { data.put(new float[] {
this.x + xscale - 0.5f * scale, this.y + yscale - 0.5f * scale, this.z + zscale - 0.5f * scale, r, g, b,
this.x + xscale + 0.5f * scale, this.y + yscale - 0.5f * scale, this.z + zscale - 0.5f * scale, r, g, b,
this.x + xscale + 0.5f * scale, this.y + yscale - 0.5f * scale, this.z + zscale + 0.5f * scale, r, g, b,
this.x + xscale - 0.5f * scale, this.y + yscale - 0.5f * scale, this.z + zscale + 0.5f * scale, r, g, b,
});}
if (left) { data.put(new float[] {
this.x + xscale - 0.5f * scale, this.y + yscale + 0.5f * scale, this.z + zscale + 0.5f * scale, r, g, b,
this.x + xscale - 0.5f * scale, this.y + yscale + 0.5f * scale, this.z + zscale - 0.5f * scale, r, g, b,
this.x + xscale - 0.5f * scale, this.y + yscale - 0.5f * scale, this.z + zscale - 0.5f * scale, r, g, b,
this.x + xscale - 0.5f * scale, this.y + yscale - 0.5f * scale, this.z + zscale + 0.5f * scale, r, g, b,
});}
if (right) { data.put(new float[] {
this.x + xscale + 0.5f * scale, this.y + yscale - 0.5f * scale, this.z + zscale + 0.5f * scale, r, g, b,
this.x + xscale + 0.5f * scale, this.y + yscale - 0.5f * scale, this.z + zscale - 0.5f * scale, r, g, b,
this.x + xscale + 0.5f * scale, this.y + yscale + 0.5f * scale, this.z + zscale - 0.5f * scale, r, g, b,
this.x + xscale + 0.5f * scale, this.y + yscale + 0.5f * scale, this.z + zscale + 0.5f * scale, r, g, b,
});}
if (front) { data.put(new float[] {
this.x + xscale + 0.5f * scale, this.y + yscale + 0.5f * scale, this.z + zscale + 0.5f * scale, r, g, b,
this.x + xscale - 0.5f * scale, this.y + yscale + 0.5f * scale, this.z + zscale + 0.5f * scale, r, g, b,
this.x + xscale - 0.5f * scale, this.y + yscale - 0.5f * scale, this.z + zscale + 0.5f * scale, r, g, b,
this.x + xscale + 0.5f * scale, this.y + yscale - 0.5f * scale, this.z + zscale + 0.5f * scale, r, g, b,
});}
if (back) { data.put(new float[] {
this.x + xscale + 0.5f * scale, this.y + yscale - 0.5f * scale, this.z + zscale - 0.5f * scale, r, g, b,
this.x + xscale - 0.5f * scale, this.y + yscale - 0.5f * scale, this.z + zscale - 0.5f * scale, r, g, b,
this.x + xscale - 0.5f * scale, this.y + yscale + 0.5f * scale, this.z + zscale - 0.5f * scale, r, g, b,
this.x + xscale + 0.5f * scale, this.y + yscale + 0.5f * scale, this.z + zscale - 0.5f * scale, r, g, b,
});}
data.flip();
return data;
}
The segment is passed the x, y, and z values through a triple-nested for-loop checking each voxel in a 3d array. However, the code seems to only output left and back sides; never any others. Even then, it’s only the left-bottom-back corner that renders, and not even the bottom face; just the back, and three left faces from the edge, and a face one above that.
Here’s a screenshot: