diff options
author | HampusM <hampus@hampusmat.com> | 2023-11-27 20:51:26 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-11-27 20:51:59 +0100 |
commit | d3f8b55aa971d1ef00345e80bc40365927bf08e5 (patch) | |
tree | e540378ba74323f59c994daeb88e4f0d0c910fb4 /engine/src/renderer/mod.rs | |
parent | fe975af71dbb7c615a1e36920c6b8df3d41d1bdb (diff) |
feat(engine): add support for custom camera implementations
Diffstat (limited to 'engine/src/renderer/mod.rs')
-rw-r--r-- | engine/src/renderer/mod.rs | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index 593b55f..2c513ff 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -7,6 +7,7 @@ use glfw::WindowSize; use crate::camera::Camera; use crate::color::Color; use crate::lighting::LightSource; +use crate::matrix::Matrix; use crate::object::Object; use crate::opengl::buffer::{ ArrayKind as ArrayBufferKind, @@ -26,14 +27,16 @@ use crate::vector::{Vec2, Vec3}; use crate::vertex::Vertex; #[derive(Debug)] -pub struct Renderer +pub struct Renderer<CameraT> { - camera: Camera, + camera: CameraT, } -impl Renderer +impl<CameraT> Renderer<CameraT> +where + CameraT: Camera, { - pub fn new(window: &glfw::Window) -> Result<Self, Error> + pub fn new(window: &glfw::Window, camera: CameraT) -> Result<Self, Error> { gl::load_with(|symbol| { let proc_name = unsafe { CString::from_vec_unchecked(symbol.into()) }; @@ -60,8 +63,6 @@ impl Renderer enable(Capability::DepthTest); - let camera = Camera::new(); - Ok(Self { camera }) } @@ -116,12 +117,12 @@ impl Renderer } #[must_use] - pub fn camera(&self) -> &Camera + pub fn camera(&self) -> &CameraT { &self.camera } - pub fn camera_mut(&mut self) -> &mut Camera + pub fn camera_mut(&mut self) -> &mut CameraT { &mut self.camera } @@ -232,7 +233,7 @@ pub enum Error fn apply_transformation_matrices( object: &Object, - camera: &Camera, + camera: &impl Camera, window_size: &WindowSize, shader_program_curr_bound: &CurrentlyBound<ShaderProgram>, ) @@ -243,7 +244,7 @@ fn apply_transformation_matrices( &object.transform().as_matrix(), ); - let view = camera.as_matrix(); + let view = create_view(camera); object.renderable().shader_program.set_uniform_matrix_4fv( shader_program_curr_bound, @@ -269,7 +270,7 @@ fn apply_transformation_matrices( fn apply_light( obj: &Object, light_source: Option<&LightSource>, - camera: &Camera, + camera: &impl Camera, shader_program_curr_bound: &CurrentlyBound<ShaderProgram>, ) { @@ -341,10 +342,19 @@ fn apply_light( obj.renderable().shader_program.set_uniform_vec_3fv( shader_program_curr_bound, cstr!("view_pos"), - camera.position(), + &camera.position(), ); } +fn create_view(camera: &impl Camera) -> Matrix<f32, 4, 4> +{ + let mut view = Matrix::new(); + + view.look_at(&camera.position(), &camera.target(), &camera.up()); + + view +} + #[cfg(feature = "debug")] #[tracing::instrument(skip_all)] fn opengl_debug_message_cb( |