Compare commits

...
8 Commits
3 changed files with 173 additions and 76 deletions
+33 -17
View File
@@ -1,36 +1,52 @@
use crate::math::{Point3, Vector3};
pub struct Plane {
point: Point3,
normal: Vector3,
pub struct Plane<T> {
point: Point3<T>,
normal: Vector3<T>,
}
impl Plane {
pub fn new(point: Point3, normal: Vector3) -> Plane {
impl<T> Plane<T> {
pub fn new(point: Point3<T>, normal: Vector3<T>) -> Plane<T> {
Plane { point, normal }
}
}
pub struct Line {
point: Point3,
direction: Vector3,
pub struct Line<T> {
point: Point3<T>,
direction: Vector3<T>,
}
pub struct Sphere {
point: Point3,
radius: f64,
pub struct Sphere<T> {
point: Point3<T>,
radius: T,
}
impl Sphere {
pub fn new(point: Point3, radius: f64) -> Sphere {
impl<T> Sphere<T> {
pub fn new(point: Point3<T>, radius: T) -> Sphere<T> {
Sphere { point, radius }
}
}
pub enum Geometry {
Sphere(Sphere),
Plane(Plane),
Line(Line),
pub enum Geometry<T> {
Sphere(Sphere<T>),
Plane(Plane<T>),
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)]
+64 -17
View File
@@ -1,31 +1,31 @@
use hello::geometry::{self, Geometry, Sphere};
use hello::math::{Mat4x4, Point3};
use hello::geometry::{Geometry, Line, Plane, Sphere};
use hello::math::{Mat4x4, Point3, Vector3};
use std::rc::Rc;
#[derive(Clone)]
struct RGB {
red: f64,
green: f64,
blue: f64,
struct RGB<T> {
red: T,
green: T,
blue: T,
}
#[derive(Clone)]
struct Texture {
struct Texture<T> {
width: usize,
height: usize,
pixel: Vec<RGB>,
pixel: Vec<RGB<T>>,
}
impl Texture {
fn load_texture() -> Texture {
impl Texture<f64> {
fn load_texture() -> Texture<f64> {
let width = 640;
let height = 480;
let mut pixel = Vec::new();
for x in 0..width {
for y in 0..height {
for _ in 0..width {
for _ in 0..height {
pixel.push(RGB {
red: 1.0,
green: 1.0,
@@ -42,14 +42,18 @@ impl Texture {
}
}
struct RenderableGeometry {
struct RenderableGeometry<T> {
transform: Mat4x4,
geometry: Geometry,
texture: Rc<Texture>,
geometry: Geometry<T>,
texture: Rc<Texture<f64>>,
}
impl RenderableGeometry {
fn new(transform: Mat4x4, geometry: Geometry, texture: Rc<Texture>) -> RenderableGeometry {
impl<T> RenderableGeometry<T> {
fn new(
transform: Mat4x4,
geometry: Geometry<T>,
texture: Rc<Texture<f64>>,
) -> RenderableGeometry<T> {
RenderableGeometry {
transform,
geometry,
@@ -58,9 +62,49 @@ impl RenderableGeometry {
}
}
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() {
let tex = Rc::new(Texture::load_texture());
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)),
@@ -80,4 +124,7 @@ fn main() {
);
println!("Reference counter = {}", Rc::strong_count(&tex));
let a = Vector3::new(2.0, 3.0, 1.0);
let b = 10.0 * a;
}
+74 -40
View File
@@ -1,91 +1,125 @@
use std::ops::{Add, Div, Mul, Sub};
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Vector3 {
x: f64,
y: f64,
z: f64,
}
impl Vector3 {
pub fn new(x: f64, y: f64, z: f64) -> Vector3 {
Vector3 { x, y, z }
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,)* }
}
}
};
}
impl Add for Vector3 {
type Output = Vector3;
create_vector! { Vector4 [x, y, z, w] }
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)
}
}
impl Sub for Vector3 {
type Output = Vector3;
impl<T> Sub for Vector3<T>
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)
}
}
impl Mul<f64> for Vector3 {
type Output = Vector3;
impl<T> Mul<T> for Vector3<T>
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)
}
}
impl Mul<Vector3> for f64 {
type Output = Vector3;
macro_rules! impl_mul_for_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)
}
})*
};
}
impl Mul for Vector3 {
type Output = f64;
impl_mul_for_vector3! { i32 f32 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
}
}
impl Div<f64> for Vector3 {
type Output = Vector3;
impl<T> Div<T> for Vector3<T>
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)
}
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Point3 {
x: f64,
y: f64,
z: f64,
pub struct Point3<T> {
x: T,
y: T,
z: T,
}
impl Point3 {
pub fn new(x: f64, y: f64, z: f64) -> Point3 {
impl<T> Point3<T> {
pub fn new(x: T, y: T, z: T) -> Point3<T> {
Point3 { x, y, z }
}
}
impl Add<Vector3> for Point3 {
type Output = Point3;
impl<T> Add<Vector3<T>> for Point3<T>
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)
}
}
impl Sub for Point3 {
type Output = Vector3;
impl<T> Sub for Point3<T>
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)
}
}