Compare commits

..

No commits in common. "8aeb643e21f421a1fa02d04fcd53d90bc8af3e46" and "3eca1b1f7b4f44fbe9775509af17169024313a3c" have entirely different histories.

3 changed files with 52 additions and 77 deletions

View File

@ -1,39 +1,39 @@
use crate::math::{Point3, Vector3}; use crate::math::{Point3, Vector3};
pub struct Plane<T> { pub struct Plane {
point: Point3<T>, point: Point3,
normal: Vector3<T>, normal: Vector3,
} }
impl<T> Plane<T> { impl Plane {
pub fn new(point: Point3<T>, normal: Vector3<T>) -> Plane<T> { pub fn new(point: Point3, normal: Vector3) -> Plane {
Plane { point, normal } Plane { point, normal }
} }
} }
pub struct Line<T> { pub struct Line {
point: Point3<T>, point: Point3,
direction: Vector3<T>, direction: Vector3,
} }
pub struct Sphere<T> { pub struct Sphere {
point: Point3<T>, point: Point3,
radius: T, radius: f64,
} }
impl<T> Sphere<T> { impl Sphere {
pub fn new(point: Point3<T>, radius: T) -> Sphere<T> { pub fn new(point: Point3, radius: f64) -> Sphere {
Sphere { point, radius } Sphere { point, radius }
} }
} }
pub enum Geometry<T> { pub enum Geometry {
Sphere(Sphere<T>), Sphere(Sphere),
Plane(Plane<T>), Plane(Plane),
Line(Line<T>), Line(Line),
} }
impl<T> Geometry<T> { impl Geometry {
pub fn draw(&self) { pub fn draw(&self) {
match self { match self {
Geometry::Sphere(_) => { Geometry::Sphere(_) => {

View File

@ -42,18 +42,14 @@ impl Texture<f64> {
} }
} }
struct RenderableGeometry<T> { struct RenderableGeometry {
transform: Mat4x4, transform: Mat4x4,
geometry: Geometry<T>, geometry: Geometry,
texture: Rc<Texture<f64>>, texture: Rc<Texture<f64>>,
} }
impl<T> RenderableGeometry<T> { impl RenderableGeometry {
fn new( fn new(transform: Mat4x4, geometry: Geometry, texture: Rc<Texture<f64>>) -> RenderableGeometry {
transform: Mat4x4,
geometry: Geometry<T>,
texture: Rc<Texture<f64>>,
) -> RenderableGeometry<T> {
RenderableGeometry { RenderableGeometry {
transform, transform,
geometry, geometry,
@ -66,19 +62,19 @@ trait Renderable {
fn render(&self); fn render(&self);
} }
impl<T> Renderable for Sphere<T> { impl Renderable for Sphere {
fn render(&self) { fn render(&self) {
println!("Rendering Sphere!"); println!("Rendering Sphere!");
} }
} }
impl<T> Renderable for Plane<T> { impl Renderable for Plane {
fn render(&self) { fn render(&self) {
println!("Rendering Plane!"); println!("Rendering Plane!");
} }
} }
impl<T> Renderable for Line<T> { impl Renderable for Line {
fn render(&self) { fn render(&self) {
println!("Rendering Line!"); println!("Rendering Line!");
} }
@ -124,7 +120,4 @@ fn main() {
); );
println!("Reference counter = {}", Rc::strong_count(&tex)); println!("Reference counter = {}", Rc::strong_count(&tex));
let a = Vector3::new(2.0, 3.0, 1.0);
let b = 10.0 * a;
} }

View File

@ -1,23 +1,17 @@
use std::ops::{Add, Div, Mul, Sub}; use std::ops::{Add, Div, Mul, Sub};
macro_rules! create_vector { #[derive(Copy, Clone, PartialEq, Debug)]
($name:ident [$($elem:ident),+] ) => { pub struct Vector3<T> {
#[derive(Copy, Clone, PartialEq, Debug)] x: T,
pub struct $name<T> { y: T,
$($elem: T,)* z: T,
}
impl<T> $name<T> {
pub fn new($($elem : T,)*) -> $name<T> {
$name { $($elem,)* }
}
}
};
} }
create_vector! { Vector4 [x, y, z, w] } impl<T> Vector3<T> {
create_vector! { Vector3 [x, y, z] } pub fn new(x: T, y: T, z: T) -> Vector3<T> {
create_vector! { Vector2 [x, y] } Vector3 { x, y, z }
}
}
impl<T> Add for Vector3<T> impl<T> Add for Vector3<T>
where where
@ -52,20 +46,14 @@ where
} }
} }
macro_rules! impl_mul_for_vector3 { /*
($($type:ty)+) => { impl<T> Mul<Vector3<T>> for f64 {
$( type Output = Vector3<T>;
impl Mul<Vector3<$type>> for $type {
type Output = Vector3<$type>;
fn mul(self, rhs: Vector3<$type>) -> Self::Output { fn mul(self, rhs: Vector3<T>) -> Self::Output {
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z) Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
} }
})* }*/
};
}
impl_mul_for_vector3! { i32 f32 f64 }
impl<T> Mul for Vector3<T> impl<T> Mul for Vector3<T>
where where
@ -90,36 +78,30 @@ where
} }
#[derive(Copy, Clone, PartialEq, Debug)] #[derive(Copy, Clone, PartialEq, Debug)]
pub struct Point3<T> { pub struct Point3 {
x: T, x: f64,
y: T, y: f64,
z: T, z: f64,
} }
impl<T> Point3<T> { impl Point3 {
pub fn new(x: T, y: T, z: T) -> Point3<T> { pub fn new(x: f64, y: f64, z: f64) -> Point3 {
Point3 { x, y, z } Point3 { x, y, z }
} }
} }
impl<T> Add<Vector3<T>> for Point3<T> impl Add<Vector3<T>> for Point3 {
where type Output = Point3;
T: Add<Output = T>,
{
type Output = Point3<T>;
fn add(self, rhs: Vector3<T>) -> Self::Output { fn add(self, rhs: Vector3<T>) -> Self::Output {
Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z) Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
} }
} }
impl<T> Sub for Point3<T> impl Sub for Point3 {
where
T: Sub<Output = T>,
{
type Output = Vector3<T>; type Output = Vector3<T>;
fn sub(self, rhs: Point3<T>) -> Self::Output { fn sub(self, rhs: Point3) -> Self::Output {
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z) Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
} }
} }