summaryrefslogtreecommitdiff
path: root/engine/src/matrix.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-10-22 19:29:11 +0200
committerHampusM <hampus@hampusmat.com>2023-10-22 19:29:11 +0200
commit54c0fd70f82eb1a6814872c78bc22380f438c9d1 (patch)
tree7b0b67b52daa7ce18432c7b83caa862234a3255c /engine/src/matrix.rs
parent30394c16ccdcdb145352e245a7a8893cef28e82d (diff)
feat(engine): add translating & scaling objects
Diffstat (limited to 'engine/src/matrix.rs')
-rw-r--r--engine/src/matrix.rs65
1 files changed, 65 insertions, 0 deletions
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);
+ }
+}