From f531d75da626d36f9e8c460cf61f4cc69ff4d846 Mon Sep 17 00:00:00 2001 From: gallant Date: Mon, 24 Apr 2023 12:48:21 -0500 Subject: [PATCH] better module organization + scrapping 3d shenanigans cuz karx told me to :sob: --- src/lib.rs | 1 - src/math/mod.rs | 1 - src/math/vec3.rs | 385 ---------------------------------------------- src/space/mesh.rs | 37 ----- src/space/mod.rs | 1 - 5 files changed, 425 deletions(-) delete mode 100644 src/math/vec3.rs delete mode 100644 src/space/mesh.rs delete mode 100644 src/space/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 50642ab..3c9ff7f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,7 +94,6 @@ pub mod math; pub mod physics; -pub mod space; #[cfg(feature = "audio")] use rodio::{self, source::Source, Decoder, OutputStream}; diff --git a/src/math/mod.rs b/src/math/mod.rs index 84df29d..c8d789b 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -1,2 +1 @@ pub mod vec2; -pub mod vec3; diff --git a/src/math/vec3.rs b/src/math/vec3.rs deleted file mode 100644 index edf5b5a..0000000 --- a/src/math/vec3.rs +++ /dev/null @@ -1,385 +0,0 @@ -use std::{ - fmt::Debug, - ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}, -}; - -// Vec3 -/// A set of 3 [`f32`]s representing a location or direction in the 3d plane. -#[derive(Clone, Copy, Default, PartialEq)] -pub struct Vec3 { - /// The x component of the vector. - pub x: f32, - /// The y component of the vector. - pub y: f32, - /// The z component of the vector - pub z: f32, -} - -impl Debug for Vec3 { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("Vec3") - .field(&self.x) - .field(&self.y) - .field(&self.z) - .finish() - } -} - -impl Vec3 { - /// Creates a new `Vec3` with the given x- and y-values. - /// - /// It is often simpler, and preferred, to just write `(x, y).into()`. - #[must_use] - pub const fn new(x: f32, y: f32, z: f32) -> Vec3 { - Self { x, y, z } - } - - /// Gets the squared magnitude of the vector. - /// - /// Useful for comparisons as it is faster to calculate than `magnitude`. - #[must_use] - pub fn sq_magnitude(self) -> f32 { - self.x * self.x + self.y * self.y + self.z * self.z - } - - /// Gets the magnitude of the vector. - #[must_use] - pub fn magnitude(self) -> f32 { - self.sq_magnitude().sqrt() - } - - /// Gets the squared distance from this vector to `rhs`. - /// - /// Useful for comparisons as it is faster to calculate than `dist`. - #[must_use] - pub fn sq_dist(self, rhs: Self) -> f32 { - (self - rhs).sq_magnitude() - } - - /// Gets the distance from this vector to `rhs`. - #[must_use] - pub fn dist(self, rhs: Self) -> f32 { - (self - rhs).magnitude() - } - - /// Normalizes the vector, making its magnitude `1`. - #[must_use] - pub fn normalized(self) -> Self { - self / self.magnitude() - } - - /// Rounds the vector to a [`Vec3Int`]. - /// - /// This uses `as i32` under the hood, and as such comes with all the same unfortunate edge cases. Beware. - #[must_use] - pub fn rounded(self) -> Vec3Int { - #[allow(clippy::cast_possible_truncation)] - Vec3Int { - x: self.x as i32, - y: self.y as i32, - z: self.z as i32, - } - } -} - -impl From<(i32, i32, i32)> for Vec3 { - fn from(v: (i32, i32, i32)) -> Self { - Vec3Int::from(v).to_f32() - } -} - -impl From<(f32, f32, f32)> for Vec3 { - fn from(v: (f32, f32, f32)) -> Self { - Self { - x: v.0, - y: v.1, - z: v.2, - } - } -} - -impl From for (f32, f32, f32) { - fn from(v: Vec3) -> Self { - (v.x, v.y, v.z) - } -} - -impl PartialEq<(i32, i32, i32)> for Vec3 { - fn eq(&self, other: &(i32, i32, i32)) -> bool { - self == &Self::from(*other) - } -} - -impl PartialEq<(f32, f32, f32)> for Vec3 { - fn eq(&self, other: &(f32, f32, f32)) -> bool { - self == &Self::from(*other) - } -} - -// ...and related op impls -impl Neg for Vec3 { - type Output = Self; - - fn neg(self) -> Self::Output { - self * -1.0 - } -} - -impl Add for Vec3 { - type Output = Self; - - fn add(self, rhs: Self) -> Self::Output { - Self { - x: self.x + rhs.x, - y: self.y + rhs.y, - z: self.z + rhs.z, - } - } -} - -impl AddAssign for Vec3 -where - Vec3: Add, -{ - fn add_assign(&mut self, rhs: T) { - *self = *self + rhs; - } -} - -impl Sub for Vec3 -where - Vec3: Add, -{ - type Output = Self; - - fn sub(self, rhs: T) -> Self::Output { - -(-self + rhs) - } -} - -impl SubAssign for Vec3 -where - Vec3: Sub, -{ - fn sub_assign(&mut self, rhs: T) { - *self = *self - rhs; - } -} - -impl Mul for Vec3 { - type Output = Self; - - fn mul(self, rhs: f32) -> Self::Output { - Self { - x: self.x * rhs, - y: self.y * rhs, - z: self.z * rhs, - } - } -} - -impl Div for Vec3 { - type Output = Self; - - fn div(self, rhs: f32) -> Self::Output { - Self { - x: self.x / rhs, - y: self.y / rhs, - z: self.z / rhs, - } - } -} - -impl MulAssign for Vec3 { - fn mul_assign(&mut self, rhs: f32) { - *self = *self * rhs; - } -} - -impl DivAssign for Vec3 { - fn div_assign(&mut self, rhs: f32) { - *self = *self / rhs; - } -} - -// Vec3Int -/// A set of 2 [`i32`]s representing a location or direction in the 2d plane. -#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)] -pub struct Vec3Int { - /// The x component of the vector. - pub x: i32, - /// The y component of the vector. - pub y: i32, - - pub z: i32, -} - -impl Debug for Vec3Int { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("Vec3Int") - .field(&self.x) - .field(&self.y) - .field(&self.z) - .finish() - } -} - -impl Vec3Int { - /// Creates a new `Vec3` with the given x- and y-values. - /// - /// It is often simpler, and preferred, to just write `(x, y).into()`. - #[must_use] - pub const fn new(x: i32, y: i32, z: i32) -> Vec3Int { - Self { x, y, z } - } - - /// Gets the squared magnitude of the vector. - /// - /// Useful for comparisons as it is faster to calculate than `magnitude`. - #[must_use] - pub fn sq_magnitude(self) -> i32 { - self.x * self.x + self.y * self.y + self.z * self.z - } - - /// Gets the magnitude of the vector. - #[must_use] - pub fn magnitude(self) -> f32 { - #[allow(clippy::cast_precision_loss)] - (self.sq_magnitude() as f32).sqrt() - } - - /// Gets the squared distance from this vector to `rhs`. - /// - /// Useful for comparisons as it is faster to calculate than `dist`. - #[must_use] - pub fn sq_dist(self, rhs: Self) -> i32 { - (self - rhs).sq_magnitude() - } - - /// Gets the distance from this vector to `rhs`. - #[must_use] - pub fn dist(self, rhs: Self) -> f32 { - (self - rhs).magnitude() - } - - /// Casts this vector to a [`Vec3`]. - /// - /// This uses `as f32` under the hood, and as such comes with all the same unfortunate edge cases. Beware. - #[must_use] - pub fn to_f32(self) -> Vec3 { - #[allow(clippy::cast_precision_loss)] - Vec3 { - x: self.x as f32, - y: self.y as f32, - z: self.z as f32, - } - } -} - -impl From<(i32, i32, i32)> for Vec3Int { - fn from(v: (i32, i32, i32)) -> Self { - Self { - x: v.0, - y: v.1, - z: v.2, - } - } -} - -impl From for (i32, i32, i32) { - fn from(v: Vec3Int) -> Self { - (v.x, v.y, v.z) - } -} - -impl PartialEq<(i32, i32, i32)> for Vec3Int { - fn eq(&self, other: &(i32, i32, i32)) -> bool { - self == &Self::from(*other) - } -} - -// ...and related op impls -impl Neg for Vec3Int { - type Output = Self; - - fn neg(self) -> Self::Output { - self * -1 - } -} - -impl Add for Vec3Int { - type Output = Self; - - fn add(self, rhs: Self) -> Self::Output { - Self { - x: self.x + rhs.x, - y: self.y + rhs.y, - z: self.z + rhs.z, - } - } -} - -impl AddAssign for Vec3Int -where - Vec3Int: Add, -{ - fn add_assign(&mut self, rhs: T) { - *self = *self + rhs; - } -} - -impl Sub for Vec3Int -where - Vec3Int: Add, -{ - type Output = Self; - - fn sub(self, rhs: T) -> Self::Output { - -(-self + rhs) - } -} - -impl SubAssign for Vec3Int -where - Vec3Int: Sub, -{ - fn sub_assign(&mut self, rhs: T) { - *self = *self - rhs; - } -} - -impl Mul for Vec3Int { - type Output = Self; - - fn mul(self, rhs: i32) -> Self::Output { - Self { - x: self.x * rhs, - y: self.y * rhs, - z: self.z * rhs, - } - } -} - -impl Div for Vec3Int { - type Output = Self; - - fn div(self, rhs: i32) -> Self::Output { - Self { - x: self.x / rhs, - y: self.y / rhs, - z: self.z / rhs, - } - } -} - -impl MulAssign for Vec3Int { - fn mul_assign(&mut self, rhs: i32) { - *self = *self * rhs; - } -} - -impl DivAssign for Vec3Int { - fn div_assign(&mut self, rhs: i32) { - *self = *self / rhs; - } -} diff --git a/src/space/mesh.rs b/src/space/mesh.rs deleted file mode 100644 index fef7cb6..0000000 --- a/src/space/mesh.rs +++ /dev/null @@ -1,37 +0,0 @@ -use crate::math::{vec3::Vec3, vec2::Vec2}; - -//credit to Djuk1c, abstracting your code into a game engine bcuz i can -#[derive(Default, Clone, Copy, Debug)] -pub struct Vertex { - pub pos: Vec3, - pub normal: Vec3, - pub texture: Vec2, - pub color: u32, - pub lit: f32, -} -impl Vertex { - pub fn new(pos: Vec3, normal: Vec3, texture: Vec2, color: u32, lit: f32) -> Self { - Self { pos, normal, texture, color, lit } - } -} - -#[derive(Clone, Debug)] -pub struct Triangle { - pub v: [Vertex; 3], -} -impl Triangle { - pub fn new( - p1: Vertex, - p2: Vertex, - p3: Vertex, - ) -> Self { - Self { - v: [p1, p2, p3] - } - } -} - -pub struct Mesh { - pub triangles: Vec, -} - diff --git a/src/space/mod.rs b/src/space/mod.rs deleted file mode 100644 index cb3e16e..0000000 --- a/src/space/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod mesh;