Add first working rotation
This commit is contained in:
parent
ec6e0c28be
commit
784e5b90be
7 changed files with 94 additions and 10 deletions
|
@ -1,13 +1,19 @@
|
|||
use crate::renderer::pipelines::triangle_pipeline::shaders::vs;
|
||||
use glam::{Mat3, Mat4, Vec3};
|
||||
use std::error::Error;
|
||||
use std::sync::Arc;
|
||||
use vulkano::buffer::Subbuffer;
|
||||
use std::time::Instant;
|
||||
use vulkano::buffer::allocator::{SubbufferAllocator, SubbufferAllocatorCreateInfo};
|
||||
use vulkano::buffer::{BufferUsage, Subbuffer};
|
||||
use vulkano::command_buffer::{AutoCommandBufferBuilder, PrimaryAutoCommandBuffer};
|
||||
use vulkano::descriptor_set::allocator::StandardDescriptorSetAllocator;
|
||||
use vulkano::descriptor_set::{DescriptorSet, WriteDescriptorSet};
|
||||
use vulkano::device::Device;
|
||||
use vulkano::memory::allocator::StandardMemoryAllocator;
|
||||
use vulkano::pipeline::GraphicsPipeline;
|
||||
use vulkano::memory::allocator::{MemoryTypeFilter, StandardMemoryAllocator};
|
||||
use vulkano::pipeline::{GraphicsPipeline, Pipeline, PipelineBindPoint};
|
||||
use vulkano::swapchain::Swapchain;
|
||||
|
||||
use crate::renderer::{create_triangle_pipeline, Vertex2D};
|
||||
use crate::renderer::{pipelines::triangle_pipeline::create_triangle_pipeline, Vertex2D};
|
||||
|
||||
const VERTICES: [Vertex2D; 12] = [
|
||||
// Triangle en haut à gauche
|
||||
|
@ -67,6 +73,11 @@ const VERTICES: [Vertex2D; 12] = [
|
|||
pub struct Scene {
|
||||
pipeline: Arc<GraphicsPipeline>,
|
||||
vertex_buffer: Subbuffer<[Vertex2D]>,
|
||||
|
||||
uniform_buffer_allocator: SubbufferAllocator,
|
||||
rotation_start: Instant,
|
||||
swapchain: Arc<Swapchain>,
|
||||
descriptor_set_allocator: Arc<StandardDescriptorSetAllocator>,
|
||||
}
|
||||
|
||||
impl Scene {
|
||||
|
@ -78,9 +89,28 @@ impl Scene {
|
|||
let pipeline = create_triangle_pipeline(device, swapchain)?;
|
||||
let vertex_buffer = Vertex2D::create_buffer(Vec::from_iter(VERTICES), memory_allocator)?;
|
||||
|
||||
let uniform_buffer_allocator = SubbufferAllocator::new(
|
||||
memory_allocator.clone(),
|
||||
SubbufferAllocatorCreateInfo {
|
||||
buffer_usage: BufferUsage::UNIFORM_BUFFER,
|
||||
memory_type_filter: MemoryTypeFilter::PREFER_DEVICE
|
||||
| MemoryTypeFilter::HOST_SEQUENTIAL_WRITE,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
let descriptor_set_allocator = Arc::new(StandardDescriptorSetAllocator::new(
|
||||
device.clone(),
|
||||
Default::default(),
|
||||
));
|
||||
|
||||
Ok(Scene {
|
||||
pipeline,
|
||||
vertex_buffer,
|
||||
uniform_buffer_allocator,
|
||||
rotation_start: Instant::now(),
|
||||
swapchain: swapchain.clone(),
|
||||
descriptor_set_allocator,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -91,13 +121,59 @@ impl Scene {
|
|||
let vertex_count = self.vertex_buffer.len() as u32;
|
||||
let instance_count = vertex_count / 3;
|
||||
|
||||
let uniform_buffer = self.get_uniform_buffer();
|
||||
let layout = &self.pipeline.layout().set_layouts()[0];
|
||||
let descriptor_set = DescriptorSet::new(
|
||||
self.descriptor_set_allocator.clone(),
|
||||
layout.clone(),
|
||||
[WriteDescriptorSet::buffer(0, uniform_buffer)],
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
unsafe {
|
||||
builder
|
||||
.bind_pipeline_graphics(self.pipeline.clone())?
|
||||
.bind_descriptor_sets(
|
||||
PipelineBindPoint::Graphics,
|
||||
self.pipeline.layout().clone(),
|
||||
0,
|
||||
descriptor_set,
|
||||
)?
|
||||
.bind_vertex_buffers(0, self.vertex_buffer.clone())?
|
||||
.draw(vertex_count, instance_count, 0, 0)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_uniform_buffer(&self) -> Subbuffer<vs::MVPData> {
|
||||
let elapsed = self.rotation_start.elapsed();
|
||||
let rotation = elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 / 1_000_000_000.0;
|
||||
let rotation = Mat3::from_rotation_y(rotation as f32);
|
||||
|
||||
// NOTE: This teapot was meant for OpenGL where the origin is at the lower left
|
||||
// instead the origin is at the upper left in Vulkan, so we reverse the Y axis.
|
||||
let aspect_ratio =
|
||||
self.swapchain.image_extent()[0] as f32 / self.swapchain.image_extent()[1] as f32;
|
||||
|
||||
let proj = Mat4::perspective_rh_gl(std::f32::consts::FRAC_PI_2, aspect_ratio, 0.01, 100.0);
|
||||
let view = Mat4::look_at_rh(
|
||||
Vec3::new(0.3, 0.3, 1.0),
|
||||
Vec3::new(0.0, 0.0, 0.0),
|
||||
Vec3::new(0.0, -1.0, 0.0),
|
||||
);
|
||||
let scale = Mat4::from_scale(Vec3::splat(1.0));
|
||||
|
||||
let uniform_data = vs::MVPData {
|
||||
world: Mat4::from_mat3(rotation).to_cols_array_2d(),
|
||||
view: (view * scale).to_cols_array_2d(),
|
||||
projection: proj.to_cols_array_2d(),
|
||||
};
|
||||
|
||||
let buffer = self.uniform_buffer_allocator.allocate_sized().unwrap();
|
||||
*buffer.write().unwrap() = uniform_data;
|
||||
|
||||
buffer
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue