From 8296be848319eecd43f94900d4d12414ec189166 Mon Sep 17 00:00:00 2001 From: Julian T Date: Wed, 3 Feb 2021 15:34:17 +0100 Subject: Add simple perspective camera --- src/core/vector3.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'src/core/vector3.rs') diff --git a/src/core/vector3.rs b/src/core/vector3.rs index 915765d..6f4d6ab 100644 --- a/src/core/vector3.rs +++ b/src/core/vector3.rs @@ -2,7 +2,8 @@ //! //! Also add more 3d math things needed for shading and 3d calculations. use crate::{Float, Number}; -use std::ops::{Sub, Add, DivAssign}; +use std::ops::{Mul, Sub, Add, DivAssign}; +use std::fmt; #[derive(Clone, Copy)] pub struct Vector3 { @@ -49,6 +50,17 @@ impl Add for Vector3 { } } +impl Mul for Vector3 { + type Output = Self; + fn mul(self, op: T) -> Self::Output { + Self::Output::new_xyz( + self.x * op, + self.y * op, + self.z * op, + ) + } +} + impl DivAssign for Vector3 { fn div_assign(&mut self, op: T) { self.x /= op; @@ -57,6 +69,12 @@ impl DivAssign for Vector3 { } } +impl fmt::Display for Vector3 { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_fmt(format_args!("[{}, {}, {}]", self.x, self.y, self.z)) + } +} + impl Vector3f { /// Calculates the length times itself /// @@ -97,4 +115,13 @@ impl Vector3f { new.norm_in(); new } + + pub fn cross(&self, op: &Self) -> Self { + Self::new_xyz( + self.y * op.z - self.z * op.y, + self.z * op.x - self.x * op.z, + self.x * op.y - self.y * op.x, + ) + + } } -- cgit v1.2.3