1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
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>
{
pub fn new() -> Self
where
Value: Default + Copy,
{
Self {
items: [[Value::default(); 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);
}
}
|