From 386aad9dac7c22cabb0f1bbba33d248c40bb0310 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 23 Oct 2023 22:23:25 +0200 Subject: feat(engine): add perspective --- engine/src/renderer/mod.rs | 51 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) (limited to 'engine/src/renderer') diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index a02bbc2..d577054 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -4,6 +4,7 @@ use std::process::abort; use cstr::cstr; use glfw::WindowSize; +use crate::matrix::Matrix; use crate::object::Object; use crate::opengl::buffer::{ ArrayKind as ArrayBufferKind, @@ -16,7 +17,8 @@ use crate::opengl::debug::{MessageSeverity, MessageSource, MessageType}; use crate::opengl::shader::Program as ShaderProgram; use crate::opengl::vertex_array::{PrimitiveKind, VertexArray}; use crate::opengl::{clear_buffers, BufferClearMask}; -use crate::vector::Vec2; +use crate::projection::new_perspective; +use crate::vector::{Vec2, Vec3}; use crate::vertex::Vertex; pub fn initialize(window: &glfw::Window) -> Result<(), Error> @@ -99,16 +101,25 @@ fn opengl_debug_message_cb( }; } -pub fn render<'obj>(objects: impl IntoIterator) +pub fn render<'obj>( + objects: impl IntoIterator, + window_size: &WindowSize, +) { clear_buffers(BufferClearMask::COLOR); for obj in objects { obj.renderable().shader_program.activate(); - obj.renderable() - .shader_program - .set_uniform_matrix_4fv(cstr!("transform"), &obj.transform().as_matrix()); + 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); obj.renderable().vertex_arr.bind(|vert_arr_curr_bound| { if let Some(index_info) = &obj.renderable().index_info { @@ -203,3 +214,33 @@ pub enum Error #[error("Failed to get window size")] GetWindowSizeFailed(#[source] glfw::Error), } + +fn apply_transformation_matrices( + object: &Object, + view: &Matrix, + window_size: &WindowSize, +) +{ + object + .renderable() + .shader_program + .set_uniform_matrix_4fv(cstr!("model"), &object.transform().as_matrix()); + + object + .renderable() + .shader_program + .set_uniform_matrix_4fv(cstr!("view"), view); + + #[allow(clippy::cast_precision_loss)] + let projection = new_perspective( + 80.0f32.to_radians(), + window_size.width as f32 / window_size.height as f32, + 0.1, + 100.0, + ); + + object + .renderable() + .shader_program + .set_uniform_matrix_4fv(cstr!("projection"), &projection); +} -- cgit v1.2.3-18-g5258