Refactor camera code
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 7m57s
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 7m57s
This commit is contained in:
parent
7401a9b5f3
commit
1976a8b53e
9 changed files with 219 additions and 79 deletions
|
@ -1,5 +1,5 @@
|
|||
pub mod app;
|
||||
pub mod pipelines;
|
||||
pub mod primitives;
|
||||
pub mod render_context;
|
||||
pub mod vertex;
|
||||
pub mod vulkan_context;
|
||||
|
|
|
@ -20,13 +20,14 @@ use vulkano::pipeline::{
|
|||
};
|
||||
use vulkano::shader::{EntryPoint, ShaderStages};
|
||||
|
||||
use crate::core::render::vertex::Vertex2D;
|
||||
use crate::core::render::primitives::vertex::Vertex2D;
|
||||
|
||||
pub mod shaders {
|
||||
pub mod vs {
|
||||
vulkano_shaders::shader! {
|
||||
ty: "vertex",
|
||||
path: r"res/shaders/vertex.vert",
|
||||
generate_structs: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,6 +35,7 @@ pub mod shaders {
|
|||
vulkano_shaders::shader! {
|
||||
ty: "fragment",
|
||||
path: r"res/shaders/vertex.frag",
|
||||
generate_structs: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
48
src/core/render/primitives/camera.rs
Normal file
48
src/core/render/primitives/camera.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use glam::Mat4;
|
||||
use vulkano::{
|
||||
Validated,
|
||||
buffer::{AllocateBufferError, Subbuffer},
|
||||
memory::allocator::StandardMemoryAllocator,
|
||||
};
|
||||
|
||||
use super::{mvp::MVP, transform::Transform};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Camera {
|
||||
view: Mat4,
|
||||
projection: Mat4,
|
||||
|
||||
transform: Transform,
|
||||
}
|
||||
|
||||
impl Camera {
|
||||
pub fn new(view: Mat4, projection: Mat4) -> Self {
|
||||
Self {
|
||||
view,
|
||||
projection,
|
||||
transform: Transform::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_transform(&self) -> &Transform {
|
||||
&self.transform
|
||||
}
|
||||
|
||||
pub fn get_transform_mut(&mut self) -> &mut Transform {
|
||||
&mut self.transform
|
||||
}
|
||||
|
||||
pub fn set_projection(&mut self, projection: Mat4) {
|
||||
self.projection = projection;
|
||||
}
|
||||
|
||||
pub fn create_buffer(
|
||||
&self,
|
||||
memory_allocator: &Arc<StandardMemoryAllocator>,
|
||||
) -> Result<Subbuffer<[MVP]>, Validated<AllocateBufferError>> {
|
||||
MVP::new(&self.transform.get_mat4(), &self.view, &self.projection)
|
||||
.into_buffer(memory_allocator)
|
||||
}
|
||||
}
|
4
src/core/render/primitives/mod.rs
Normal file
4
src/core/render/primitives/mod.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
pub mod camera;
|
||||
mod mvp;
|
||||
pub mod transform;
|
||||
pub mod vertex;
|
45
src/core/render/primitives/mvp.rs
Normal file
45
src/core/render/primitives/mvp.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use glam::Mat4;
|
||||
use vulkano::Validated;
|
||||
use vulkano::buffer::{
|
||||
AllocateBufferError, Buffer, BufferContents, BufferCreateInfo, BufferUsage, Subbuffer,
|
||||
};
|
||||
use vulkano::memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator};
|
||||
|
||||
#[derive(BufferContents, Clone, Copy)]
|
||||
#[repr(C)]
|
||||
pub struct MVP {
|
||||
world: [[f32; 4]; 4],
|
||||
view: [[f32; 4]; 4],
|
||||
projection: [[f32; 4]; 4],
|
||||
}
|
||||
|
||||
impl MVP {
|
||||
pub fn new(world: &Mat4, view: &Mat4, projection: &Mat4) -> Self {
|
||||
Self {
|
||||
world: world.to_cols_array_2d(),
|
||||
view: view.to_cols_array_2d(),
|
||||
projection: projection.to_cols_array_2d(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_buffer(
|
||||
self,
|
||||
memory_allocator: &Arc<StandardMemoryAllocator>,
|
||||
) -> Result<Subbuffer<[MVP]>, Validated<AllocateBufferError>> {
|
||||
Buffer::from_iter(
|
||||
memory_allocator.clone(),
|
||||
BufferCreateInfo {
|
||||
usage: BufferUsage::UNIFORM_BUFFER,
|
||||
..Default::default()
|
||||
},
|
||||
AllocationCreateInfo {
|
||||
memory_type_filter: MemoryTypeFilter::PREFER_DEVICE
|
||||
| MemoryTypeFilter::HOST_SEQUENTIAL_WRITE,
|
||||
..Default::default()
|
||||
},
|
||||
[self],
|
||||
)
|
||||
}
|
||||
}
|
37
src/core/render/primitives/transform.rs
Normal file
37
src/core/render/primitives/transform.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
use glam::{Mat4, Quat, Vec3};
|
||||
|
||||
pub struct Transform {
|
||||
position: Vec3,
|
||||
rotation: Quat,
|
||||
scale: Vec3,
|
||||
}
|
||||
|
||||
impl Default for Transform {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
position: Vec3::default(),
|
||||
rotation: Quat::default(),
|
||||
scale: Vec3::ONE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Transform {
|
||||
pub fn rotate(&mut self, rotation: Quat) {
|
||||
self.rotation = self.rotation * rotation;
|
||||
}
|
||||
|
||||
pub fn translate(&mut self, translation: Vec3) {
|
||||
self.position += translation;
|
||||
}
|
||||
|
||||
pub fn scale(&mut self, scale: Vec3) {
|
||||
self.scale *= scale;
|
||||
}
|
||||
|
||||
pub fn get_mat4(&self) -> Mat4 {
|
||||
Mat4::from_translation(self.position)
|
||||
* Mat4::from_quat(self.rotation)
|
||||
* Mat4::from_scale(self.scale)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue