Compare commits

..
13 Commits
4 changed files with 246 additions and 64 deletions
+33 -17
View File
@@ -1,36 +1,52 @@
use crate::math::{Point3, Vector3}; use crate::math::{Point3, Vector3};
pub struct Plane { pub struct Plane<T> {
point: Point3, point: Point3<T>,
normal: Vector3, normal: Vector3<T>,
} }
impl Plane { impl<T> Plane<T> {
pub fn new(point: Point3, normal: Vector3) -> Plane { pub fn new(point: Point3<T>, normal: Vector3<T>) -> Plane<T> {
Plane { point, normal } Plane { point, normal }
} }
} }
pub struct Line { pub struct Line<T> {
point: Point3, point: Point3<T>,
direction: Vector3, direction: Vector3<T>,
} }
pub struct Sphere { pub struct Sphere<T> {
point: Point3, point: Point3<T>,
radius: f64, radius: T,
} }
impl Sphere { impl<T> Sphere<T> {
pub fn new(point: Point3, radius: f64) -> Sphere { pub fn new(point: Point3<T>, radius: T) -> Sphere<T> {
Sphere { point, radius } Sphere { point, radius }
} }
} }
pub enum Geometry { pub enum Geometry<T> {
Sphere(Sphere), Sphere(Sphere<T>),
Plane(Plane), Plane(Plane<T>),
Line(Line), Line(Line<T>),
}
impl<T> Geometry<T> {
pub fn draw(&self) {
match self {
Geometry::Sphere(_) => {
println!("Drawing Sphere!");
}
Geometry::Line(_) => {
println!("Drawing Line!");
}
Geometry::Plane(_) => {
println!("Drawing Plane!");
}
}
}
} }
#[cfg(test)] #[cfg(test)]
+1 -1
View File
@@ -1,2 +1,2 @@
mod geometry; pub mod geometry;
pub mod math; pub mod math;
+126 -4
View File
@@ -1,8 +1,130 @@
use hello::math::Point3; use hello::geometry::{Geometry, Line, Plane, Sphere};
use hello::math::{Mat4x4, Point3, Vector3};
use std::rc::Rc;
#[derive(Clone)]
struct RGB<T> {
red: T,
green: T,
blue: T,
}
#[derive(Clone)]
struct Texture<T> {
width: usize,
height: usize,
pixel: Vec<RGB<T>>,
}
impl Texture<f64> {
fn load_texture() -> Texture<f64> {
let width = 640;
let height = 480;
let mut pixel = Vec::new();
for _ in 0..width {
for _ in 0..height {
pixel.push(RGB {
red: 1.0,
green: 1.0,
blue: 1.0,
});
}
}
Texture {
width,
height,
pixel,
}
}
}
struct RenderableGeometry<T> {
transform: Mat4x4,
geometry: Geometry<T>,
texture: Rc<Texture<f64>>,
}
impl<T> RenderableGeometry<T> {
fn new(
transform: Mat4x4,
geometry: Geometry<T>,
texture: Rc<Texture<f64>>,
) -> RenderableGeometry<T> {
RenderableGeometry {
transform,
geometry,
texture,
}
}
}
trait Renderable {
fn render(&self);
}
impl<T> Renderable for Sphere<T> {
fn render(&self) {
println!("Rendering Sphere!");
}
}
impl<T> Renderable for Plane<T> {
fn render(&self) {
println!("Rendering Plane!");
}
}
impl<T> Renderable for Line<T> {
fn render(&self) {
println!("Rendering Line!");
}
}
fn main() { fn main() {
let p1 = Point3::new(0.0, 0.0, 0.0); let tex = Rc::new(Texture::load_texture());
let p2 = Point3::new(0.0, 0.0, 0.0);
let v = p1 - p2; let sphere = Geometry::Sphere(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0));
let geometries = vec![sphere];
for g in geometries {
g.draw();
}
let mut renderables: Vec<Box<dyn Renderable>> = Vec::new();
renderables.push(Box::new(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0)));
renderables.push(Box::new(Plane::new(
Point3::new(0.0, 0.0, 0.0),
Vector3::new(0.0, 1.0, 0.0),
)));
for r in renderables {
r.render();
}
let geo1 = RenderableGeometry::new(
Mat4x4::ident(),
Geometry::Sphere(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0)),
tex.clone(),
);
let geo2 = RenderableGeometry::new(
Mat4x4::ident(),
Geometry::Sphere(Sphere::new(Point3::new(-2.0, 0.0, 0.0), 1.0)),
tex.clone(),
);
let geo3 = RenderableGeometry::new(
Mat4x4::ident(),
Geometry::Sphere(Sphere::new(Point3::new(2.0, 0.0, 0.0), 1.0)),
tex.clone(),
);
println!("Reference counter = {}", Rc::strong_count(&tex));
let a = Vector3::new(2.0, 3.0, 1.0);
let b = 10.0 * a;
} }
+81 -37
View File
@@ -1,95 +1,139 @@
use std::ops::{Add, Div, Mul, Sub}; use std::ops::{Add, Div, Mul, Sub};
macro_rules! create_vector {
($name:ident [$($elem:ident),+] ) => {
#[derive(Copy, Clone, PartialEq, Debug)] #[derive(Copy, Clone, PartialEq, Debug)]
pub struct Vector3 { pub struct $name<T> {
x: f64, $($elem: T,)*
y: f64,
z: f64,
} }
impl Vector3 { impl<T> $name<T> {
pub fn new(x: f64, y: f64, z: f64) -> Vector3 { pub fn new($($elem : T,)*) -> $name<T> {
Vector3 { x, y, z } $name { $($elem,)* }
} }
} }
};
}
impl Add for Vector3 { create_vector! { Vector4 [x, y, z, w] }
type Output = Vector3; create_vector! { Vector3 [x, y, z] }
create_vector! { Vector2 [x, y] }
fn add(self, rhs: Vector3) -> Self::Output { impl<T> Add for Vector3<T>
where
T: Add<Output = T>,
{
type Output = Vector3<T>;
fn add(self, rhs: Vector3<T>) -> 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)
} }
} }
impl Sub for Vector3 { impl<T> Sub for Vector3<T>
type Output = Vector3; where
T: Sub<Output = T>,
{
type Output = Vector3<T>;
fn sub(self, rhs: Vector3) -> Self::Output { fn sub(self, rhs: Vector3<T>) -> 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)
} }
} }
impl Mul<f64> for Vector3 { impl<T> Mul<T> for Vector3<T>
type Output = Vector3; where
T: Mul<Output = T> + Clone + Copy,
{
type Output = Vector3<T>;
fn mul(self, rhs: f64) -> Self::Output { fn mul(self, rhs: T) -> Self::Output {
Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs) Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs)
} }
} }
impl Mul<Vector3> for f64 { macro_rules! impl_mul_for_vector3 {
type Output = Vector3; ($($type:ty)+) => {
$(
impl Mul<Vector3<$type>> for $type {
type Output = Vector3<$type>;
fn mul(self, rhs: Vector3) -> Self::Output { fn mul(self, rhs: Vector3<$type>) -> 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 { impl_mul_for_vector3! { i32 f32 f64 }
type Output = f64;
fn mul(self, rhs: Vector3) -> Self::Output { impl<T> Mul for Vector3<T>
where
T: Mul<Output = T> + Add<Output = T>,
{
type Output = T;
fn mul(self, rhs: Vector3<T>) -> Self::Output {
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
} }
} }
impl Div<f64> for Vector3 { impl<T> Div<T> for Vector3<T>
type Output = Vector3; where
T: Div<Output = T> + Copy + Clone,
{
type Output = Vector3<T>;
fn div(self, rhs: f64) -> Self::Output { fn div(self, rhs: T) -> Self::Output {
Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs) Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs)
} }
} }
#[derive(Copy, Clone, PartialEq, Debug)] #[derive(Copy, Clone, PartialEq, Debug)]
pub struct Point3 { pub struct Point3<T> {
x: f64, x: T,
y: f64, y: T,
z: f64, z: T,
} }
impl Point3 { impl<T> Point3<T> {
pub fn new(x: f64, y: f64, z: f64) -> Point3 { pub fn new(x: T, y: T, z: T) -> Point3<T> {
Point3 { x, y, z } Point3 { x, y, z }
} }
} }
impl Add<Vector3> for Point3 { impl<T> Add<Vector3<T>> for Point3<T>
type Output = Point3; where
T: Add<Output = T>,
{
type Output = Point3<T>;
fn add(self, rhs: Vector3) -> 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 Sub for Point3 { impl<T> Sub for Point3<T>
type Output = Vector3; where
T: Sub<Output = T>,
{
type Output = Vector3<T>;
fn sub(self, rhs: Point3) -> Self::Output { fn sub(self, rhs: Point3<T>) -> 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)
} }
} }
pub struct Mat4x4 {
v: f64,
}
impl Mat4x4 {
pub fn ident() -> Mat4x4 {
Mat4x4 { v: 0.0 }
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;