From 3d2a53d13338e6b11dbb795ceb235dc0856bb015 Mon Sep 17 00:00:00 2001 From: Natasha England-Elbro Date: Thu, 1 Feb 2024 23:55:57 +0000 Subject: [PATCH] feat: glam support --- citro3d/Cargo.toml | 5 ++++- citro3d/src/math/fvec.rs | 26 ++++++++++++++++++++++++++ citro3d/src/math/matrix.rs | 14 ++++++++++++++ citro3d/src/math/ops.rs | 3 +-- citro3d/src/uniform.rs | 14 ++++++++++++++ 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/citro3d/Cargo.toml b/citro3d/Cargo.toml index 4147662..a139e28 100644 --- a/citro3d/Cargo.toml +++ b/citro3d/Cargo.toml @@ -6,6 +6,7 @@ version = "0.1.0" edition = "2021" [dependencies] +glam = { version = "0.25.0", optional = true } approx = { version = "0.5.1", optional = true } bitflags = "1.3.2" bytemuck = { version = "1.10.0", features = ["extern_crate_std"] } @@ -17,9 +18,11 @@ document-features = "0.2.7" libc = "0.2.125" [features] -default = [] +default = ["glam"] ## Enable this feature to use the `approx` crate for comparing vectors and matrices. approx = ["dep:approx"] +# Enable for glam support in uniforms +glam = ["dep:glam"] [dev-dependencies] test-runner = { git = "https://github.com/rust3ds/test-runner.git" } diff --git a/citro3d/src/math/fvec.rs b/citro3d/src/math/fvec.rs index 3fa888e..127855b 100644 --- a/citro3d/src/math/fvec.rs +++ b/citro3d/src/math/fvec.rs @@ -250,6 +250,32 @@ impl FVec3 { } } +#[cfg(feature = "glam")] +impl From for FVec4 { + fn from(value: glam::Vec4) -> Self { + Self::new(value.x, value.y, value.z, value.w) + } +} +#[cfg(feature = "glam")] +impl From for FVec3 { + fn from(value: glam::Vec3) -> Self { + Self::new(value.x, value.y, value.z) + } +} +#[cfg(feature = "glam")] +impl From for glam::Vec4 { + fn from(value: FVec4) -> Self { + glam::Vec4::new(value.x(), value.y(), value.z(), value.w()) + } +} + +#[cfg(feature = "glam")] +impl From for glam::Vec3 { + fn from(value: FVec3) -> Self { + glam::Vec3::new(value.x(), value.y(), value.z()) + } +} + #[cfg(test)] mod tests { use approx::assert_abs_diff_eq; diff --git a/citro3d/src/math/matrix.rs b/citro3d/src/math/matrix.rs index 7397a3d..96fbbbe 100644 --- a/citro3d/src/math/matrix.rs +++ b/citro3d/src/math/matrix.rs @@ -198,3 +198,17 @@ impl PartialEq for Matrix4 { } } impl Eq for Matrix4 {} + +#[cfg(feature = "glam")] +impl From 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 for glam::Mat4 { + fn from(mat: Matrix4) -> Self { + glam::Mat4::from_cols_array_2d(&mat.rows_xyzw()).transpose() + } +} diff --git a/citro3d/src/math/ops.rs b/citro3d/src/math/ops.rs index 92823ed..ce962fc 100644 --- a/citro3d/src/math/ops.rs +++ b/citro3d/src/math/ops.rs @@ -1,6 +1,5 @@ -use std::borrow::Borrow; use std::mem::MaybeUninit; -use std::ops::{Add, Deref, Div, Mul, Neg, Sub}; +use std::ops::{Add, Div, Mul, Neg, Sub}; #[cfg(feature = "approx")] use approx::AbsDiffEq; diff --git a/citro3d/src/uniform.rs b/citro3d/src/uniform.rs index e35fc2c..e4eb46d 100644 --- a/citro3d/src/uniform.rs +++ b/citro3d/src/uniform.rs @@ -144,3 +144,17 @@ impl From<&Matrix4> for Uniform { (*value).into() } } + +#[cfg(feature = "glam")] +impl From for Uniform { + fn from(value: glam::Vec4) -> Self { + Self::Float(value.into()) + } +} + +#[cfg(feature = "glam")] +impl From for Uniform { + fn from(value: glam::Mat4) -> Self { + Self::Float4(value.into()) + } +}