diff options
Diffstat (limited to 'engine/src')
-rw-r--r-- | engine/src/lib.rs | 4 | ||||
-rw-r--r-- | engine/src/matrix.rs | 65 | ||||
-rw-r--r-- | engine/src/object.rs | 26 | ||||
-rw-r--r-- | engine/src/opengl/shader.rs | 12 | ||||
-rw-r--r-- | engine/src/renderer/mod.rs | 14 | ||||
-rw-r--r-- | engine/src/transform.rs | 45 | ||||
-rw-r--r-- | engine/src/vector.rs | 2 |
7 files changed, 161 insertions, 7 deletions
diff --git a/engine/src/lib.rs b/engine/src/lib.rs index db6ab94..dbbd407 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -7,8 +7,10 @@ use glfw::{Window, WindowBuilder}; use crate::object::{Id as ObjectId, Object}; use crate::vector::Vec2; +mod matrix; mod opengl; mod renderer; +mod transform; pub mod color; pub mod object; @@ -91,7 +93,7 @@ impl Engine while !self.window.should_close() { func(self); - crate::renderer::render(self.objects.values().map(Object::renderable)); + crate::renderer::render(self.objects.values()); self.window .swap_buffers() diff --git a/engine/src/matrix.rs b/engine/src/matrix.rs new file mode 100644 index 0000000..7e161f2 --- /dev/null +++ b/engine/src/matrix.rs @@ -0,0 +1,65 @@ +use crate::vector::Vec3; + +#[derive(Debug, Clone)] +#[repr(C)] +pub struct Matrix<Value, const ROWS: usize, const COLUMNS: usize> +{ + /// Items must be layed out this way for it to work with OpenGL shaders. + items: [[Value; ROWS]; COLUMNS], +} + +impl<Value, const ROWS: usize, const COLUMNS: usize> Matrix<Value, ROWS, COLUMNS> +{ + /// Sets the value at the specified cell. + pub fn set_cell(&mut self, row: usize, column: usize, value: Value) + { + self.items[column][row] = value; + } + + #[must_use] + pub fn as_ptr(&self) -> *const Value + { + self.items[0].as_ptr() + } +} + +impl<const ROWS_COLS: usize> Matrix<f32, ROWS_COLS, ROWS_COLS> +{ + /// Creates a new identity matrix. + #[must_use] + pub fn new_identity() -> Self + { + let mut index = 0; + + let items = [(); ROWS_COLS].map(|()| { + let mut columns = [0.0; ROWS_COLS]; + + columns[index] = 1.0; + + index += 1; + + columns + }); + + Self { items } + } +} + +impl Matrix<f32, 4, 4> +{ + pub fn translate(&mut self, translation: &Vec3<f32>) + { + self.set_cell(0, 3, translation.x); + self.set_cell(1, 3, translation.y); + self.set_cell(2, 3, translation.z); + self.set_cell(3, 3, 1.0); + } + + pub fn scale(&mut self, scaling: &Vec3<f32>) + { + self.set_cell(0, 0, scaling.x); + self.set_cell(1, 1, scaling.y); + self.set_cell(2, 2, scaling.z); + self.set_cell(3, 3, 1.0); + } +} diff --git a/engine/src/object.rs b/engine/src/object.rs index 11ba7e6..c3d82d0 100644 --- a/engine/src/object.rs +++ b/engine/src/object.rs @@ -5,6 +5,8 @@ use crate::opengl::shader::{ Shader, }; use crate::renderer::Renderable; +use crate::transform::Transform; +use crate::vector::Vec3; use crate::vertex::Vertex; #[derive(Debug)] @@ -12,6 +14,7 @@ pub struct Object { id: Id, renderable: Renderable, + transform: Transform, } impl Object @@ -53,7 +56,13 @@ impl Object let renderable = Renderable::new(shader_program, vertices); - Ok(Self { id, renderable }) + let transform = Transform::new(); + + Ok(Self { + id, + renderable, + transform, + }) } /// Returns the object ID. @@ -63,10 +72,25 @@ impl Object self.id } + pub fn translate(&mut self, translation: Vec3<f32>) + { + self.transform.set_translation(translation); + } + + pub fn scale(&mut self, scaling: Vec3<f32>) + { + self.transform.set_scaling(scaling); + } + pub(crate) fn renderable(&self) -> &Renderable { &self.renderable } + + pub(crate) fn transform(&self) -> &Transform + { + &self.transform + } } /// Object error diff --git a/engine/src/opengl/shader.rs b/engine/src/opengl/shader.rs index 0bbca77..603c790 100644 --- a/engine/src/opengl/shader.rs +++ b/engine/src/opengl/shader.rs @@ -1,6 +1,8 @@ use std::ffi::CStr; use std::ptr::null_mut; +use crate::matrix::Matrix; + #[derive(Debug)] pub struct Shader { @@ -156,6 +158,16 @@ impl Program } } + pub fn set_uniform_matrix_4fv(&self, name: &CStr, matrix: &Matrix<f32, 4, 4>) + { + let uniform_location = + unsafe { gl::GetUniformLocation(self.program, name.as_ptr().cast()) }; + + unsafe { + gl::UniformMatrix4fv(uniform_location, 1, gl::FALSE, matrix.as_ptr()); + } + } + fn get_info_log(&self) -> String { let mut buf = vec![gl::types::GLchar::default(); 512]; diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index 8ca85e2..068b8f0 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -1,8 +1,10 @@ use std::ffi::{c_void, CString}; use std::process::abort; +use cstr::cstr; use glfw::WindowSize; +use crate::object::Object; #[cfg(feature = "debug")] use crate::opengl::debug::{MessageSeverity, MessageSource, MessageType}; use crate::opengl::shader::Program as ShaderProgram; @@ -92,14 +94,18 @@ fn opengl_debug_message_cb( }; } -pub fn render<'renderable>(renderables: impl IntoIterator<Item = &'renderable Renderable>) +pub fn render<'obj>(objects: impl IntoIterator<Item = &'obj Object>) { clear_buffers(BufferClearMask::COLOR); - for renderable in renderables { - renderable.shader_program.activate(); + for obj in objects { + obj.renderable().shader_program.activate(); - renderable.vertex_arr.bind(|vert_arr_curr_bound| { + obj.renderable() + .shader_program + .set_uniform_matrix_4fv(cstr!("transform"), &obj.transform().as_matrix()); + + obj.renderable().vertex_arr.bind(|vert_arr_curr_bound| { VertexArray::draw(&vert_arr_curr_bound, PrimitiveKind::Triangles, 0, 3); }); } diff --git a/engine/src/transform.rs b/engine/src/transform.rs new file mode 100644 index 0000000..9ce52ee --- /dev/null +++ b/engine/src/transform.rs @@ -0,0 +1,45 @@ +use crate::matrix::Matrix; +use crate::vector::Vec3; + +#[derive(Debug, Clone)] +pub struct Transform +{ + translation: Vec3<f32>, + scaling: Vec3<f32>, +} + +impl Transform +{ + pub fn new() -> Self + { + Self { + translation: Vec3::default(), + scaling: Vec3 { + x: 1.0, + y: 1.0, + z: 1.0, + }, + } + } + + pub fn set_translation(&mut self, translation: Vec3<f32>) + { + self.translation = translation; + } + + pub fn set_scaling(&mut self, scaling: Vec3<f32>) + { + self.scaling = scaling; + } + + pub fn as_matrix(&self) -> Matrix<f32, 4, 4> + { + let mut matrix = Matrix::new_identity(); + + matrix.translate(&self.translation); + + matrix.scale(&self.scaling); + + matrix + } +} diff --git a/engine/src/vector.rs b/engine/src/vector.rs index ada64c4..c25a258 100644 --- a/engine/src/vector.rs +++ b/engine/src/vector.rs @@ -10,7 +10,7 @@ impl Vec2<u32> pub const ZERO: Self = Self { x: 0, y: 0 }; } -#[derive(Debug)] +#[derive(Debug, Default, Clone)] #[repr(C)] pub struct Vec3<Value> { |