#I need help solving my face cull attempt in voxel engine
5 messages · Page 1 of 1 (latest)
the code to generate the data for the top face
fn add_top(
vertices: &mut Vec<[f32; 3]>,
indices: &mut Vec<u32>,
normals: &mut Vec<[f32; 3]>,
colors: &mut Vec<[f32; 4]>,
voxel_pos: Vec3,
block_type: &BlockType,
index_offset: u32,
) {
let x = voxel_pos.x;
let y = voxel_pos.y;
let z = voxel_pos.z;
let face_vertices = vec![
[x + 0.0, y + 1.0, z + 1.0], // 0
[x + 0.0, y + 1.0, z + 0.0], // 1 // Top face
[x + 1.0, y + 1.0, z + 0.0], // 2
[x + 1.0, y + 1.0, z + 1.0], // 3
];
vertices.extend(face_vertices);
let face_indices: Vec<u32> = vec![0, 1, 2, 2, 3, 0]
.into_iter()
.map(|i| i + index_offset)
.collect();
indices.extend(face_indices);
let face_normals = vec![[0.0, 1.0, 0.0]; 4];
normals.extend(&face_normals);
let _test_color: [[f32; 4]; 4] = [[1.0, 0.0, 0.0, 1.0]; 4];
let face_colors = vec![block_type.color(); 4];
colors.extend(face_colors);
}
oddly enough when other faces are added as well, there appear to be holes on them. This is what happens when I added the bottom face.
This is the code for adding the bottom
fn add_bottom(
vertices: &mut Vec<[f32; 3]>,
indices: &mut Vec<u32>,
normals: &mut Vec<[f32; 3]>,
colors: &mut Vec<[f32; 4]>,
voxel_pos: Vec3,
block_type: &BlockType,
index_offset: u32,
) {
let x = voxel_pos.x;
let y = voxel_pos.y;
let z = voxel_pos.z;
let face_vertices = vec![
[x + 1.0, y + 0.0, z + 1.0], // 4
[x + 0.0, y + 0.0, z + 1.0], // 5 // Bottom face
[x + 0.0, y + 0.0, z + 0.0], // 6
[x + 1.0, y + 0.0, z + 0.0], // 7
];
vertices.extend(face_vertices);
let face_indices: Vec<u32> = vec![4, 5, 6, 6, 7, 4]
.into_iter()
.map(|i| i + index_offset)
.collect();
indices.extend(face_indices);
let face_normals = vec![[0.0, -1.0, 0.0]; 4];
normals.extend(&face_normals);
let _test_color: [[f32; 4]; 4] = [[1.0, 0.0, 0.0, 1.0]; 4];
let face_colors = vec![block_type.color(); 4];
colors.extend(face_colors);
}
The two functions are virtually the same except from the vertex and indices data. The index offset is incremented by 4 each time a face is added because each face has 4 vertex