Further simplify Quad::to_geometry

This commit is contained in:
Sijmen 2021-06-02 13:16:14 +02:00
parent 2fb56b9e6f
commit fb9aba8f61
Signed by: vijfhoek
GPG key ID: 82D05C89B28B0DAE
2 changed files with 35 additions and 35 deletions

View file

@ -220,12 +220,12 @@ impl Chunk {
culled: AHashMap<(usize, usize), (BlockType, FaceFlags)>,
queue: &mut VecDeque<(usize, usize)>,
highlighted: Option<&(Vector3<usize>, Vector3<i32>)>,
) -> Vec<(BlockType, Vector3<i32>, Quad)> {
let mut quads: Vec<(BlockType, Vector3<i32>, Quad)> = Vec::new();
) -> Vec<Quad> {
let mut quads: Vec<Quad> = Vec::new();
let mut visited = AHashSet::new();
let hl = highlighted.map(|h| h.0);
while let Some((x, z)) = queue.pop_front() {
let translation = offset + Vector3::unit_y() * y as i32;
let position = offset + Vector3::new(x, y, z).cast().unwrap();
if visited.contains(&(x, z)) {
continue;
@ -236,17 +236,19 @@ impl Chunk {
let mut quad_faces = visible_faces;
if hl == Some(Vector3::new(x, y, z)) {
let mut quad = Quad::new(x as i32, z as i32, 1, 1);
let mut quad = Quad::new(position, 1, 1);
quad.highlighted_normal = highlighted.unwrap().1;
quad.visible_faces = quad_faces;
quads.push((block_type, translation, quad));
quad.block_type = Some(block_type);
quads.push(quad);
continue;
}
if block_type == BlockType::Water {
let mut quad = Quad::new(x as i32, z as i32, 1, 1);
let mut quad = Quad::new(position, 1, 1);
quad.visible_faces = quad_faces;
quads.push((block_type, translation, quad));
quad.block_type = Some(block_type);
quads.push(quad);
continue;
}
@ -296,26 +298,21 @@ impl Chunk {
}
}
let mut quad = Quad::new(x as i32, z as i32, (xmax - x) as i32, (zmax - z) as i32);
let mut quad = Quad::new(position, (xmax - x) as i32, (zmax - z) as i32);
quad.visible_faces = quad_faces;
quads.push((block_type, translation, quad));
quad.block_type = Some(block_type);
quads.push(quad);
}
}
quads
}
fn quads_to_geometry(quads: Vec<(BlockType, Vector3<i32>, Quad)>) -> Geometry<BlockVertex> {
fn quads_to_geometry(quads: Vec<Quad>) -> Geometry<BlockVertex> {
let mut geometry: Geometry<BlockVertex> = Default::default();
for (block_type, translation, quad) in quads {
geometry.append(&mut quad.to_geometry(
translation,
block_type,
geometry.vertices.len() as u16,
));
for quad in quads {
geometry.append(&mut quad.to_geometry(geometry.vertices.len() as u16));
}
geometry
}
@ -324,7 +321,7 @@ impl Chunk {
offset: Vector3<i32>,
highlighted: Option<&(Vector3<usize>, Vector3<i32>)>,
) -> Geometry<BlockVertex> {
let mut quads: Vec<(BlockType, Vector3<i32>, Quad)> = Vec::new();
let mut quads: Vec<Quad> = Vec::new();
for y in 0..CHUNK_SIZE {
let (culled, mut queue) = self.cull_layer(y);

View file

@ -11,22 +11,21 @@ use crate::{
#[derive(Debug)]
pub struct Quad {
pub x: i32,
pub y: i32,
pub position: Vector3<i32>,
pub dx: i32,
pub dy: i32,
pub dz: i32,
pub highlighted_normal: Vector3<i32>,
pub visible_faces: FaceFlags,
pub block_type: Option<BlockType>,
}
impl Quad {
pub fn new(x: i32, y: i32, dx: i32, dy: i32) -> Self {
pub fn new(position: Vector3<i32>, dx: i32, dz: i32) -> Self {
Quad {
x,
y,
position,
dx,
dy,
dz,
/// The normal of the face that was highlighted.
///
@ -35,6 +34,11 @@ impl Quad {
/// Bitmap of the visible faces.
visible_faces: FACE_ALL,
/// The `BlockType` of the blocks the quad describes.
///
/// Used for determining which texture to map to it. When `None`, texture index 0 will be used.
block_type: None,
}
}
@ -42,26 +46,25 @@ impl Quad {
///
/// # Arguments
///
/// * `translation` - How much to translate the quad for when rendering.
/// * `block_type` - The type of the block. Used for determining the texture indices.
/// * `start_index` - Which geometry index to start at.
#[allow(clippy::many_single_char_names)]
#[rustfmt::skip]
pub fn to_geometry(
&self,
translation: Vector3<i32>,
block_type: BlockType,
start_index: u16,
) -> Geometry<BlockVertex> {
let dx = self.dx as f32;
let dz = self.dy as f32;
let dz = self.dz as f32;
let dy = 1.0;
let x = (self.x + translation.x) as f32;
let y = translation.y as f32;
let z = (self.y + translation.z) as f32;
let x = self.position.x as f32;
let y = self.position.y as f32;
let z = self.position.z as f32;
let t = block_type.texture_indices();
let t = match self.block_type {
Some(block_type) => block_type.texture_indices(),
None => (0, 0, 0, 0, 0, 0),
};
let mut current_index = start_index;
let mut vertices = Vec::new();