Working on RiA-weekend
This commit is contained in:
commit
4ec798cfec
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
target/
|
4
Cargo.lock
generated
Normal file
4
Cargo.lock
generated
Normal file
|
@ -0,0 +1,4 @@
|
|||
[[package]]
|
||||
name = "ria-weekend"
|
||||
version = "0.1.0"
|
||||
|
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "ria-weekend"
|
||||
version = "0.1.0"
|
||||
authors = ["ishanjain28 <ishanjain28@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
41
examples/ppm.rs
Normal file
41
examples/ppm.rs
Normal file
|
@ -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::<u32>().expect("error in parsing input"))
|
||||
.collect::<Vec<u32>>();
|
||||
|
||||
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()),
|
||||
}
|
||||
}
|
3
src/lib.rs
Normal file
3
src/lib.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub mod ppm;
|
||||
pub mod vec3;
|
||||
pub mod ray;
|
16
src/ppm.rs
Normal file
16
src/ppm.rs
Normal file
|
@ -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
|
||||
}
|
24
src/ray.rs
Normal file
24
src/ray.rs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
142
src/vec3.rs
Normal file
142
src/vec3.rs
Normal file
|
@ -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<Vec3> 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<f32> 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<Vec3> 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<f32> 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<usize> for Vec3 {
|
||||
type Output = f32;
|
||||
|
||||
fn index(&self, q: usize) -> &f32 {
|
||||
&self.inner[q]
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user