commit 4ec798cfec50fd1aefe0c2994e4be2c569724f4a Author: ishanjain28 Date: Tue Jan 22 22:37:51 2019 +0530 Working on RiA-weekend diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..54777c4 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "ria-weekend" +version = "0.1.0" + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8505789 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "ria-weekend" +version = "0.1.0" +authors = ["ishanjain28 "] +edition = "2018" + +[dependencies] diff --git a/examples/ppm.rs b/examples/ppm.rs new file mode 100644 index 0000000..8422cd0 --- /dev/null +++ b/examples/ppm.rs @@ -0,0 +1,41 @@ +extern crate ria_weekend; + +use ria_weekend::ppm; +use std::error::Error; +use std::fs::File; +use std::{io, io::Write}; + +fn main() { + let mut height = 1080; + let mut width = 1920; + + println!("Enter width and height seperated by space"); + + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("error in reading input"); + + let input = input + .trim() + .split(" ") + .map(|v| v.parse::().expect("error in parsing input")) + .collect::>(); + + if input.len() >= 2 { + height = input[1]; + width = input[0]; + } + let file_name = format!("ppm_sample{}x{}.ppm", width, height); + let ppm_sample = ppm::create_sample(height, width); + + let mut file = match File::create(&file_name) { + Ok(file) => file, + Err(e) => panic!("couldn't create {}: {}", file_name, e.description()), + }; + + match file.write_all(ppm_sample.as_bytes()) { + Ok(_) => println!("Succesfully wrote to {}", file_name), + Err(e) => panic!("couldn't write to {}: {}", file_name, e.description()), + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..43dbe67 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,3 @@ +pub mod ppm; +pub mod vec3; +pub mod ray; diff --git a/src/ppm.rs b/src/ppm.rs new file mode 100644 index 0000000..39b7133 --- /dev/null +++ b/src/ppm.rs @@ -0,0 +1,16 @@ +use crate::vec3::Vec3; + +pub fn create_sample(h: u32, w: u32) -> String { + let mut buf = format!("P3\n{} {}\n255\n", w, h); + for j in (0..h).rev() { + for i in 0..w { + let color = Vec3::new((i as f32) / (w as f32), (j as f32) / (h as f32), 0.5_f32); + + let ir = (255.99 * color[0]) as u8; + let ig = (255.99 * color[1]) as u8; + let ib = (255.99 * color[2]) as u8; + buf.push_str(&format!("{} {} {}\n", ir, ig, ib)); + } + } + buf +} diff --git a/src/ray.rs b/src/ray.rs new file mode 100644 index 0000000..39b763f --- /dev/null +++ b/src/ray.rs @@ -0,0 +1,24 @@ +use crate::vec3::Vec3; + +pub struct Ray { + a: Vec3, + b: Vec3, +} + +impl Ray { + pub const fn new(a: Vec3, b: Vec3) -> Ray { + Ray { a, b } + } + + pub const fn origin(&self) -> &Vec3 { + return &self.a; + } + + pub const fn direction(&self) -> &Vec3 { + return &self.b; + } + + pub const fn point_at_diameter(&self, t: f32) -> Vec3 { + return self.a + t * self.b; + } +} diff --git a/src/vec3.rs b/src/vec3.rs new file mode 100644 index 0000000..99e4581 --- /dev/null +++ b/src/vec3.rs @@ -0,0 +1,142 @@ +use std::ops::{Add, Div, Index, Mul, Sub}; + +pub struct Vec3 { + inner: [f32; 3], +} + +impl Vec3 { + pub fn new(a: f32, b: f32, c: f32) -> Vec3 { + Vec3 { inner: [a, b, c] } + } + + pub fn x(&self) -> f32 { + self.inner[0] + } + pub fn y(&self) -> f32 { + self.inner[1] + } + pub fn z(&self) -> f32 { + self.inner[2] + } + pub fn r(&self) -> f32 { + self.inner[0] + } + pub fn g(&self) -> f32 { + self.inner[1] + } + pub fn b(&self) -> f32 { + self.inner[2] + } + + pub fn length(&self) -> f32 { + (self.inner[0] * self.inner[0] + + self.inner[1] * self.inner[1] + + self.inner[2] * self.inner[2]) + .sqrt() + } + + pub fn sq_len(&self) -> f32 { + self.inner[0] * self.inner[0] + + self.inner[1] * self.inner[1] + + self.inner[2] * self.inner[2] + } + + pub fn dot(&self, v: &Vec3) -> f32 { + self.inner[0] * v.inner[0] + self.inner[1] * v.inner[1] + self.inner[2] * v.inner[2] + } + + pub fn cross(&self, v: &Vec3) -> Vec3 { + Vec3 { + inner: [ + self.inner[1] * v.inner[2] - self.inner[2] * v.inner[1], + self.inner[2] * v.inner[0] - self.inner[0] * v.inner[2], + self.inner[0] * v.inner[1] - self.inner[1] * v.inner[0], + ], + } + } +} + +impl Add for Vec3 { + type Output = Vec3; + + fn add(self, o: Vec3) -> Vec3 { + Vec3 { + inner: [ + self.inner[0] + o.inner[0], + self.inner[1] + o.inner[1], + self.inner[2] + o.inner[2], + ], + } + } +} + +impl Sub for Vec3 { + type Output = Vec3; + + fn sub(self, o: Vec3) -> Vec3 { + Vec3 { + inner: [ + self.inner[0] - o.inner[0], + self.inner[1] - o.inner[1], + self.inner[2] - o.inner[2], + ], + } + } +} + +impl Mul for Vec3 { + type Output = Vec3; + + fn mul(self, o: Vec3) -> Vec3 { + Vec3 { + inner: [ + self.inner[0] * o.inner[0], + self.inner[1] * o.inner[1], + self.inner[2] * o.inner[2], + ], + } + } +} + +impl Mul for Vec3 { + type Output = Vec3; + + fn mul(self, o: f32) -> Vec3 { + Vec3 { + inner: [self.inner[0] * o, self.inner[1] * o, self.inner[2] * o], + } + } +} + +impl Div for Vec3 { + type Output = Vec3; + + fn div(self, o: Vec3) -> Vec3 { + Vec3 { + inner: [ + self.inner[0] / o.inner[0], + self.inner[1] / o.inner[1], + self.inner[2] / o.inner[2], + ], + } + } +} + +impl Div for Vec3 { + type Output = Vec3; + + fn div(self, o: f32) -> Vec3 { + let o = 1.0 / o; + Vec3 { + inner: [self.inner[0] * o, self.inner[1] * o, self.inner[2] * o], + } + } +} + +impl Index for Vec3 { + type Output = f32; + + fn index(&self, q: usize) -> &f32 { + &self.inner[q] + } +}