summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-07-13 13:25:16 -0700
committerIrene Knapp <ireneista@irenes.space>2026-07-15 22:35:16 -0700
commit5dddb64f6a2fd072f2a26d4400f9a2a84386dc4e (patch)
treedb4e3b75cb5230f97f068b5e9455856cecba8807 /src/main.rs
parentec66f7419f2155d1b4bd852fdc3da87646d865c0 (diff)
it spins!!!!
ahem. more formally: provide animated uniforms to the shader, and use them

the tutorial just wants us to do three matrices, and to defer all the actual math to a library it recommends. that would certainly be simpler. anyway, we use quaterion+offset for everything but the projection frustum, and do all the math ourselves

Change-Id: I399f432bebb1e7ca4d4342efa1d5aaf37b32f427
Force-Push: yes
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index e6e3c0a..7372a24 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,8 +6,13 @@ use crate::graphics_permanent::{
 use crate::graphics_window_dressing::{
   WindowDressing, N_SIMULTANEOUS_FRAMES
 };
+use crate::linear_algebra::{ Vec3, Vec4, Mat4, Transformation, UniformBlock };
 
 use std::cell::RefCell;
+use std::f32::consts::{ FRAC_PI_2, FRAC_PI_4, TAU };
+use std::ptr::copy_nonoverlapping;
+use std::time::Instant;
+use vulkanalia::Device;
 use vulkanalia::vk::{ self, Handle, HasBuilder, DeviceV1_0,
                       KhrSwapchainExtensionDeviceCommands };
 use winit::application::ApplicationHandler;
@@ -28,6 +33,7 @@ struct Surreality {
   is_minimized: bool,
   is_reinit_queued: bool,
   frame_index: usize,
+  simulation_start: Instant,
 }
 
 impl Surreality {
@@ -39,6 +45,7 @@ impl Surreality {
       is_minimized: false,
       is_reinit_queued: false,
       frame_index: 0,
+      simulation_start: Instant::now(),
     }
   }
 
@@ -120,6 +127,11 @@ impl Surreality {
 
       concurrency.image_fences[image_index] = *frame_fence;
 
+      render_uniforms(device,
+                      &window_dressing.uniform_buffer_memory[image_index],
+                      &window_dressing.swapchain.extent,
+                      self.simulation_start)?;
+
       let first_semaphores = [*image_available_semaphore];
       let second_semaphores = [*rendering_finished_semaphore];
 
@@ -170,6 +182,10 @@ impl Drop for Surreality {
         window_dressing.destroy(&permanent.device);
       }
 
+      if let Some(for_reinit) = self.for_reinit.replace(None) {
+        for_reinit.destroy(&permanent.device);
+      }
+
       permanent.destroy();
     }
   }
@@ -217,6 +233,12 @@ impl ApplicationHandler for Surreality {
       _ => { }
     }
   }
+
+  fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
+    if let Some(permanent) = self.permanent.borrow().as_ref() {
+      permanent.window.request_redraw();
+    }
+  }
 }
 
 
@@ -237,3 +259,39 @@ fn main() -> std::process::ExitCode {
   }
 }
 
+
+#[allow(unsafe_code)]
+fn render_uniforms(device: &Device, device_memory: &vk::DeviceMemory,
+                   extent: &vk::Extent2D, simulation_start: Instant)
+    -> Result<()>
+{
+  let time = simulation_start.elapsed().as_secs_f32();
+
+  let scale = Vec3::new(1.0, 1.0, 1.0);
+  let model = Transformation {
+    rotation: Vec4::rotation_quaternion(&Vec3::new(-1.0, 0.0, 0.0), FRAC_PI_2)
+                  .rotate(&Vec3::new(0.0, 1.0, 0.0), time % TAU),
+    translation: Vec3::new(0.0, 0.0, 0.0),
+  };
+  let view = Transformation {
+    rotation: Vec4::rotation_quaternion(&Vec3::new(1.0, 0.0, 0.0), 0.1),
+    translation: Vec3::new(0.0, 0.0, 2.0),
+  };
+  let aspect_ratio = extent.width as f32 / extent.height as f32;
+  let projection = Mat4::perspective(FRAC_PI_4, aspect_ratio, 0.1, 10.0);
+  let block = UniformBlock::<f32> { scale, model, view, projection };
+
+  let size = size_of::<UniformBlock<f32>>() as u64;
+  let host_memory = unsafe {
+    device.map_memory(*device_memory, 0, size, vk::MemoryMapFlags::empty())
+  }?;
+
+  unsafe {
+    copy_nonoverlapping(&block, host_memory.cast(), 1)
+  };
+
+  unsafe { device.unmap_memory(*device_memory) };
+
+  Ok(())
+}
+