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" @@ -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" @@ -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" }

26
citro3d/src/math/fvec.rs

@ -250,6 +250,32 @@ impl FVec3 { @@ -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)]
mod tests {
use approx::assert_abs_diff_eq;

14
citro3d/src/math/matrix.rs

@ -198,3 +198,17 @@ impl PartialEq<Matrix4> for Matrix4 { @@ -198,3 +198,17 @@ impl PartialEq<Matrix4> 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 @@ @@ -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;

14
citro3d/src/uniform.rs

@ -144,3 +144,17 @@ impl From<&Matrix4> for Uniform { @@ -144,3 +144,17 @@ impl From<&Matrix4> for Uniform {
(*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