diff options
Diffstat (limited to 'engine')
-rw-r--r-- | engine/src/camera.rs | 34 | ||||
-rw-r--r-- | engine/src/lib.rs | 11 | ||||
-rw-r--r-- | engine/src/renderer/mod.rs | 21 |
3 files changed, 52 insertions, 14 deletions
diff --git a/engine/src/camera.rs b/engine/src/camera.rs new file mode 100644 index 0000000..b98f3f4 --- /dev/null +++ b/engine/src/camera.rs @@ -0,0 +1,34 @@ +use crate::transform::Transform; +use crate::vector::Vec3; + +#[derive(Debug)] +pub struct Camera +{ + transform: Transform, +} + +impl Camera +{ + pub fn translate(&mut self, translation: Vec3<f32>) + { + self.transform.set_translation(translation); + } + + pub(crate) fn new() -> Self + { + let mut transform = Transform::new(); + + transform.set_translation(Vec3 { + x: 0.0, + y: 0.0, + z: -3.0, + }); + + Self { transform } + } + + pub(crate) fn transform(&self) -> &Transform + { + &self.transform + } +} diff --git a/engine/src/lib.rs b/engine/src/lib.rs index ee93695..ce953f8 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -4,6 +4,7 @@ use std::collections::BTreeMap; use glfw::{Window, WindowBuilder}; +use crate::camera::Camera; use crate::object::{Id as ObjectId, Object}; use crate::vector::Vec2; @@ -13,6 +14,7 @@ mod projection; mod renderer; mod transform; +pub mod camera; pub mod color; pub mod object; pub mod vector; @@ -26,6 +28,7 @@ pub struct Engine /// Objects have to be dropped before window. Otherwise, UB. objects: BTreeMap<ObjectId, Object>, window: Window, + camera: Camera, } impl Engine @@ -59,6 +62,7 @@ impl Engine Ok(Self { window, objects: BTreeMap::new(), + camera: Camera::new(), }) } @@ -96,7 +100,7 @@ impl Engine let window_size = self.window.size().map_err(Error::GetWindowSizeFailed)?; - crate::renderer::render(self.objects.values(), &window_size); + crate::renderer::render(self.objects.values(), &window_size, &self.camera); self.window .swap_buffers() @@ -109,6 +113,11 @@ impl Engine Ok(()) } + + pub fn camera(&mut self) -> &mut Camera + { + &mut self.camera + } } /// Engine Error diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index d577054..aa828fe 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -4,7 +4,7 @@ use std::process::abort; use cstr::cstr; use glfw::WindowSize; -use crate::matrix::Matrix; +use crate::camera::Camera; use crate::object::Object; use crate::opengl::buffer::{ ArrayKind as ArrayBufferKind, @@ -18,7 +18,7 @@ use crate::opengl::shader::Program as ShaderProgram; use crate::opengl::vertex_array::{PrimitiveKind, VertexArray}; use crate::opengl::{clear_buffers, BufferClearMask}; use crate::projection::new_perspective; -use crate::vector::{Vec2, Vec3}; +use crate::vector::Vec2; use crate::vertex::Vertex; pub fn initialize(window: &glfw::Window) -> Result<(), Error> @@ -104,6 +104,7 @@ fn opengl_debug_message_cb( pub fn render<'obj>( objects: impl IntoIterator<Item = &'obj Object>, window_size: &WindowSize, + camera: &Camera, ) { clear_buffers(BufferClearMask::COLOR); @@ -111,15 +112,7 @@ pub fn render<'obj>( for obj in objects { obj.renderable().shader_program.activate(); - let mut view = Matrix::new_identity(); - - view.translate(&Vec3 { - x: 0.0, - y: 0.0, - z: -3.0, - }); - - apply_transformation_matrices(obj, &view, window_size); + apply_transformation_matrices(obj, camera, window_size); obj.renderable().vertex_arr.bind(|vert_arr_curr_bound| { if let Some(index_info) = &obj.renderable().index_info { @@ -217,7 +210,7 @@ pub enum Error fn apply_transformation_matrices( object: &Object, - view: &Matrix<f32, 4, 4>, + camera: &Camera, window_size: &WindowSize, ) { @@ -226,10 +219,12 @@ fn apply_transformation_matrices( .shader_program .set_uniform_matrix_4fv(cstr!("model"), &object.transform().as_matrix()); + let view = camera.transform().as_matrix(); + object .renderable() .shader_program - .set_uniform_matrix_4fv(cstr!("view"), view); + .set_uniform_matrix_4fv(cstr!("view"), &view); #[allow(clippy::cast_precision_loss)] let projection = new_perspective( |