Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clipping refactoring #194

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion core/src/geom.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//! Basic geometric primitives.

use crate::math::vec::{Vec2, Vec3};
use crate::{
math::point::{Point2, Point3},
math::vec::{Vec2, Vec3},
render::Model,
};

pub use mesh::Mesh;

Expand All @@ -13,6 +17,12 @@ pub struct Vertex<P, A> {
pub attrib: A,
}

/// Two-dimensional vertex type.
pub type Vertex2<A, B = Model> = Vertex<Point2<B>, A>;

/// Three-dimensional vertex type.
pub type Vertex3<A, B = Model> = Vertex<Point3<B>, A>;

/// Triangle, defined by three vertices.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(transparent)]
Expand Down
51 changes: 25 additions & 26 deletions core/src/geom/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ use alloc::{vec, vec::Vec};

use crate::math::{
mat::{Mat4x4, RealToReal},
point::Point3,
space::Linear,
vec::Vec3,
};
use crate::render::Model;

use super::{vertex, Normal3, Tri};

/// Convenience type alias for a mesh vertex.
pub type Vertex<A, B = Model> = super::Vertex<Vec3<B>, A>;
use super::{vertex, Normal3, Tri, Vertex3};

/// A triangle mesh.
///
Expand All @@ -32,7 +29,7 @@ pub struct Mesh<Attrib, Basis = Model> {
/// to the `verts` vector. Several faces can share a vertex.
pub faces: Vec<Tri<usize>>,
/// The vertices of the mesh.
pub verts: Vec<Vertex<Attrib, Basis>>,
pub verts: Vec<Vertex3<Attrib, Basis>>,
}

