glium_types/vectors/
uvec4.rs

1use derive_cmp_ops::*;
2use glium::uniforms::AsUniformValue;
3use super::{vec4::Vec4, uvec3::*, bvec4::*};
4#[derive(
5    Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, CmpAdd, CmpAddAssign, CmpDiv,
6    CmpDivAssign, CmpMul, CmpMulAssign, CmpRem, CmpRemAssign, CmpSub, CmpSubAssign
7)]
8/// an unsigned interger vector made from a x, y, z and w coordinate.
9pub struct UVec4 {
10    pub x: u32,
11    pub y: u32,
12    pub z: u32,
13    pub w: u32
14}
15impl UVec4 {
16    /// a zero vector
17    pub const ZERO: Self = uvec4(0, 0, 0, 0);
18    /// a vector full of ones
19    pub const ONE: Self = uvec4(1, 1, 1, 1);
20    /// the x axis
21    pub const X: Self = uvec4(1, 0, 0, 0);
22    /// the y axis
23    pub const Y: Self = uvec4(0, 1, 0, 0);
24    /// the z axis
25    pub const Z: Self = uvec4(0, 0, 1, 0);
26    /// the w axis
27    pub const W: Self = uvec4(0, 0, 0, 1);
28
29    pub const fn new(x: u32, y: u32, z: u32, w: u32) -> Self { Self { x, y, z, w } }
30    pub const fn truncate(self) -> UVec3 { uvec3(self.x, self.y, self.z) }
31    /// create a vector where x, y and z equals `value`
32    pub const fn splat(value: u32) -> Self { Self::new(value, value, value, value) }
33
34    /// the length of the vector without being square rooted
35    pub fn length_squared(self) -> u32 {
36        self.x*self.x + self.y*self.y + self.z*self.z + self.w*self.w
37    }
38    /// distance between two vectors before being square rooted
39    pub fn distance_squared(self, other: UVec4) -> u32{
40        (self - other).length_squared()
41    }
42    /// get the dot product of 2 vectors. equal to the cosign of the angle between vectors
43    pub fn dot(self, other: UVec4) -> u32{
44        self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w
45    }
46    /// multiplies each value by the scalar
47    pub fn scale(self, scalar: u32) -> UVec4{
48        Self::new(self.x * scalar, self.y * scalar, self.z * scalar, self.w * scalar)
49    }
50    /// returns whether the 2 components are equal
51    pub fn eq(self, rhs: Self) -> BVec4 { bvec4(self.x == rhs.x, self.y == rhs.y, self.z == rhs.z, self.w == rhs.w) }
52    /// returns whether the 1st components are less than the 2nd
53    pub fn less(self, rhs: Self) -> BVec4 { bvec4(self.x < rhs.x, self.y < rhs.y, self.z < rhs.z, self.w < rhs.w) }
54    /// returns whether the 1st components are more than the 2nd
55    pub fn more(self, rhs: Self) -> BVec4 { bvec4(self.x > rhs.x, self.y > rhs.y, self.z > rhs.z, self.w > rhs.w) }
56    /// returns whether the 1st components are less than or equal to the 2nd
57    pub fn less_or_eq(self, rhs: Self) -> BVec4 {
58        bvec4(self.x <= rhs.x, self.y <= rhs.y, self.z <= rhs.z, self.w <= rhs.w)
59    }
60    /// returns whether the 1st components are more than or equal to the 2nd
61    pub fn more_or_eq(self, rhs: Self) -> BVec4 {
62        bvec4(self.x >= rhs.x, self.y >= rhs.y, self.z >= rhs.z, self.w >= rhs.w)
63    }
64}
65impl std::ops::Mul<UVec4> for u32 {
66    fn mul(self, rhs: UVec4) -> Self::Output { rhs * self }
67    type Output = UVec4;
68}
69impl std::ops::Mul<u32> for UVec4 {
70    fn mul(self, rhs: u32) -> Self::Output { self.scale(rhs) }
71    type Output = Self;
72}
73impl std::ops::MulAssign<u32> for UVec4 { fn mul_assign(&mut self, rhs: u32) { *self = *self * rhs } }
74impl std::ops::Div<UVec4> for u32 {
75    fn div(self, rhs: UVec4) -> Self::Output { UVec4::splat(self) / rhs }
76    type Output = UVec4;
77}
78impl std::ops::Div<u32> for UVec4 {
79    fn div(self, rhs: u32) -> Self::Output { self / UVec4::splat(rhs) }
80    type Output = Self;
81}
82impl std::ops::DivAssign<u32> for UVec4 { fn div_assign(&mut self, rhs: u32) { *self = *self / rhs } }
83impl std::ops::Rem<UVec4> for u32 {
84    fn rem(self, rhs: UVec4) -> Self::Output { UVec4::splat(self) % rhs }
85    type Output = UVec4;
86}
87impl std::ops::Rem<u32> for UVec4 {
88    fn rem(self, rhs: u32) -> Self::Output { self % UVec4::splat(rhs) }
89    type Output = Self;
90}
91impl std::ops::RemAssign<u32> for UVec4 { fn rem_assign(&mut self, rhs: u32) { *self = *self % rhs } }
92
93impl AsUniformValue for UVec4{
94    fn as_uniform_value(&self) -> glium::uniforms::UniformValue<'_> {
95        glium::uniforms::UniformValue::UnsignedIntVec4([self.x, self.y, self.z, self.w])
96    }
97}
98impl From<Vec4> for UVec4 {
99    fn from(value: Vec4) -> Self {
100        Self { x: value.x as u32, y: value.y as u32, z: value.z as u32, w: value.w as u32 }
101    }
102}
103impl From<(u32, u32, u32, u32)> for UVec4 {
104    fn from(value: (u32, u32, u32, u32)) -> Self {
105        Self { x: value.0, y: value.1, z: value.2, w: value.3 }
106    }
107}
108impl From<[u32; 4]> for UVec4 {
109    fn from(value: [u32; 4]) -> Self {
110        Self { x: value[0], y: value[1], z: value[2], w: value[3] }
111    }
112}
113impl From<UVec4> for [u32; 4] {
114    fn from(value: UVec4) -> Self {
115        [value.x, value.y, value.z, value.w]
116    }
117}
118impl From<UVec4> for (u32, u32, u32, u32) {
119    fn from(value: UVec4) -> Self {
120        (value.x, value.y, value.z, value.w)
121    }
122}
123/// create an unsigned interger vector with an x, y, z and w coordinate.
124pub const fn uvec4(x: u32, y: u32, z: u32, w: u32) -> UVec4{
125    UVec4 { x, y, z, w }
126}