Browse Source

feat: glam support

pull/43/head
Natasha England-Elbro 11 months ago
parent
commit
3d2a53d133
No known key found for this signature in database
GPG Key ID: 46F323AE9236FD6A
  1. 5
      citro3d/Cargo.toml
  2. 26
      citro3d/src/math/fvec.rs
  3. 14
      citro3d/src/math/matrix.rs
  4. 3
      citro3d/src/math/ops.rs
  5. 14
      citro3d/src/uniform.rs

5
citro3d/Cargo.toml

@ -6,6 +6,7 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
glam = { version = "0.25.0", optional = true }
approx = { version = "0.5.1", optional = true } approx = { version = "0.5.1", optional = true }
bitflags = "1.3.2" bitflags = "1.3.2"
bytemuck = { version = "1.10.0", features = ["extern_crate_std"] } bytemuck = { version = "1.10.0", features = ["extern_crate_std"] }
@ -17,9 +18,11 @@ document-features = "0.2.7"
libc = "0.2.125" libc = "0.2.125"
[features] [features]
default = [] default = ["glam"]
## Enable this feature to use the `approx` crate for comparing vectors and matrices. ## Enable this feature to use the `approx` crate for comparing vectors and matrices.
approx = ["dep:approx"] approx = ["dep:approx"]
# Enable for glam support in uniforms
glam = ["dep:glam"]
[dev-dependencies] [dev-dependencies]
test-runner = { git = "https://github.com/rust3ds/test-runner.git" } test-runner = { git = "https://github.com/rust3ds/test-runner.git" }

26
citro3d/src/math/fvec.rs

@ -250,6 +250,32 @@ impl FVec3 {
} }
} }
#[cfg(feature = "glam")]
impl From<glam::Vec4> for FVec4 {
fn from(value: glam::Vec4) -> Self {
Self::new(value.x, value.y, value.z, value.w)
}
}
#[cfg(feature = "glam")]
impl From<glam::Vec3> for FVec3 {
fn from(value: glam::Vec3) -> Self {
Self::new(value.x, value.y, value.z)
}
}
#[cfg(feature = "glam")]
impl From<FVec4> for glam::Vec4 {
fn from(value: FVec4) -> Self {
glam::Vec4::new(value.x(), value.y(), value.z(), value.w())
}
}
#[cfg(feature = "glam")]
impl From<FVec3> for glam::Vec3 {
fn from(value: FVec3) -> Self {
glam::Vec3::new(value.x(), value.y(), value.z())
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use approx::assert_abs_diff_eq; use approx::assert_abs_diff_eq;

14
citro3d/src/math/matrix.rs

@ -198,3 +198,17 @@ impl PartialEq<Matrix4> for Matrix4 {
} }
} }
impl Eq for Matrix4 {} impl Eq for Matrix4 {}
#[cfg(feature = "glam")]
impl From<glam::Mat4> for Matrix4 {
fn from(mat: glam::Mat4) -> Self {
Matrix4::from_rows(core::array::from_fn(|i| mat.row(i).into()))
}
}
#[cfg(feature = "glam")]
impl From<Matrix4> for glam::Mat4 {
fn from(mat: Matrix4) -> Self {
glam::Mat4::from_cols_array_2d(&mat.rows_xyzw()).transpose()
}
}

3
citro3d/src/math/ops.rs

@ -1,6 +1,5 @@
use std::borrow::Borrow;
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
use std::ops::{Add, Deref, Div, Mul, Neg, Sub}; use std::ops::{Add, Div, Mul, Neg, Sub};
#[cfg(feature = "approx")] #[cfg(feature = "approx")]
use approx::AbsDiffEq; use approx::AbsDiffEq;

14
citro3d/src/uniform.rs

@ -144,3 +144,17 @@ impl From<&Matrix4> for Uniform {
(*value).into() (*value).into()
} }
} }
#[cfg(feature = "glam")]
impl From<glam::Vec4> for Uniform {
fn from(value: glam::Vec4) -> Self {
Self::Float(value.into())
}
}
#[cfg(feature = "glam")]
impl From<glam::Mat4> for Uniform {
fn from(value: glam::Mat4) -> Self {
Self::Float4(value.into())
}
}

Loading…
Cancel
Save