Avançando mais no raytracer
This commit is contained in:
parent
2fc49e1a7c
commit
f31f2df3bd
6
rusty-raytracer/Cargo.toml
Normal file
6
rusty-raytracer/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "rusty-raytracer"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
90003
rusty-raytracer/output.ppm
Normal file
90003
rusty-raytracer/output.ppm
Normal file
File diff suppressed because it is too large
Load Diff
90
rusty-raytracer/src/main.rs
Normal file
90
rusty-raytracer/src/main.rs
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
mod ray;
|
||||||
|
mod vector3;
|
||||||
|
use vector3::Color;
|
||||||
|
use vector3::Vec3;
|
||||||
|
|
||||||
|
use crate::ray::ray_color;
|
||||||
|
|
||||||
|
fn write_pixel(color: Color) {
|
||||||
|
let r = (255.999 * color.x) as i32;
|
||||||
|
let g = (255.999 * color.y) as i32;
|
||||||
|
let b = (255.999 * color.z) as i32;
|
||||||
|
println!("{} {} {}", r, g, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let aspect_ratio = 16.0 / 9.0;
|
||||||
|
let image_width = 400.0;
|
||||||
|
|
||||||
|
let image_height_tmp: i32 = (image_width / aspect_ratio) as i32;
|
||||||
|
let image_height: i32 = if image_height_tmp < 1 {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
image_height_tmp
|
||||||
|
};
|
||||||
|
// Camera
|
||||||
|
let focal_length = 1.0;
|
||||||
|
let viewport_height = 2.0;
|
||||||
|
let viewport_width = viewport_height * (((image_width as i32) / image_height) as f64);
|
||||||
|
let camera_center = Vec3 {
|
||||||
|
x: 0.0,
|
||||||
|
y: 0.0,
|
||||||
|
z: 0.0,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Calculate the vectors across the horizontal and down the vertical viewport edges.
|
||||||
|
let viewport_u = Vec3 {
|
||||||
|
x: viewport_width,
|
||||||
|
y: 0.0,
|
||||||
|
z: 0.0,
|
||||||
|
};
|
||||||
|
let viewport_v = Vec3 {
|
||||||
|
x: 0.0,
|
||||||
|
y: -viewport_height,
|
||||||
|
z: 0.0,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Calculate the horizontal and vertical delta vectors from pixel to pixel.
|
||||||
|
let pixel_delta_u = viewport_u / image_width;
|
||||||
|
let pixel_delta_v = viewport_v / (image_height as f64);
|
||||||
|
|
||||||
|
// Calculate the location of the upper left pixel.
|
||||||
|
let viewport_upper_left = camera_center
|
||||||
|
- Vec3 {
|
||||||
|
x: 0.0,
|
||||||
|
y: 0.0,
|
||||||
|
z: focal_length,
|
||||||
|
}
|
||||||
|
- viewport_u / 2.0
|
||||||
|
- viewport_v / 2.0;
|
||||||
|
|
||||||
|
let pixel00_loc = viewport_upper_left + 0.5 * (pixel_delta_u + pixel_delta_v);
|
||||||
|
println!("P3");
|
||||||
|
println!("{} {}", image_width, image_height);
|
||||||
|
println!("255");
|
||||||
|
for j in 0..image_height {
|
||||||
|
eprintln!("Scanlines remaining: {}", image_height - j);
|
||||||
|
for i in 0..(image_width as i32) {
|
||||||
|
let pixel_center =
|
||||||
|
pixel00_loc + ((i as f64) * pixel_delta_u) + ((j as f64) * pixel_delta_v);
|
||||||
|
let ray_direction = pixel_center - camera_center;
|
||||||
|
let r = ray::ray {
|
||||||
|
origin: camera_center,
|
||||||
|
direction: ray_direction,
|
||||||
|
};
|
||||||
|
let color = ray_color(r);
|
||||||
|
// let pos = Vec3 {
|
||||||
|
// x: (i as f64) / ((image_width as f64) - 1.0),
|
||||||
|
// y: (j as f64) / ((image_height as f64) - 1.0),
|
||||||
|
// z: 0.25,
|
||||||
|
// };
|
||||||
|
// let color = Color {
|
||||||
|
// x: (255_999.0 * pos.x / 1000.0),
|
||||||
|
// y: (255_999.0 * pos.y / 1000.0),
|
||||||
|
// z: (255_999.0 * pos.z / 1000.0),
|
||||||
|
// };
|
||||||
|
write_pixel(color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
eprintln!("Done!");
|
||||||
|
}
|
||||||
156
rusty-raytracer/src/ray.rs
Normal file
156
rusty-raytracer/src/ray.rs
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
use std::mem::Discriminant;
|
||||||
|
|
||||||
|
use crate::vector3::{self, Color, Vec3};
|
||||||
|
use crate::vector3::{LengthSquared, dot};
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub struct ray {
|
||||||
|
pub origin: Vec3,
|
||||||
|
pub direction: Vec3,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn at(ray: ray, t: f64) -> Vec3 {
|
||||||
|
ray.origin + t * ray.direction
|
||||||
|
}
|
||||||
|
|
||||||
|
//pub fn hit(center: Vec3, radius: f64, r: ray) -> f64 {
|
||||||
|
pub fn hit(center: Vec3, radius: f64, r: ray) -> f64 {
|
||||||
|
let oc = center - r.origin;
|
||||||
|
let a = r.direction.length_squared();
|
||||||
|
let h = dot(r.direction, oc);
|
||||||
|
let c = oc.length_squared() - radius * radius;
|
||||||
|
let discriminant = h * h - a * c;
|
||||||
|
if discriminant < 0.0 {
|
||||||
|
-1.0
|
||||||
|
} else {
|
||||||
|
(h - discriminant.sqrt()) / a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ray_color(r: ray, world: Vec<Hittable>) -> Vec3 {
|
||||||
|
let infinity = 0.0;
|
||||||
|
for object in world {
|
||||||
|
let hit = object.hit(&r, 0.0, infinity);
|
||||||
|
match hit {
|
||||||
|
Option::Some(HitRecord {
|
||||||
|
point,
|
||||||
|
normal,
|
||||||
|
t,
|
||||||
|
front_face,
|
||||||
|
}) => {
|
||||||
|
return 0.5
|
||||||
|
* (normal
|
||||||
|
+ Color {
|
||||||
|
x: 1.0,
|
||||||
|
y: 1.0,
|
||||||
|
z: 1.0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let t = hit(
|
||||||
|
Vec3 {
|
||||||
|
x: 0.0,
|
||||||
|
y: 0.0,
|
||||||
|
z: -1.0,
|
||||||
|
},
|
||||||
|
0.5,
|
||||||
|
r,
|
||||||
|
);
|
||||||
|
if t > 0.0 {
|
||||||
|
let n: Vec3 = vector3::unit_vector(
|
||||||
|
at(r, t)
|
||||||
|
- Vec3 {
|
||||||
|
x: 0.0,
|
||||||
|
y: 0.0,
|
||||||
|
z: -1.0,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
0.5 * Color {
|
||||||
|
x: n.x + 1.0,
|
||||||
|
y: n.y + 1.0,
|
||||||
|
z: n.z + 1.0,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let unit_direction = vector3::unit_vector(r.direction);
|
||||||
|
let a = 0.5 * (unit_direction.y + 1.0);
|
||||||
|
let one = Color {
|
||||||
|
x: 1.0,
|
||||||
|
y: 1.0,
|
||||||
|
z: 1.0,
|
||||||
|
};
|
||||||
|
(1.0 - a) * one
|
||||||
|
+ a * Color {
|
||||||
|
x: 0.5,
|
||||||
|
y: 0.7,
|
||||||
|
z: 1.0,
|
||||||
|
}
|
||||||
|
// Vec3 {
|
||||||
|
// x: 0.0,
|
||||||
|
// y: 0.0,
|
||||||
|
// z: 0.0,
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub struct HitRecord {
|
||||||
|
pub point: Vec3,
|
||||||
|
pub normal: Vec3,
|
||||||
|
pub t: f64,
|
||||||
|
pub front_face: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compute_face_normal(record: HitRecord, r: &ray, outward_normal: Vec3) -> HitRecord {
|
||||||
|
HitRecord {
|
||||||
|
point: record.point,
|
||||||
|
t: record.t,
|
||||||
|
front_face: dot(r.direction, outward_normal) < 0.0,
|
||||||
|
normal: if record.front_face {
|
||||||
|
outward_normal
|
||||||
|
} else {
|
||||||
|
-outward_normal
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub enum Hittable {
|
||||||
|
Sphere { center: Vec3, radius: f64 },
|
||||||
|
}
|
||||||
|
impl Hittable {
|
||||||
|
pub fn hit(&self, r: &ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
|
||||||
|
match self {
|
||||||
|
Hittable::Sphere { center, radius } => {
|
||||||
|
let oc = *center - r.origin;
|
||||||
|
let a = r.direction.length_squared();
|
||||||
|
let h = dot(r.direction, oc);
|
||||||
|
let c = oc.length_squared() - radius * radius;
|
||||||
|
let discriminant = h * h - a * c;
|
||||||
|
if discriminant < 0.0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let sqrtd = discriminant.sqrt();
|
||||||
|
let mut root = (h - sqrtd) / a;
|
||||||
|
if root < t_min || root > t_max {
|
||||||
|
root = (h + sqrtd) / a;
|
||||||
|
if root < t_min || root > t_max {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let p = at(*r, root);
|
||||||
|
let outward_normal = (p - *center) / *radius;
|
||||||
|
|
||||||
|
Some(compute_face_normal(
|
||||||
|
HitRecord {
|
||||||
|
point: p,
|
||||||
|
normal: (p - *center) / *radius,
|
||||||
|
t: root,
|
||||||
|
front_face: false,
|
||||||
|
},
|
||||||
|
r,
|
||||||
|
outward_normal,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
120
rusty-raytracer/src/vector3.rs
Normal file
120
rusty-raytracer/src/vector3.rs
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
use std::ops::Add;
|
||||||
|
//use std::ops::Div;
|
||||||
|
use std::ops::Div;
|
||||||
|
use std::ops::Mul;
|
||||||
|
use std::ops::Neg;
|
||||||
|
use std::ops::Sub;
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub struct Vec3 {
|
||||||
|
pub x: f64,
|
||||||
|
pub y: f64,
|
||||||
|
pub z: f64,
|
||||||
|
}
|
||||||
|
pub trait LengthSquared {
|
||||||
|
fn length_squared(self) -> f64;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Length {
|
||||||
|
fn length(self) -> f64;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LengthSquared for Vec3 {
|
||||||
|
fn length_squared(self) -> f64 {
|
||||||
|
self.x * self.x + self.y * self.y + self.z * self.z
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Length for Vec3 {
|
||||||
|
fn length(self) -> f64 {
|
||||||
|
self.length_squared().sqrt()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add for Vec3 {
|
||||||
|
type Output = Vec3;
|
||||||
|
fn add(self, other: Vec3) -> Vec3 {
|
||||||
|
Vec3 {
|
||||||
|
x: self.x + other.x,
|
||||||
|
y: self.y + other.y,
|
||||||
|
z: self.z + other.z,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mul<Vec3> for f64 {
|
||||||
|
type Output = Vec3;
|
||||||
|
fn mul(self, vec: Vec3) -> Vec3 {
|
||||||
|
Vec3 {
|
||||||
|
x: vec.x * self,
|
||||||
|
y: vec.y * self,
|
||||||
|
z: vec.z * self,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mul<Vec3> for Vec3 {
|
||||||
|
type Output = Vec3;
|
||||||
|
fn mul(self, vec: Vec3) -> Vec3 {
|
||||||
|
Vec3 {
|
||||||
|
x: vec.x * self.x,
|
||||||
|
y: vec.y * self.y,
|
||||||
|
z: vec.z * self.z,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Div<f64> for Vec3 {
|
||||||
|
type Output = Vec3;
|
||||||
|
fn div(self, t: f64) -> Vec3 {
|
||||||
|
(1.0 / t) * self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Div<Vec3> for f64 {
|
||||||
|
type Output = Vec3;
|
||||||
|
fn div(self, t: Vec3) -> Vec3 {
|
||||||
|
(1.0 / self) * t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sub for Vec3 {
|
||||||
|
type Output = Vec3;
|
||||||
|
|
||||||
|
fn sub(self, other: Self) -> Self::Output {
|
||||||
|
Self {
|
||||||
|
x: self.x - other.x,
|
||||||
|
y: self.y - other.y,
|
||||||
|
z: self.z - other.z,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Neg for Vec3 {
|
||||||
|
type Output = Self;
|
||||||
|
fn neg(self) -> Self::Output {
|
||||||
|
Self {
|
||||||
|
x: -self.x,
|
||||||
|
y: -self.y,
|
||||||
|
z: -self.z,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn cross(lhs: Vec3, rhs: Vec3) -> Vec3 {
|
||||||
|
Vec3 {
|
||||||
|
x: lhs.y * rhs.z - lhs.z * rhs.y,
|
||||||
|
y: lhs.z * rhs.x - lhs.x * rhs.z,
|
||||||
|
z: lhs.x * rhs.y - lhs.y * rhs.x,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn dot(lhs: Vec3, rhs: Vec3) -> f64 {
|
||||||
|
let tmp = lhs * rhs;
|
||||||
|
tmp.x + tmp.y + tmp.z
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn unit_vector(v: Vec3) -> Vec3 {
|
||||||
|
v / v.length()
|
||||||
|
}
|
||||||
|
pub type Color = Vec3;
|
||||||
Loading…
Reference in New Issue
Block a user