swamp_wgpu_math/
lib.rs

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/piot/swamp-render
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */

use std::ops::{Add, Index, Mul};

// ------------ Ortho -------------
pub struct OrthoInfo {
    pub left: f32,
    pub right: f32,
    pub bottom: f32,
    pub top: f32,
    pub near: f32,
    pub far: f32,
}

impl From<OrthoInfo> for Matrix4 {
    fn from(ortho: OrthoInfo) -> Self {
        let c0r0 = 2.0 / (ortho.right - ortho.left);
        let c1r1 = 2.0 / (ortho.top - ortho.bottom);

        let c2r2 = -2.0 / (ortho.far - ortho.near);

        let c3r0 = -(ortho.right + ortho.left) / (ortho.right - ortho.left);
        let c3r1 = -(ortho.top + ortho.bottom) / (ortho.top - ortho.bottom);
        let c3r2 = -(ortho.far + ortho.near) / (ortho.far - ortho.near);

        //    #[rustfmt::skip]
        Self([
            [c0r0, 0.0, 0.0, 0.0].into(),
            [0.0, c1r1, 0.0, 0.0].into(),
            [0.0, 0.0, c2r2, 0.0].into(),
            [c3r0, c3r1, c3r2, 1.0].into(),
        ])
    }
}

// ----------------- FMatrix4 ----------------
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct Matrix4([Vec4; 4]);
impl Matrix4 {
    #[inline]
    pub fn from_scale(x: f32, y: f32, z: f32) -> Self {
        Self::from([
            [x, 0.0, 0.0, 0.0],
            [0.0, y, 0.0, 0.0],
            [0.0, 0.0, z, 0.0],
            [0.0, 0.0, 0.0, 1.0],
        ])
    }

    #[inline]
    pub fn from_translation(x: f32, y: f32, z: f32) -> Self {
        Self::from([
            [1.0, 0.0, 0.0, 0.0],
            [0.0, 1.0, 0.0, 0.0],
            [0.0, 0.0, 1.0, 0.0],
            [x, y, z, 1.0],
        ])
    }

    #[inline]
    pub fn identity() -> Self {
        Self::from_scale(1.0, 1.0, 1.0)
    }
}

impl From<[[f32; 4]; 4]> for Matrix4 {
    fn from(v: [[f32; 4]; 4]) -> Self {
        Self([v[0].into(), v[1].into(), v[2].into(), v[3].into()])
    }
}

impl Index<usize> for Matrix4 {
    type Output = Vec4;

    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        &self.0[index]
    }
}

impl Mul<Self> for Matrix4 {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        let a = self[0];
        let b = self[1];
        let c = self[2];
        let d = self[3];

        Self([
            a * rhs[0][0] + b * rhs[0][1] + c * rhs[0][2] + d * rhs[0][3],
            a * rhs[1][0] + b * rhs[1][1] + c * rhs[1][2] + d * rhs[1][3],
            a * rhs[2][0] + b * rhs[2][1] + c * rhs[2][2] + d * rhs[2][3],
            a * rhs[3][0] + b * rhs[3][1] + c * rhs[3][2] + d * rhs[3][3],
        ])
    }
}

// ------------- FVec4

#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct Vec4(pub [f32; 4]);

impl From<[f32; 4]> for Vec4 {
    fn from(v: [f32; 4]) -> Self {
        Self(v)
    }
}

impl Mul<f32> for Vec4 {
    type Output = Self;

    fn mul(self, rhs: f32) -> Self::Output {
        Self([self[0] * rhs, self[1] * rhs, self[2] * rhs, self[3] * rhs])
    }
}

impl Add<Self> for Vec4 {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self([
            self.0[0] + rhs.0[0],
            self.0[1] + rhs.0[1],
            self.0[2] + rhs.0[2],
            self.0[3] + rhs.0[3],
        ])
    }
}

impl Index<usize> for Vec4 {
    type Output = f32;

    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        &self.0[index]
    }
}