Compare commits
No commits in common. "8aeb643e21f421a1fa02d04fcd53d90bc8af3e46" and "3eca1b1f7b4f44fbe9775509af17169024313a3c" have entirely different histories.
8aeb643e21
...
3eca1b1f7b
@ -1,39 +1,39 @@
|
||||
use crate::math::{Point3, Vector3};
|
||||
|
||||
pub struct Plane<T> {
|
||||
point: Point3<T>,
|
||||
normal: Vector3<T>,
|
||||
pub struct Plane {
|
||||
point: Point3,
|
||||
normal: Vector3,
|
||||
}
|
||||
|
||||
impl<T> Plane<T> {
|
||||
pub fn new(point: Point3<T>, normal: Vector3<T>) -> Plane<T> {
|
||||
impl Plane {
|
||||
pub fn new(point: Point3, normal: Vector3) -> Plane {
|
||||
Plane { point, normal }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Line<T> {
|
||||
point: Point3<T>,
|
||||
direction: Vector3<T>,
|
||||
pub struct Line {
|
||||
point: Point3,
|
||||
direction: Vector3,
|
||||
}
|
||||
|
||||
pub struct Sphere<T> {
|
||||
point: Point3<T>,
|
||||
radius: T,
|
||||
pub struct Sphere {
|
||||
point: Point3,
|
||||
radius: f64,
|
||||
}
|
||||
|
||||
impl<T> Sphere<T> {
|
||||
pub fn new(point: Point3<T>, radius: T) -> Sphere<T> {
|
||||
impl Sphere {
|
||||
pub fn new(point: Point3, radius: f64) -> Sphere {
|
||||
Sphere { point, radius }
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Geometry<T> {
|
||||
Sphere(Sphere<T>),
|
||||
Plane(Plane<T>),
|
||||
Line(Line<T>),
|
||||
pub enum Geometry {
|
||||
Sphere(Sphere),
|
||||
Plane(Plane),
|
||||
Line(Line),
|
||||
}
|
||||
|
||||
impl<T> Geometry<T> {
|
||||
impl Geometry {
|
||||
pub fn draw(&self) {
|
||||
match self {
|
||||
Geometry::Sphere(_) => {
|
||||
|
||||
21
src/main.rs
21
src/main.rs
@ -42,18 +42,14 @@ impl Texture<f64> {
|
||||
}
|
||||
}
|
||||
|
||||
struct RenderableGeometry<T> {
|
||||
struct RenderableGeometry {
|
||||
transform: Mat4x4,
|
||||
geometry: Geometry<T>,
|
||||
geometry: Geometry,
|
||||
texture: Rc<Texture<f64>>,
|
||||
}
|
||||
|
||||
impl<T> RenderableGeometry<T> {
|
||||
fn new(
|
||||
transform: Mat4x4,
|
||||
geometry: Geometry<T>,
|
||||
texture: Rc<Texture<f64>>,
|
||||
) -> RenderableGeometry<T> {
|
||||
impl RenderableGeometry {
|
||||
fn new(transform: Mat4x4, geometry: Geometry, texture: Rc<Texture<f64>>) -> RenderableGeometry {
|
||||
RenderableGeometry {
|
||||
transform,
|
||||
geometry,
|
||||
@ -66,19 +62,19 @@ trait Renderable {
|
||||
fn render(&self);
|
||||
}
|
||||
|
||||
impl<T> Renderable for Sphere<T> {
|
||||
impl Renderable for Sphere {
|
||||
fn render(&self) {
|
||||
println!("Rendering Sphere!");
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Renderable for Plane<T> {
|
||||
impl Renderable for Plane {
|
||||
fn render(&self) {
|
||||
println!("Rendering Plane!");
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Renderable for Line<T> {
|
||||
impl Renderable for Line {
|
||||
fn render(&self) {
|
||||
println!("Rendering Line!");
|
||||
}
|
||||
@ -124,7 +120,4 @@ fn main() {
|
||||
);
|
||||
|
||||
println!("Reference counter = {}", Rc::strong_count(&tex));
|
||||
|
||||
let a = Vector3::new(2.0, 3.0, 1.0);
|
||||
let b = 10.0 * a;
|
||||
}
|
||||
|
||||
72
src/math.rs
72
src/math.rs
@ -1,23 +1,17 @@
|
||||
use std::ops::{Add, Div, Mul, Sub};
|
||||
|
||||
macro_rules! create_vector {
|
||||
($name:ident [$($elem:ident),+] ) => {
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
pub struct $name<T> {
|
||||
$($elem: T,)*
|
||||
}
|
||||
|
||||
impl<T> $name<T> {
|
||||
pub fn new($($elem : T,)*) -> $name<T> {
|
||||
$name { $($elem,)* }
|
||||
}
|
||||
}
|
||||
};
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
pub struct Vector3<T> {
|
||||
x: T,
|
||||
y: T,
|
||||
z: T,
|
||||
}
|
||||
|
||||
create_vector! { Vector4 [x, y, z, w] }
|
||||
create_vector! { Vector3 [x, y, z] }
|
||||
create_vector! { Vector2 [x, y] }
|
||||
impl<T> Vector3<T> {
|
||||
pub fn new(x: T, y: T, z: T) -> Vector3<T> {
|
||||
Vector3 { x, y, z }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Add for Vector3<T>
|
||||
where
|
||||
@ -52,20 +46,14 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_mul_for_vector3 {
|
||||
($($type:ty)+) => {
|
||||
$(
|
||||
impl Mul<Vector3<$type>> for $type {
|
||||
type Output = Vector3<$type>;
|
||||
/*
|
||||
impl<T> Mul<Vector3<T>> for f64 {
|
||||
type Output = Vector3<T>;
|
||||
|
||||
fn mul(self, rhs: Vector3<$type>) -> Self::Output {
|
||||
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
|
||||
}
|
||||
})*
|
||||
};
|
||||
}
|
||||
|
||||
impl_mul_for_vector3! { i32 f32 f64 }
|
||||
fn mul(self, rhs: Vector3<T>) -> Self::Output {
|
||||
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
|
||||
}
|
||||
}*/
|
||||
|
||||
impl<T> Mul for Vector3<T>
|
||||
where
|
||||
@ -90,36 +78,30 @@ where
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
pub struct Point3<T> {
|
||||
x: T,
|
||||
y: T,
|
||||
z: T,
|
||||
pub struct Point3 {
|
||||
x: f64,
|
||||
y: f64,
|
||||
z: f64,
|
||||
}
|
||||
|
||||
impl<T> Point3<T> {
|
||||
pub fn new(x: T, y: T, z: T) -> Point3<T> {
|
||||
impl Point3 {
|
||||
pub fn new(x: f64, y: f64, z: f64) -> Point3 {
|
||||
Point3 { x, y, z }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Add<Vector3<T>> for Point3<T>
|
||||
where
|
||||
T: Add<Output = T>,
|
||||
{
|
||||
type Output = Point3<T>;
|
||||
impl Add<Vector3<T>> for Point3 {
|
||||
type Output = Point3;
|
||||
|
||||
fn add(self, rhs: Vector3<T>) -> Self::Output {
|
||||
Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Sub for Point3<T>
|
||||
where
|
||||
T: Sub<Output = T>,
|
||||
{
|
||||
impl Sub for Point3 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user