Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions crates/lambda-rs/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl MeshBuilder {

/// Allocates memory for the given number of vertices and fills
/// the mesh with empty vertices.
pub fn with_capacity(&mut self, size: usize) -> &mut Self {
pub fn with_capacity(mut self, size: usize) -> Self {
self.vertices.resize(
size,
Vertex {
Expand All @@ -77,34 +77,32 @@ impl MeshBuilder {
}

/// Adds a vertex to the mesh.
pub fn with_vertex(&mut self, vertex: Vertex) -> &mut Self {
pub fn with_vertex(mut self, vertex: Vertex) -> Self {
self.vertices.push(vertex);
return self;
}

/// Specify the attributes of the mesh. This is used to map the vertex data to
/// the input of the vertex shader.
pub fn with_attributes(
&mut self,
attributes: Vec<VertexAttribute>,
) -> &mut Self {
pub fn with_attributes(mut self, attributes: Vec<VertexAttribute>) -> Self {
self.attributes = attributes;
return self;
}

/// Builds a mesh from the vertices and indices that have been added to the
/// builder and allocates the memory for the mesh on the GPU.
pub fn build(&self) -> Mesh {
pub fn build(self) -> Mesh {
return Mesh {
vertices: self.vertices.clone(),
attributes: self.attributes.clone(),
vertices: self.vertices,
attributes: self.attributes,
};
}

/// Builds a mesh from the vertices of an OBJ file. The mesh will have the same
/// attributes as the OBJ file and can be allocated on to the GPU with
/// `BufferBuilder::build_from_mesh`.
pub fn build_from_obj(&self, file_path: &str) -> Mesh {
pub fn build_from_obj(self, file_path: &str) -> Mesh {
let _ = self;
let obj = load_textured_obj_from_file(file_path);

let vertices = obj
Expand Down Expand Up @@ -170,17 +168,16 @@ mod tests {
/// the built mesh.
#[test]
fn mesh_builder_capacity_and_attributes_are_applied() {
let mut builder = MeshBuilder::new();
builder.with_capacity(2);
let builder = MeshBuilder::new().with_capacity(2);
assert_eq!(builder.vertices.len(), 2);

builder.with_vertex(Vertex {
position: [1.0, 2.0, 3.0],
normal: [0.0, 1.0, 0.0],
color: [0.5, 0.5, 0.5],
});

let mesh = builder.build();
let mesh = builder
.with_vertex(Vertex {
position: [1.0, 2.0, 3.0],
normal: [0.0, 1.0, 0.0],
color: [0.5, 0.5, 0.5],
})
.build();
assert_eq!(mesh.vertices().len(), 3);
}

Expand Down
12 changes: 6 additions & 6 deletions demos/physics/src/bin/physics_falling_quad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ impl Component<ComponentResult, String> for FallingQuadDemo {
.build(),
];

let mut mesh_builder = MeshBuilder::new();
vertices.iter().for_each(|vertex| {
mesh_builder.with_vertex(*vertex);
});

let mesh = mesh_builder
let mesh = vertices
.iter()
.copied()
.fold(MeshBuilder::new(), |builder, vertex| {
return builder.with_vertex(vertex);
})
.with_attributes(vec![
VertexAttribute {
location: 0,
Expand Down
12 changes: 6 additions & 6 deletions demos/physics/src/bin/physics_rigid_bodies_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,12 @@ impl Component<ComponentResult, String> for RigidBodies2DDemo {
.build(),
];

let mut mesh_builder = MeshBuilder::new();
vertices.iter().for_each(|vertex| {
mesh_builder.with_vertex(*vertex);
});

let mesh = mesh_builder
let mesh = vertices
.iter()
.copied()
.fold(MeshBuilder::new(), |builder, vertex| {
return builder.with_vertex(vertex);
})
.with_attributes(vec![
VertexAttribute {
location: 0,
Expand Down
12 changes: 6 additions & 6 deletions demos/render/src/bin/immediates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ impl Component<ComponentResult, String> for ImmediatesExample {
.build(),
];

let mut mesh_builder = MeshBuilder::new();
vertices.iter().for_each(|vertex| {
mesh_builder.with_vertex(*vertex);
});

let mesh = mesh_builder
let mesh = vertices
.iter()
.copied()
.fold(MeshBuilder::new(), |builder, vertex| {
return builder.with_vertex(vertex);
})
.with_attributes(vec![
VertexAttribute {
location: 0,
Expand Down
11 changes: 5 additions & 6 deletions demos/render/src/bin/offscreen_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,11 @@ impl OffscreenPostExample {
.build(),
];

let mut mesh_builder = MeshBuilder::new();
for v in vertices {
mesh_builder.with_vertex(v);
}

return mesh_builder
return vertices
.into_iter()
.fold(MeshBuilder::new(), |builder, vertex| {
return builder.with_vertex(vertex);
})
.with_attributes(vec![
VertexAttribute {
location: 0,
Expand Down
30 changes: 14 additions & 16 deletions demos/render/src/bin/reflective_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,11 +870,11 @@ fn build_unit_cube_mesh() -> Mesh {
[(h, -h, -h), (-h, -h, -h), (-h, h, -h), (h, h, -h)],
);

let mut mesh_builder = MeshBuilder::new();
for v in verts.into_iter() {
mesh_builder.with_vertex(v);
}
let mesh = mesh_builder
let mesh = verts
.into_iter()
.fold(MeshBuilder::new(), |builder, vertex| {
return builder.with_vertex(vertex);
})
.with_attributes(vec![
VertexAttribute {
location: 0,
Expand Down Expand Up @@ -914,18 +914,16 @@ fn build_floor_quad_mesh(extent: f32) -> Mesh {
let p2 = v(h, h);
let p3 = v(-h, h);

let mut mesh_builder = MeshBuilder::new();
// Tri winding flipped to face +Y (avoid back-face cull)
// Tri 1
mesh_builder.with_vertex(p0);
mesh_builder.with_vertex(p2);
mesh_builder.with_vertex(p1);
// Tri 2
mesh_builder.with_vertex(p0);
mesh_builder.with_vertex(p3);
mesh_builder.with_vertex(p2);

let mesh = mesh_builder
let mesh = MeshBuilder::new()
// Tri 1
.with_vertex(p0)
.with_vertex(p2)
.with_vertex(p1)
// Tri 2
.with_vertex(p0)
.with_vertex(p3)
.with_vertex(p2)
.with_attributes(vec![
VertexAttribute {
location: 0,
Expand Down
10 changes: 5 additions & 5 deletions demos/render/src/bin/textured_cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ impl Component<ComponentResult, String> for TexturedCubeExample {
[(h, -h, -h), (-h, -h, -h), (-h, h, -h), (h, h, -h)],
);

let mut mesh_builder = MeshBuilder::new();
for v in verts.into_iter() {
mesh_builder.with_vertex(v);
}
let mesh = mesh_builder
let mesh = verts
.into_iter()
.fold(MeshBuilder::new(), |builder, vertex| {
return builder.with_vertex(vertex);
})
.with_attributes(vec![
VertexAttribute {
location: 0,
Expand Down
11 changes: 6 additions & 5 deletions demos/render/src/bin/textured_quad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,12 @@ impl Component<ComponentResult, String> for TexturedQuadExample {
.build(), // uv (0,1)
];

let mut mesh_builder = MeshBuilder::new();
vertices.iter().for_each(|v| {
mesh_builder.with_vertex(*v);
});
let mesh = mesh_builder
let mesh = vertices
.iter()
.copied()
.fold(MeshBuilder::new(), |builder, vertex| {
return builder.with_vertex(vertex);
})
.with_attributes(vec![
VertexAttribute {
location: 0,
Expand Down
12 changes: 6 additions & 6 deletions demos/render/src/bin/uniform_buffer_triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ impl Component<ComponentResult, String> for UniformBufferExample {
.build(),
];

let mut mesh_builder = MeshBuilder::new();
vertices.iter().for_each(|vertex| {
mesh_builder.with_vertex(*vertex);
});

let mesh = mesh_builder
let mesh = vertices
.iter()
.copied()
.fold(MeshBuilder::new(), |builder, vertex| {
return builder.with_vertex(vertex);
})
.with_attributes(vec![
VertexAttribute {
location: 0,
Expand Down
10 changes: 4 additions & 6 deletions docs/tutorials/physics/basics/falling-quad-kinematic.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,10 @@ fn on_attach(
.build(),
];

let mut mesh_builder = MeshBuilder::new();
vertices.iter().for_each(|vertex| {
mesh_builder.with_vertex(*vertex);
});

let mesh = mesh_builder
let mesh = vertices
.iter()
.copied()
.fold(MeshBuilder::new(), |builder, vertex| builder.with_vertex(vertex))
.with_attributes(vec![
VertexAttribute {
location: 0,
Expand Down
7 changes: 3 additions & 4 deletions docs/tutorials/rendering/resources/textured-cube.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,9 @@ add_face( 0.0, -1.0, 0.0, [(-h, -h, -h), ( h, -h, -h), ( h, -h, h), (-h, -h,
add_face( 0.0, 0.0, 1.0, [(-h, -h, h), ( h, -h, h), ( h, h, h), (-h, h, h)]);
add_face( 0.0, 0.0, -1.0, [( h, -h, -h), (-h, -h, -h), (-h, h, -h), ( h, h, -h)]);

let mut mesh_builder = MeshBuilder::new();
verts.into_iter().for_each(|v| { mesh_builder.with_vertex(v); });

let mesh: Mesh = mesh_builder
let mesh: Mesh = verts
.into_iter()
.fold(MeshBuilder::new(), |builder, vertex| builder.with_vertex(vertex))
.with_attributes(vec![
VertexAttribute { // position @ location 0
location: 0, offset: 0,
Expand Down
8 changes: 4 additions & 4 deletions docs/tutorials/rendering/resources/textured-quad.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,10 @@ let vertices: [Vertex; 6] = [
VertexBuilder::new().with_position([-0.5, 0.5, 0.0]).with_normal([0.0, 0.0, 1.0]).with_color([0.0, 1.0, 0.0]).build(), // uv (0,1)
];

let mut mesh_builder = MeshBuilder::new();
vertices.iter().for_each(|v| { mesh_builder.with_vertex(*v); });

let mesh: Mesh = mesh_builder
let mesh: Mesh = vertices
.iter()
.copied()
.fold(MeshBuilder::new(), |builder, vertex| builder.with_vertex(vertex))
.with_attributes(vec![
VertexAttribute { // position @ location 0
location: 0, offset: 0,
Expand Down
8 changes: 4 additions & 4 deletions docs/tutorials/rendering/resources/uniform-buffers.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ let vertices = [
VertexBuilder::new().with_position([ 0.0, -1.0, 0.0]).with_normal([0.0,0.0,0.0]).with_color([0.0,0.0,1.0]).build(),
];

let mut mesh_builder = MeshBuilder::new();
vertices.iter().for_each(|v| { mesh_builder.with_vertex(v.clone()); });

let mesh: Mesh = mesh_builder
let mesh: Mesh = vertices
.iter()
.copied()
.fold(MeshBuilder::new(), |builder, vertex| builder.with_vertex(vertex))
.with_attributes(vec![
VertexAttribute { // position @ location 0
location: 0, offset: 0,
Expand Down
9 changes: 3 additions & 6 deletions docs/tutorials/rendering/techniques/offscreen-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,9 @@ impl OffscreenPostExample {
.build(),
];

let mut mesh_builder = MeshBuilder::new();
for v in vertices {
mesh_builder.with_vertex(v);
}

return mesh_builder
return vertices
.into_iter()
.fold(MeshBuilder::new(), |builder, vertex| builder.with_vertex(vertex))
.with_attributes(vec![
VertexAttribute {
location: 0,
Expand Down
Loading