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,14 +1,14 @@
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;
@ -37,13 +37,12 @@ fn ray_color(r: &Ray) -> Color{
} }
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
@ -54,7 +53,8 @@ fn main() {
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();
@ -69,7 +69,10 @@ fn main() {
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);

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 {

View file

@ -18,15 +18,15 @@ impl Vec3 {
} }
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.y() * b.z() - a.z() * b.y(),
a.z() * b.x() - a.x() * b.z(), a.z() * b.x() - a.x() * b.z(),
a.x()*b.y() - a.y()*b.x()) a.x() * b.y() - a.y() * b.x(),
)
} }
pub fn dot(a: Vec3, b: Vec3) -> f64 { pub fn dot(a: Vec3, b: Vec3) -> f64 {
@ -37,9 +37,15 @@ impl 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 {