From c846fe5e269a827b3c324fb66b75be3fabc56a57 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 23 Oct 2023 22:38:12 +0200 Subject: feat(engine): add camera --- engine/src/renderer/mod.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) (limited to 'engine/src/renderer/mod.rs') 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, 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, + 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( -- cgit v1.2.3-18-g5258