Compare commits
11 Commits
81205e5b29
...
45cad795a7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
45cad795a7 | ||
|
|
5daaf9e1bc | ||
|
|
bb45906753 | ||
|
|
b7a1f52360 | ||
|
|
b1963c7e6a | ||
|
|
9ef8e08936 | ||
|
|
31e224f9b0 | ||
|
|
07b2fc28d8 | ||
|
|
31c9bda5a2 | ||
|
|
794228dfce | ||
|
|
28df45da67 |
195
src/main.rs
195
src/main.rs
@ -1,91 +1,148 @@
|
|||||||
//#[derive(Copy, Clone)]
|
use std::ops::{Add, Div, Mul, Sub};
|
||||||
struct RGB {
|
use std::collections::
|
||||||
red: u8,
|
|
||||||
green: u8,
|
#[derive(Copy, Clone)]
|
||||||
blue: u8,
|
struct Vector3 {
|
||||||
|
x: f64,
|
||||||
|
y: f64,
|
||||||
|
z: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RGB {
|
impl Vector3 {
|
||||||
fn new(red: u8, green: u8, blue: u8) -> RGB {
|
fn new(x: f64, y: f64, z: f64) -> Vector3 {
|
||||||
RGB { red, green, blue }
|
Vector3 { x, y, z }
|
||||||
}
|
|
||||||
|
|
||||||
fn black() -> RGB {
|
|
||||||
RGB::new(0, 0, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn white() -> RGB {
|
|
||||||
RGB::new(255, 255, 255)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Image {
|
impl Add for Vector3 {
|
||||||
width: usize,
|
type Output = Vector3;
|
||||||
height: usize,
|
|
||||||
data: Vec<RGB>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Image {
|
fn add(self, rhs: Vector3) -> Self::Output {
|
||||||
fn new(width: usize, height: usize, data: Vec<RGB>) -> Image {
|
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
||||||
if width * height != data.len() {
|
|
||||||
panic!("Width and height of image does not match amount of data.");
|
|
||||||
}
|
|
||||||
|
|
||||||
let img = Image {
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
data,
|
|
||||||
};
|
|
||||||
|
|
||||||
img
|
|
||||||
}
|
|
||||||
|
|
||||||
fn white_image(width: usize, height: usize) -> Image {
|
|
||||||
let mut data = Vec::new();
|
|
||||||
|
|
||||||
for i in 0..width * height {
|
|
||||||
data.push(RGB::white());
|
|
||||||
}
|
|
||||||
|
|
||||||
Image::new(width, height, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get(&self, x: usize, y: usize) -> &RGB {
|
|
||||||
self.data.get(y * self.width + x).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_mut(&mut self, x: usize, y: usize) -> &mut RGB {
|
|
||||||
self.data.get_mut(y * self.width + x).unwrap()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Container<'a> {
|
impl Sub for Vector3 {
|
||||||
value: &'a RGB,
|
type Output = Vector3;
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Container<'a> {
|
fn sub(self, rhs: Vector3) -> Self::Output {
|
||||||
fn new(value: &RGB) -> Container {
|
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
||||||
Container { value }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn brighter<'a>(a: &'a RGB, b: &'a RGB) -> &'a RGB {
|
impl Mul<f64> for Vector3 {
|
||||||
if a.red / 3 + a.green / 3 + a.blue / 3 > b.red / 3 + b.green / 3 + b.blue / 3 {
|
type Output = Vector3;
|
||||||
a
|
|
||||||
} else {
|
fn mul(self, rhs: f64) -> Self::Output {
|
||||||
b
|
Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Mul<Vector3> for f64 {
|
||||||
|
type Output = Vector3;
|
||||||
|
|
||||||
|
fn mul(self, rhs: Vector3) -> Self::Output {
|
||||||
|
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mul for Vector3 {
|
||||||
|
type Output = f64;
|
||||||
|
|
||||||
|
fn mul(self, rhs: Vector3) -> Self::Output {
|
||||||
|
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Div<f64> for Vector3 {
|
||||||
|
type Output = Vector3;
|
||||||
|
|
||||||
|
fn div(self, rhs: f64) -> Self::Output {
|
||||||
|
Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
struct Point3 {
|
||||||
|
x: f64,
|
||||||
|
y: f64,
|
||||||
|
z: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Point3 {
|
||||||
|
fn new(x: f64, y: f64, z: f64) -> Point3 {
|
||||||
|
Point3 { x, y, z }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add<Vector3> for Point3 {
|
||||||
|
type Output = Point3;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
fn sub(self, rhs: Point3) -> Self::Output {
|
||||||
|
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Plane {
|
||||||
|
point: Point3,
|
||||||
|
normal: Vector3,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Plane {
|
||||||
|
fn new(point: Point3, normal: Vector3) -> Plane {
|
||||||
|
Plane { point, normal }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Line {
|
||||||
|
point: Point3,
|
||||||
|
direction: Vector3,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Sphere {
|
||||||
|
point: Point3,
|
||||||
|
radius: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sphere {
|
||||||
|
fn new(point: Point3, radius: f64) -> Sphere {
|
||||||
|
Sphere { point, radius }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Geometry {
|
||||||
|
Sphere(Sphere),
|
||||||
|
Plane(Plane),
|
||||||
|
Line(Line),
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let image = Image::white_image(640, 480);
|
let sphere1 = Sphere::new(Point3::new(0.0, 0.0, 0.0), 25.0);
|
||||||
|
let sphere2 = Sphere::new(Point3::new(1.0, -45.4, 3.55), 2.0);
|
||||||
|
let sphere3 = Sphere::new(Point3::new(3.0, 2.0, 1.0), 0.5);
|
||||||
|
|
||||||
let black = RGB::black();
|
let plane1 = Plane::new(Point3::new(1.0, 2.0, 3.0), Vector3::new(0.0, 1.0, 0.0));
|
||||||
|
|
||||||
let c1 = image.get(0, 0);
|
let mut v = Vec::new();
|
||||||
//let c2 = image.get(1, 1);
|
|
||||||
|
|
||||||
let brightest = brighter(c1, &black);
|
v.push(Geometry::Sphere(sphere1));
|
||||||
|
v.push(Geometry::Plane(plane1));
|
||||||
|
v.push(Geometry::Sphere(sphere2));
|
||||||
|
v.push(Geometry::Sphere(sphere3));
|
||||||
|
|
||||||
println!("{}", brightest.red);
|
for geo in v {
|
||||||
|
match geo {
|
||||||
|
Geometry::Sphere(s) => println!("A Sphere"),
|
||||||
|
Geometry::Plane(p) => println!("A Plane"),
|
||||||
|
Geometry::Line(l) => println!("A Line"),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user