effect_core/primitives/
matrix.rs

1use std::ops::Mul;
2
3#[repr(C)]
4#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
5pub struct Matrix4 {
6    pub inner: [[f32; 4]; 4],
7}
8
9impl Matrix4 {
10    /// Returns the identity matrix
11    pub fn new() -> Self {
12        let inner = [
13            [1.0, 0.0, 0.0, 0.0],
14            [0.0, 1.0, 0.0, 0.0],
15            [0.0, 0.0, 1.0, 0.0],
16            [0.0, 0.0, 0.0, 1.0],
17        ];
18        Self { inner }
19    }
20
21    pub fn from_slice(mat_slice: [[f32; 4]; 4]) -> Self {
22        Self { inner: mat_slice }
23    }
24}
25
26impl Mul for Matrix4 {
27    type Output = Matrix4;
28    fn mul(self, rhs: Self) -> Self::Output {
29        let mut new_mat = Matrix4::new();
30        for i in 0..4 {
31            for j in 0..4 {
32                for k in 0..4 {
33                    new_mat.inner[i][j] += rhs.inner[k][j] * self.inner[i][k];
34                }
35            }
36        }
37        new_mat
38    }
39}