Generating an indexed voxel chunk mesh

Started by moderator_man, August 27, 2022, 19:27:29

Previous topic - Next topic

moderator_man

My goal is to generate a Minecraft-like voxel chunk mesh & render it. The geometry is incorrect, but I'm not quite sure why.

Here is my mesh generation code:

private void generateMeshData(Vector3f pos)
{
    Block block = Block.getBlock(getBlockAt(pos));
    int vertexIndex = 0;
    LinkedHashMap<Vertex, Integer> vertex2index = new LinkedHashMap<>();
    ArrayList<Integer> indices = new ArrayList<>();

    for (int side = 0; side < 6; side++)
    {
        if (!shouldRenderVoxel(pos.add(VoxelData.faceChecks[side]), block.isFluid()))
        {
            int x0 = Math.round(pos.x);
            int x1 = Math.round(pos.x);
            int z0 = Math.round(pos.z);
            int z1 = Math.round(pos.z);
            int xx = x1 - x0 + 1, zz = z1 - z0 + 1, yy = 1;
            int red = 25, green = 25, blue = 25;

            float[] vertices = AABB.SIDE.values[side].translate_and_expand(pos.x + x_offset, pos.y + y_offset, pos.z + z_offset, xx, yy, zz);
            float[] textures = Texture.calcAtlasCoords(block.getTextureIndex(side), 16);

            Integer index;

            Vertex vertex0 = new Vertex(vertices[0], vertices[1], vertices[2], textures[0], textures[1], textures[2], textures[3], red, green, blue);
            index = vertex2index.get(vertex0);
            if (index == null)
            {
                index = vertexIndex++;
                vertex2index.put(vertex0, index);
            }
            indices.add(index);
            indices.add(vertex2index.get(vertex0));

            Vertex vertex1 = new Vertex(vertices[3], vertices[4], vertices[5], textures[0], textures[1], textures[2], textures[3], red, green, blue);
            index = vertex2index.get(vertex1);
            if (index == null)
            {
                index = vertexIndex++;
                vertex2index.put(vertex1, index);
            }
            indices.add(index);
            indices.add(vertex2index.get(vertex1));

            Vertex vertex2 = new Vertex(vertices[6], vertices[7], vertices[8], textures[0], textures[1], textures[2], textures[3], red, green, blue);
            index = vertex2index.get(vertex2);
            if (index == null)
            {
                index = vertexIndex++;
                vertex2index.put(vertex2, index);
            }
            indices.add(index);
            indices.add(vertex2index.get(vertex2));

            Vertex vertex3 = new Vertex(vertices[9], vertices[10], vertices[11], textures[0], textures[1], textures[2], textures[3], red, green, blue);
            index = vertex2index.get(vertex3);
            if (index == null)
            {
                index = vertexIndex++;
                vertex2index.put(vertex3, index);
            }
            indices.add(index);
            indices.add(vertex2index.get(vertex3));
        }
    }

    mesh.update_gl_data(vertex2index.keySet(), indices);
}


And here is how I'm updating it:

public void update_gl_data(Set<Vertex> vertices, List<Integer> indices) {
	this.indices_counter = indices.size();
	this.ibo_data = BufferUtils.createByteBuffer(indices_counter * 4);
	this.vbo_data = BufferUtils.createByteBuffer(vertices.size() * 40);
	for (int index : indices) {
		ibo_data.putInt(index);
	}
	ibo_data.flip();
	
	for (Vertex vertex : vertices) {
		vbo_data.putFloat(vertex.pos_x).putFloat(vertex.pos_y).putFloat(vertex.pos_z);
		vbo_data.putFloat(vertex.tex_coord_x).putFloat(vertex.tex_coord_y);
		vbo_data.putFloat(vertex.tex_coord_offset_x).putFloat(vertex.tex_coord_offset_y);
		vbo_data.putFloat(vertex.color_r).putFloat(vertex.color_g).putFloat(vertex.color_b);
	}
	vbo_data.flip();
}

Lightbuffer

I'm not entirely sure what is exactly the issue, but it's possible that I could give you some insight:
1. Where you have x0 and x1, and z0 and z1 would yield the same value respectively, while xx and zz would yield always 1. Could it possible a copy-paste typo or unused variables?
2. Have you tried rendering this by disabling cull facing (i.e. GL11.glDisable(GL11.GL_CULL_FACE))?
3. Does VAO's vertex structure matches how you filled the data (i.e. 3 floats vectors, 2 floats UV, 2 floats another UV(?), 3 floats color)?