Skip to content

Commit

Permalink
Minor clean-up + added vertex_count method
Browse files Browse the repository at this point in the history
  • Loading branch information
asny committed Feb 26, 2024
1 parent f1a3bc6 commit d39df68
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/renderer/geometry/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,22 @@ impl Mesh {
self.animation = Some(Box::new(animation));
}

///
/// Returns the number of vertices in this mesh.
///
pub fn vertex_count(&self) -> u32 {
self.base_mesh.positions.vertex_count()
}

/// Updates the vertex positions of the mesh.
///
/// # Panics
///
/// Panics if the number of positions does not match the number of vertices in the mesh.
pub fn update_positions(&mut self, positions: &[Vector3<f32>]) {
assert_eq!(
positions.len() as u32,
self.base_mesh.positions.vertex_count()
);
if positions.len() as u32 != self.vertex_count() {
panic!("Failed updating positions: The number of positions {} does not match the number of vertices {} in the mesh.", positions.len(), self.vertex_count())
}
self.base_mesh.positions.fill(positions);
}

Expand All @@ -98,14 +104,14 @@ impl Mesh {
///
/// Panics if the number of normals does not match the number of vertices in the mesh.
pub fn update_normals(&mut self, normals: &[Vector3<f32>]) {
assert_eq!(
normals.len() as u32,
self.base_mesh.positions.vertex_count()
);
if self.base_mesh.normals.is_none() {
self.base_mesh.normals = Some(VertexBuffer::new_with_data(&self.context, normals));
if normals.len() as u32 != self.vertex_count() {
panic!("Failed updating normals: The number of normals {} does not match the number of vertices {} in the mesh.", normals.len(), self.vertex_count())
}

if let Some(normal_buffer) = &mut self.base_mesh.normals {
normal_buffer.fill(normals);
} else {
self.base_mesh.normals.as_mut().unwrap().fill(normals);
self.base_mesh.normals = Some(VertexBuffer::new_with_data(&self.context, normals));
}
}
}
Expand Down

0 comments on commit d39df68

Please sign in to comment.