more new stuff

This commit is contained in:
Jonathan Flueren 2022-04-20 18:59:29 +02:00
parent 87867df2d8
commit 91876b66dd
7 changed files with 48246 additions and 498238 deletions

90004
image.pp

File diff suppressed because it is too large Load diff

366372
image.ppm

File diff suppressed because it is too large Load diff

90004
image2.ppm

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,13 @@
use std::io::Write;
use super::Color; use super::Color;
use std::io::Write;
pub fn write_color(out: &mut impl Write, pixel_color: Color) { pub fn write_color(out: &mut impl Write, pixel_color: Color) {
write!(out, "{} {} {}\n", (255.999*pixel_color.x()) as u32, (255.999*pixel_color.y()) as u32, (255.999*pixel_color.z()) as u32).unwrap(); write!(
} out,
"{} {} {}\n",
(255.999 * pixel_color.x()) as u32,
(255.999 * pixel_color.y()) as u32,
(255.999 * pixel_color.z()) as u32
)
.unwrap();
}

View file

@ -1,49 +1,48 @@
mod vec3;
mod color; mod color;
mod ray; mod ray;
use vec3::Vec3; mod vec3;
use vec3::Color;
use vec3::Point3;
use ray::Ray; use ray::Ray;
use std::env; use std::env;
use std::fs::File; use std::fs::File;
use std::io::BufWriter; use std::io::BufWriter;
use std::io::Write; use std::io::Write;
use vec3::Color;
use vec3::Point3;
use vec3::Vec3;
fn hit_sphere(center: &Point3, radius: f64, r: &Ray) -> f64 { fn hit_sphere(center: &Point3, radius: f64, r: &Ray) -> f64 {
let oc = r.origin() - *center; let oc = r.origin() - *center;
let a = Vec3::dot(r.direction(), r.direction()); let a = Vec3::dot(r.direction(), r.direction());
let b = 2.0 * Vec3::dot(oc, r.direction()); let b = 2.0 * Vec3::dot(oc, r.direction());
let c = Vec3::dot(oc, oc) - radius*radius; let c = Vec3::dot(oc, oc) - radius * radius;
let discriminant = b*b - 4.0*a*c; let discriminant = b * b - 4.0 * a * c;
if discriminant < 0.0 { if discriminant < 0.0 {
return -1.0; return -1.0;
} else { } else {
return (-b - discriminant.sqrt()) / (2.0*a); return (-b - discriminant.sqrt()) / (2.0 * a);
} }
} }
fn ray_color(r: &Ray) -> Color{ fn ray_color(r: &Ray) -> Color {
let t = hit_sphere(&Point3::new(0.0, 0.0, -1.0), 0.5, r); let t = hit_sphere(&Point3::new(0.0, 0.0, -1.0), 0.5, r);
if t > 0.0 { if t > 0.0 {
let n = Vec3::unit_vector(r.at(t) - Vec3::new(0.0, 0.0, -1.0)); let n = Vec3::unit_vector(r.at(t) - Vec3::new(0.0, 0.0, -1.0));
return 0.5*Color::new(n.x()+1.0, n.y()+1.0, n.z()+1.0); return 0.5 * Color::new(n.x() + 1.0, n.y() + 1.0, n.z() + 1.0);
} }
let unit_direction = r.direction(); let unit_direction = r.direction();
let t = 0.5*(unit_direction.y() + 1.0); let t = 0.5 * (unit_direction.y() + 1.0);
return (1.0-t)*Color::new(1.0,1.0,1.0) + t*Color::new(0.5,0.7,1.0); return (1.0 - t) * Color::new(1.0, 1.0, 1.0) + t * Color::new(0.5, 0.7, 1.0);
} }
fn main() { fn main() {
// File // File
let mut default_file = "image.ppm"; let mut default_file = "image.ppm";
// Image // Image
let aspect_ratio = 16.0 / 9.0; let aspect_ratio = 16.0 / 9.0;
let image_width = 800; let image_width = 400;
let image_height = (image_width as f64 / aspect_ratio) as u64; let image_height = (image_width as f64 / aspect_ratio) as u64;
// Camera // Camera
@ -51,29 +50,33 @@ fn main() {
let viewport_width = aspect_ratio * viewport_height; let viewport_width = aspect_ratio * viewport_height;
let focal_length = 1.0; let focal_length = 1.0;
let origin = Point3::new(0.0,0.0,0.0); let origin = Point3::new(0.0, 0.0, 0.0);
let horizontal = Vec3::new(viewport_width, 0.0, 0.0); let horizontal = Vec3::new(viewport_width, 0.0, 0.0);
let vertical = Vec3::new(0.0, viewport_height, 0.0); let vertical = Vec3::new(0.0, viewport_height, 0.0);
let lower_left_corner = origin - horizontal/2.0 - vertical/2.0 - Vec3::new(0.0, 0.0, focal_length); let lower_left_corner =
origin - horizontal / 2.0 - vertical / 2.0 - Vec3::new(0.0, 0.0, focal_length);
// Render // Render
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
if args.len() > 1 && args[1] != "" { if args.len() > 1 && args[1] != "" {
default_file = &args[1]; default_file = &args[1];
} }
let mut file = BufWriter::new(File::create(default_file).unwrap()); let mut file = BufWriter::new(File::create(default_file).unwrap());
writeln!(&mut file,"P3\n{image_width} {image_height}\n255\n").unwrap(); writeln!(&mut file, "P3\n{image_width} {image_height}\n255\n").unwrap();
for j in (0..image_height).rev() { for j in (0..image_height).rev() {
eprint!("\rScanlines remaining: {j:5}"); eprint!("\rScanlines remaining: {j:5}");
for i in 0..image_width { for i in 0..image_width {
let u = i as f64 / (image_width-1) as f64; let u = i as f64 / (image_width - 1) as f64;
let v = j as f64 / (image_height-1) as f64; let v = j as f64 / (image_height - 1) as f64;
let r = Ray::new(origin, lower_left_corner + u*horizontal + v*vertical - origin); let r = Ray::new(
origin,
lower_left_corner + u * horizontal + v * vertical - origin,
);
let pixel_color = ray_color(&r); let pixel_color = ray_color(&r);
color::write_color(&mut file, pixel_color); color::write_color(&mut file, pixel_color);
} }
} }
eprintln!("\nDone!"); eprintln!("\nDone!");
} }

View file

@ -3,7 +3,7 @@ use super::Vec3;
pub struct Ray { pub struct Ray {
pub orig: Point3, pub orig: Point3,
pub dir: Vec3 pub dir: Vec3,
} }
impl Ray { impl Ray {
@ -20,6 +20,6 @@ impl Ray {
} }
pub fn at(&self, t: f64) -> Point3 { pub fn at(&self, t: f64) -> Point3 {
self.orig + t*self.dir self.orig + t * self.dir
} }
} }

