summaryrefslogtreecommitdiff
path: root/engine/src/renderer
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-10-23 22:23:25 +0200
committerHampusM <hampus@hampusmat.com>2023-10-23 22:23:25 +0200
commit386aad9dac7c22cabb0f1bbba33d248c40bb0310 (patch)
tree1f4379ab50ffc85e9ad511b9fe9e9dd8a7f8c321 /engine/src/renderer
parentfee2320189d6e758748a939c56d539043cbffde6 (diff)
feat(engine): add perspective
Diffstat (limited to 'engine/src/renderer')
-rw-r--r--engine/src/renderer/mod.rs51
1 files changed, 46 insertions, 5 deletions
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<Item = &'obj Object>)
+pub fn render<'obj>(
+ objects: impl IntoIterator<Item = &'obj Object>,
+ 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<f32, 4, 4>,
+ 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);
+}