Compare commits

..

No commits in common. "3eca1b1f7b4f44fbe9775509af17169024313a3c" and "36c16d053e46ec6ab94f6847533db1196a74a427" have entirely different histories.

3 changed files with 47 additions and 100 deletions

View File

@ -34,16 +34,16 @@ pub enum Geometry {
}
impl Geometry {
pub fn draw(&self) {
pub fn greet(&self) {
match self {
Geometry::Sphere(_) => {
println!("Drawing Sphere!");
println!("Sphere!");
}
Geometry::Line(_) => {
println!("Drawing Line!");
println!("Line!");
}
Geometry::Plane(_) => {
println!("Drawing Plane!");
println!("Plane!");
}
}
}

View File

@ -1,31 +1,31 @@
use hello::geometry::{Geometry, Line, Plane, Sphere};
use hello::math::{Mat4x4, Point3, Vector3};
use hello::geometry::{self, Geometry, Sphere};
use hello::math::{Mat4x4, Point3};
use std::rc::Rc;
#[derive(Clone)]
struct RGB<T> {
red: T,
green: T,
blue: T,
struct RGB {
red: f64,
green: f64,
blue: f64,
}
#[derive(Clone)]
struct Texture<T> {
struct Texture {
width: usize,
height: usize,
pixel: Vec<RGB<T>>,
pixel: Vec<RGB>,
}
impl Texture<f64> {
fn load_texture() -> Texture<f64> {
impl Texture {
fn load_texture() -> Texture {
let width = 640;
let height = 480;
let mut pixel = Vec::new();
for _ in 0..width {
for _ in 0..height {
for x in 0..width {
for y in 0..height {
pixel.push(RGB {
red: 1.0,
green: 1.0,
@ -45,11 +45,11 @@ impl Texture<f64> {
struct RenderableGeometry {
transform: Mat4x4,
geometry: Geometry,
texture: Rc<Texture<f64>>,
texture: Rc<Texture>,
}
impl RenderableGeometry {
fn new(transform: Mat4x4, geometry: Geometry, texture: Rc<Texture<f64>>) -> RenderableGeometry {
fn new(transform: Mat4x4, geometry: Geometry, texture: Rc<Texture>) -> RenderableGeometry {
RenderableGeometry {
transform,
geometry,
@ -58,48 +58,11 @@ impl RenderableGeometry {
}
}
trait Renderable {
fn render(&self);
}
impl Renderable for Sphere {
fn render(&self) {
println!("Rendering Sphere!");
}
}
impl Renderable for Plane {
fn render(&self) {
println!("Rendering Plane!");
}
}
impl Renderable for Line {
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();
}
sphere.greet();
let geo1 = RenderableGeometry::new(
Mat4x4::ident(),

View File

@ -1,78 +1,62 @@
use std::ops::{Add, Div, Mul, Sub};
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Vector3<T> {
x: T,
y: T,
z: T,
pub struct Vector3 {
x: f64,
y: f64,
z: f64,
}
impl<T> Vector3<T> {
pub fn new(x: T, y: T, z: T) -> Vector3<T> {
impl Vector3 {
pub fn new(x: f64, y: f64, z: f64) -> Vector3 {
Vector3 { x, y, z }
}
}
impl<T> Add for Vector3<T>
where
T: Add<Output = T>,
{
type Output = Vector3<T>;
impl Add for Vector3 {
type Output = Vector3;
fn add(self, rhs: Vector3<T>) -> Self::Output {
fn add(self, rhs: Vector3) -> Self::Output {
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
}
}
impl<T> Sub for Vector3<T>
where
T: Sub<Output = T>,
{
type Output = Vector3<T>;
impl Sub for Vector3 {
type Output = Vector3;
fn sub(self, rhs: Vector3<T>) -> Self::Output {
fn sub(self, rhs: Vector3) -> Self::Output {
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}
impl<T> Mul<T> for Vector3<T>
where
T: Mul<Output = T> + Clone + Copy,
{
type Output = Vector3<T>;
impl Mul<f64> for Vector3 {
type Output = Vector3;
fn mul(self, rhs: T) -> Self::Output {
fn mul(self, rhs: f64) -> Self::Output {
Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs)
}
}
/*
impl<T> Mul<Vector3<T>> for f64 {
type Output = Vector3<T>;
impl Mul<Vector3> for f64 {
type Output = Vector3;
fn mul(self, rhs: Vector3<T>) -> Self::Output {
fn mul(self, rhs: Vector3) -> Self::Output {
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
}
}*/
}
impl<T> Mul for Vector3<T>
where
T: Mul<Output = T> + Add<Output = T>,
{
type Output = T;
impl Mul for Vector3 {
type Output = f64;
fn mul(self, rhs: Vector3<T>) -> Self::Output {
fn mul(self, rhs: Vector3) -> Self::Output {
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
}
}
impl<T> Div<T> for Vector3<T>
where
T: Div<Output = T> + Copy + Clone,
{
type Output = Vector3<T>;
impl Div<f64> for Vector3 {
type Output = Vector3;
fn div(self, rhs: T) -> Self::Output {
fn div(self, rhs: f64) -> Self::Output {
Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs)
}
}
@ -90,16 +74,16 @@ impl Point3 {
}
}
impl Add<Vector3<T>> for Point3 {
impl Add<Vector3> for Point3 {
type Output = Point3;
fn add(self, rhs: Vector3<T>) -> Self::Output {
fn add(self, rhs: Vector3) -> Self::Output {
Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
}
}
impl Sub for Point3 {
type Output = Vector3<T>;
type Output = Vector3;
fn sub(self, rhs: Point3) -> Self::Output {
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)