/// A builder type for creating meshes.
Expand All @@ -54,16 +51,17 @@ impl<A, B> Mesh<A, B> {
/// # Examples
/// ```
/// # use retrofire_core::geom::{Tri, Mesh, vertex};
/// # use retrofire_core::math::vec3;
/// # use retrofire_core::math::point::pt3;
/// let verts = [
/// vec3(0.0, 0.0, 0.0),
/// vec3(1.0, 0.0, 0.0),
/// vec3(0.0, 1.0, 0.0),
/// vec3(0.0, 0.0, 1.0)
/// pt3(0.0, 0.0, 0.0),
/// pt3(1.0, 0.0, 0.0),
/// pt3(0.0, 1.0, 0.0),
/// pt3(0.0, 0.0, 1.0)
/// ]
/// .map(|v| vertex(v, ()));
///
/// let faces = [
/// // Indices point to the verts array
/// Tri([0, 1, 2]),
/// Tri([0, 1, 3]),
/// Tri([0, 2, 3]),
Expand All @@ -78,7 +76,7 @@ impl<A, B> Mesh<A, B> {
pub fn new<F, V>(faces: F, verts: V) -> Self
where
F: IntoIterator<Item = Tri<usize>>,
V: IntoIterator<Item = Vertex<A, B>>,
V: IntoIterator<Item = Vertex3<A, B>>,
{
let faces: Vec<_> = faces.into_iter().collect();
let verts: Vec<_> = verts.into_iter().collect();
Expand Down Expand Up @@ -129,16 +127,16 @@ impl<A> Builder<A> {
}

/// Appends a vertex with the given position and attribute.
pub fn push_vert(&mut self, pos: Vec3, attrib: A) {
pub fn push_vert(&mut self, pos: Point3, attrib: A) {
self.mesh.verts.push(vertex(pos.to(), attrib));
}

/// Appends all the vertices yielded by the given iterator.
pub fn push_verts<Vs>(&mut self, verts: Vs)
where
Vs: IntoIterator<Item = (Vec3, A)>,
Vs: IntoIterator<Item = (Point3, A)>,
{
let vs = verts.into_iter().map(|(v, a)| vertex(v.to(), a));
let vs = verts.into_iter().map(|(p, a)| vertex(p.to(), a));
self.mesh.verts.extend(vs);
}

Expand Down Expand Up @@ -167,7 +165,7 @@ impl Builder<()> {
.mesh
.verts
.into_iter()
.map(|v| vertex(tf.apply(&v.pos), v.attrib))
.map(|v| vertex(tf.apply_pt(&v.pos), v.attrib))
.collect(),
};
mesh.into_builder()
Expand Down Expand Up @@ -257,6 +255,7 @@ mod tests {
use core::f32::consts::FRAC_1_SQRT_2;

use crate::geom::vertex;
use crate::math::point::pt3;
use crate::math::vec3;
use crate::prelude::splat;

Expand All @@ -268,9 +267,9 @@ mod tests {
let _: Mesh<()> = Mesh::new(
[Tri([0, 1, 2]), Tri([1, 2, 3])],
[
vertex(vec3(0.0, 0.0, 0.0), ()),
vertex(vec3(1.0, 1.0, 1.0), ()),
vertex(vec3(2.0, 2.0, 2.0), ()),
vertex(pt3(0.0, 0.0, 0.0), ()),
vertex(pt3(1.0, 1.0, 1.0), ()),
vertex(pt3(2.0, 2.0, 2.0), ()),
],
);
}
Expand All @@ -281,9 +280,9 @@ mod tests {
let mut b = Mesh::builder();
b.push_faces([[0, 1, 2], [1, 2, 3]]);
b.push_verts([
(vec3(0.0, 0.0, 0.0), ()),
(vec3(1.0, 1.0, 1.0), ()),
(vec3(2.0, 2.0, 2.0), ()),
(pt3(0.0, 0.0, 0.0), ()),
(pt3(1.0, 1.0, 1.0), ()),
(pt3(2.0, 2.0, 2.0), ()),
]);
_ = b.build();
}
Expand All @@ -295,10 +294,10 @@ mod tests {
let mut b = Mesh::builder();
b.push_faces([[0, 2, 1], [0, 1, 3], [0, 3, 2]]);
b.push_verts([
(vec3(0.0, 0.0, 0.0), ()),
(vec3(1.0, 0.0, 0.0), ()),
(vec3(0.0, 1.0, 0.0), ()),
(vec3(0.0, 0.0, 1.0), ()),
(pt3(0.0, 0.0, 0.0), ()),
(pt3(1.0, 0.0, 0.0), ()),
(pt3(0.0, 1.0, 0.0), ()),
(pt3(0.0, 0.0, 1.0), ()),
]);
let b = b.with_vertex_normals();

Expand Down
14 changes: 14 additions & 0 deletions core/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,22 @@ pub mod approx;
pub mod color;
pub mod float;
pub mod mat;
pub mod point;
pub mod rand;
pub mod space;
pub mod spline;
pub mod vary;
pub mod vec;

pub trait Lerp {
fn lerp(&self, other: &Self, t: f32) -> Self;
}

impl<T> Lerp for T
where
T: Affine<Diff: Linear<Scalar = f32>>,
{
fn lerp(&self, other: &Self, t: f32) -> Self {
self.add(&other.sub(self).mul(t))
}
}
5 changes: 4 additions & 1 deletion core/src/math/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::math::vec::Vector;

#[cfg(feature = "fp")]
use crate::math::float::f32;
use crate::math::vary::ZDiv;
#[cfg(feature = "fp")]
use crate::math::vec::{vec2, vec3, Vec2, Vec3};

Expand Down Expand Up @@ -463,6 +464,8 @@ impl Linear for Angle {
}
}

impl ZDiv for Angle {}

//
// Foreign trait impls
//
Expand Down Expand Up @@ -571,7 +574,6 @@ mod tests {
use core::f32::consts::{PI, TAU};

use crate::assert_approx_eq;
use crate::math::vary::Vary;

use super::*;

Expand Down Expand Up @@ -678,6 +680,7 @@ mod tests {

#[test]
fn varying() {
use crate::math::vary::Vary;
let mut i = degs(45.0).vary(degs(15.0), Some(4));

assert_approx_eq!(i.next(), Some(degs(45.0)));
Expand Down
23 changes: 15 additions & 8 deletions core/src/math/color.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
//! Colors and color spaces.

use core::array;
use core::fmt::{self, Debug, Formatter};
use core::marker::PhantomData;
use core::ops::Index;

use crate::math::float::f32;
use crate::math::space::{Affine, Linear};
use crate::math::vec::Vector;
use core::{
array,
fmt::{self, Debug, Formatter},
marker::PhantomData,
ops::Index,
};

use crate::math::{
float::f32,
space::{Affine, Linear},
vary::ZDiv,
vec::Vector,
};

//
// Types
Expand Down Expand Up @@ -531,6 +536,8 @@ impl<Sp, const DIM: usize> Linear for Color<[f32; DIM], Sp> {
}
}

impl<Sc, Sp, const N: usize> ZDiv for Color<[Sc; N], Sp> where Sc: ZDiv + Copy {}

//
// Foreign trait impls
//
Expand Down
Loading
Loading