summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-08-17 03:32:02 -0700
committerIrene Knapp <ireneista@irenes.space>2026-08-17 03:32:02 -0700
commit4b63074631fa35bd875a79b5c31d490d10a0491e (patch)
tree9e4c35d764fec540a4783023291779ffcf4bfe1a /src
parentb14c2c4313cff700bfacde4c716e8effe68f66c1 (diff)
draw three teapots HEAD main
and split out the model rendering code a bunch for rapid instancing

Force-Push: yes
Change-Id: I75be46d8cd11bbf662013711ee5ec56abeb5e6ea
Diffstat (limited to 'src')
-rw-r--r--src/graphics/model.rs32
-rw-r--r--src/graphics/scene.rs62
2 files changed, 68 insertions, 26 deletions
diff --git a/src/graphics/model.rs b/src/graphics/model.rs
index 096de86..053818f 100644
--- a/src/graphics/model.rs
+++ b/src/graphics/model.rs
@@ -5,8 +5,6 @@ use crate::graphics::util::init_buffer;
 use crate::linear_algebra::{ Vec3, Vec4, Transformation };
 use crate::shader_data::{ Vertex, VertexPushBlock };
 
-use std::f32::consts::{ TAU };
-
 use vulkanalia::{ Device, Instance };
 use vulkanalia::vk::{ self, DeviceV1_0 };
 
@@ -60,8 +58,8 @@ impl Model {
   }
 
   #[allow(unsafe_code)]
-  pub fn render(&self, time: f32, device: &Device, render: &Render,
-                command_buffer: &vk::CommandBuffer)
+  pub fn make_active(&self, device: &Device, render: &Render,
+                     command_buffer: &vk::CommandBuffer)
   {
     unsafe {
       device.cmd_bind_vertex_buffers(*command_buffer, 0,
@@ -72,28 +70,12 @@ impl Model {
       device.cmd_bind_index_buffer(*command_buffer, self.index_buffer,
                                    0, vk::IndexType::UINT32)
     };
+  }
 
-    let scale = Vec3::new(1.0, 1.0, 1.0);
-    let model = Transformation {
-      rotation: Vec4::rotation_quaternion(&Vec3::new(0.0, 1.0, 0.0),
-                                          time % TAU),
-      translation: Vec3::new(0.0, 0.0, 0.0),
-    };
-    let push_block = VertexPushBlock::<f32> { scale, model };
-    let size = size_of::<VertexPushBlock::<f32>>();
-    let push_block_bytes = unsafe {
-      std::slice::from_raw_parts(&push_block as *const VertexPushBlock<f32>
-                                             as *const u8,
-                                 size)
-    };
-
-    unsafe {
-      device.cmd_push_constants(*command_buffer, render.pipeline_layout,
-                                vk::ShaderStageFlags::VERTEX,
-                                0,
-                                push_block_bytes)
-    };
-
+  #[allow(unsafe_code)]
+  pub fn render(&self, device: &Device, render: &Render,
+                command_buffer: &vk::CommandBuffer)
+  {
     unsafe {
       device.cmd_draw_indexed(*command_buffer, self.index_count as u32,
                               1, 0, 0, 0)
diff --git a/src/graphics/scene.rs b/src/graphics/scene.rs
index 9631dba..b27b68b 100644
--- a/src/graphics/scene.rs
+++ b/src/graphics/scene.rs
@@ -1,6 +1,10 @@
 #![deny(unsafe_code)]
 use crate::error::*;
 use crate::graphics::Render;
+use crate::linear_algebra::{ Vec3, Vec4, Transformation };
+use crate::shader_data::{ Vertex, VertexPushBlock };
+
+use std::f32::consts::{ TAU };
 
 use vulkanalia::Device;
 use vulkanalia::vk::{ self, HasBuilder, DeviceV1_0 };
@@ -78,7 +82,24 @@ pub fn generate_scene_commands<'a>(image_index: usize, time: f32,
   }
 
   if let Some(model) = &render.model {
-    model.render(time, device, render, command_buffer);
+    model.make_active(device, render, command_buffer);
+
+    let (scale, mut transformation) = demo_spin_transformation(time);
+    apply_model_transformation(device, render, command_buffer,
+                               &scale, &transformation);
+    model.render(device, render, command_buffer);
+
+    transformation.translation = transformation.translation.add(
+        &Vec3::new(0.0, 4.0, 0.0));
+    apply_model_transformation(device, render, command_buffer,
+                               &scale, &transformation);
+    model.render(device, render, command_buffer);
+
+    transformation.translation = transformation.translation.add(
+        &Vec3::new(0.0, -8.0, 0.0));
+    apply_model_transformation(device, render, command_buffer,
+                               &scale, &transformation);
+    model.render(device, render, command_buffer);
   }
 
   unsafe { device.cmd_end_render_pass(*command_buffer) };
@@ -88,3 +109,42 @@ pub fn generate_scene_commands<'a>(image_index: usize, time: f32,
   Ok(command_buffer)
 }
 
+
+fn demo_spin_transformation(time: f32) -> (Vec3<f32>, Transformation<f32>) {
+  let scale = Vec3::new(1.0, 1.0, 1.0);
+
+  let transformation = Transformation {
+    rotation: Vec4::rotation_quaternion(&Vec3::new(0.0, 1.0, 0.0),
+                                        time % TAU),
+    translation: Vec3::new(0.0, 0.0, 0.0),
+  };
+
+  (scale, transformation)
+}
+
+
+#[allow(unsafe_code)]
+fn apply_model_transformation(device: &Device, render: &Render,
+                              command_buffer: &vk::CommandBuffer,
+                              scale: &Vec3<f32>,
+                              transformation: &Transformation<f32>)
+{
+  let push_block = VertexPushBlock::<f32> {
+    scale: scale.clone(),
+    model: transformation.clone(),
+  };
+  let size = size_of::<VertexPushBlock::<f32>>();
+  let push_block_bytes = unsafe {
+    std::slice::from_raw_parts(&push_block as *const VertexPushBlock<f32>
+                                           as *const u8,
+                               size)
+  };
+
+  unsafe {
+    device.cmd_push_constants(*command_buffer, render.pipeline_layout,
+                              vk::ShaderStageFlags::VERTEX,
+                              0,
+                              push_block_bytes)
+  };
+}
+