View file

@ -17,36 +17,42 @@ impl Vec3 {
self.length_squared().sqrt() self.length_squared().sqrt()
} }
pub fn length_squared(&self) -> f64 { pub fn length_squared(&self) -> f64 {
self.e[0]*self.e[0] self.e[0] * self.e[0] + self.e[1] * self.e[1] + self.e[2] * self.e[2]
+ self.e[1]*self.e[1]
+ self.e[2]*self.e[2]
} }
pub fn cross(a: Vec3, b: Vec3) -> Vec3 { pub fn cross(a: Vec3, b: Vec3) -> Vec3 {
Vec3::new(a.y()*b.z() - a.z()*b.y(), Vec3::new(
a.z()*b.x() - a.x()*b.z(), a.y() * b.z() - a.z() * b.y(),
a.x()*b.y() - a.y()*b.x()) a.z() * b.x() - a.x() * b.z(),
a.x() * b.y() - a.y() * b.x(),
)
} }
pub fn dot(a: Vec3, b: Vec3) -> f64 { pub fn dot(a: Vec3, b: Vec3) -> f64 {
a.x()*b.x() + a.y()*b.y() + a.z()*b.z() a.x() * b.x() + a.y() * b.y() + a.z() * b.z()
} }
pub fn unit_vector(a: Vec3) -> Vec3 { pub fn unit_vector(a: Vec3) -> Vec3 {
a / a.length() a / a.length()
} }
pub fn x(&self) -> f64 { self.e[0] } pub fn x(&self) -> f64 {
pub fn y(&self) -> f64 { self.e[1] } self.e[0]
pub fn z(&self) -> f64 { self.e[2] } }
pub fn y(&self) -> f64 {
self.e[1]
}
pub fn z(&self) -> f64 {
self.e[2]
}
} }
impl Add for Vec3 { impl Add for Vec3 {
type Output = Vec3; type Output = Vec3;
fn add(self, rhs: Self) -> Vec3 { fn add(self, rhs: Self) -> Vec3 {
Vec3::new(self.x()+rhs.x(), self.y()+rhs.y(), self.z()+rhs.z()) Vec3::new(self.x() + rhs.x(), self.y() + rhs.y(), self.z() + rhs.z())
} }
} }
@ -62,7 +68,7 @@ impl Sub for Vec3 {
type Output = Vec3; type Output = Vec3;
fn sub(self, rhs: Self) -> Vec3 { fn sub(self, rhs: Self) -> Vec3 {
Vec3::new(self.x()-rhs.x(), self.y()-rhs.y(), self.z()-rhs.z()) Vec3::new(self.x() - rhs.x(), self.y() - rhs.y(), self.z() - rhs.z())
} }
} }
@ -78,7 +84,7 @@ impl Mul<Vec3> for f64 {
type Output = Vec3; type Output = Vec3;
fn mul(self, rhs: Vec3) -> Vec3 { fn mul(self, rhs: Vec3) -> Vec3 {
Vec3::new(self*rhs.x(), self*rhs.y(), self*rhs.z()) Vec3::new(self * rhs.x(), self * rhs.y(), self * rhs.z())
} }
} }
@ -102,6 +108,6 @@ impl Div<f64> for Vec3 {
type Output = Vec3; type Output = Vec3;
fn div(self, rhs: f64) -> Vec3 { fn div(self, rhs: f64) -> Vec3 {
Vec3::new(self.x()/rhs, self.y()/rhs, self.z()/rhs) Vec3::new(self.x() / rhs, self.y() / rhs, self.z() / rhs)
} }
} }