From 5c971d028825027e22d04cee2acf205f3608934b Mon Sep 17 00:00:00 2001 From: zomseffen Date: Tue, 28 May 2024 19:36:16 +0200 Subject: [PATCH] finish cube --- src/scene.rs | 93 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 23 deletions(-) diff --git a/src/scene.rs b/src/scene.rs index f1d91d8..e553bb3 100644 --- a/src/scene.rs +++ b/src/scene.rs @@ -23,68 +23,115 @@ impl Scene { pub unsafe fn prepare_data(&mut self, instance: &vulkanalia::Instance, device: &vulkanalia::Device, data: &AppData) -> Result<()> { let grid_size = 1000; + //todo use the 14 vertice box method. Not using geometry shaders seems to be faster... make this a setting? + // have cube elements with a method asking for vertices, while giving a primitive type -> method for preferred primitive type as well as one collecting all primitives for x_index in -grid_size..grid_size { for y_index in -grid_size..grid_size { let index = self.vertices.len(); + // 0 top left far self.vertices.push(vertex::Vertex::new( vec3(x_index as f32 - 0.5, y_index as f32 + 0.5, 0.5), vec3(0.0, 1.0, 0.0), vec2(0.0, 0.0) )); - self.indices.push(index as u32); - self.vertices.push(vertex::Vertex::new( - vec3(x_index as f32 - 0.5, y_index as f32 - 0.5, 0.5), - vec3(0.0, 1.0, 0.0), - vec2(0.0, 0.0) - )); - self.indices.push(index as u32 + 1); + // 1 top right far self.vertices.push(vertex::Vertex::new( vec3(x_index as f32 + 0.5, y_index as f32 + 0.5, 0.5), vec3(0.0, 1.0, 0.0), vec2(0.0, 0.0) )); - self.indices.push(index as u32 + 2); + // 2 top left near + self.vertices.push(vertex::Vertex::new( + vec3(x_index as f32 - 0.5, y_index as f32 - 0.5, 0.5), + vec3(0.0, 1.0, 0.0), + vec2(0.0, 0.0) + )); + // 3 top right near self.vertices.push(vertex::Vertex::new( vec3(x_index as f32 + 0.5, y_index as f32 - 0.5, 0.5), vec3(0.0, 1.0, 0.0), vec2(0.0, 0.0) )); - self.indices.push(index as u32 + 2); - self.indices.push(index as u32 + 1); - self.indices.push(index as u32 + 3); - } - } - for x_index in -grid_size..grid_size { - for y_index in -grid_size..grid_size { - let index = self.vertices.len(); + // 4 bottom left far self.vertices.push(vertex::Vertex::new( vec3(x_index as f32 - 0.5, y_index as f32 + 0.5, -0.5), vec3(0.0, 1.0, 0.0), vec2(0.0, 0.0) )); - self.vertices.push(vertex::Vertex::new( - vec3(x_index as f32 - 0.5, y_index as f32 - 0.5, -0.5), - vec3(0.0, 1.0, 0.0), - vec2(0.0, 0.0) - )); + // 5 bottom right far self.vertices.push(vertex::Vertex::new( vec3(x_index as f32 + 0.5, y_index as f32 + 0.5, -0.5), vec3(0.0, 1.0, 0.0), vec2(0.0, 0.0) )); + // 6 bottom left near + self.vertices.push(vertex::Vertex::new( + vec3(x_index as f32 - 0.5, y_index as f32 - 0.5, -0.5), + vec3(0.0, 1.0, 0.0), + vec2(0.0, 0.0) + )); + // 7 bottom right near self.vertices.push(vertex::Vertex::new( vec3(x_index as f32 + 0.5, y_index as f32 - 0.5, -0.5), vec3(0.0, 1.0, 0.0), vec2(0.0, 0.0) )); + + + // top + self.indices.push(index as u32 + 3); + self.indices.push(index as u32 + 0); self.indices.push(index as u32 + 2); - self.indices.push(index as u32 + 1); - self.indices.push(index as u32); self.indices.push(index as u32 + 3); self.indices.push(index as u32 + 1); + self.indices.push(index as u32 + 0); + + // bottom + self.indices.push(index as u32 + 6); + self.indices.push(index as u32 + 4); + self.indices.push(index as u32 + 7); + + self.indices.push(index as u32 + 4); + self.indices.push(index as u32 + 5); + self.indices.push(index as u32 + 7); + + // left + self.indices.push(index as u32 + 0); + self.indices.push(index as u32 + 4); self.indices.push(index as u32 + 2); + + self.indices.push(index as u32 + 6); + self.indices.push(index as u32 + 2); + self.indices.push(index as u32 + 4); + + // right + self.indices.push(index as u32 + 1); + self.indices.push(index as u32 + 3); + self.indices.push(index as u32 + 5); + + self.indices.push(index as u32 + 5); + self.indices.push(index as u32 + 3); + self.indices.push(index as u32 + 7); + + // near + self.indices.push(index as u32 + 6); + self.indices.push(index as u32 + 3); + self.indices.push(index as u32 + 2); + + self.indices.push(index as u32 + 3); + self.indices.push(index as u32 + 6); + self.indices.push(index as u32 + 7); + + // far + self.indices.push(index as u32 + 0); + self.indices.push(index as u32 + 1); + self.indices.push(index as u32 + 4); + + self.indices.push(index as u32 + 5); + self.indices.push(index as u32 + 4); + self.indices.push(index as u32 + 1); } }