Viel Stuff, hittable objects auslagern
This commit is contained in:
parent
91876b66dd
commit
2e1a147fad
4 changed files with 2176348 additions and 16289 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
/target
|
/target
|
||||||
|
*.ppm
|
||||||
|
|
57
src/hittable.rs
Normal file
57
src/hittable.rs
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
use super::Point3;
|
||||||
|
use super::Ray;
|
||||||
|
use super::Vec3;
|
||||||
|
|
||||||
|
pub struct HitRecord {
|
||||||
|
p: Point3,
|
||||||
|
normal: Vec3,
|
||||||
|
t: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Sphere {
|
||||||
|
center: Point3,
|
||||||
|
radius: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Hittable {
|
||||||
|
fn hit(&self, r: Ray, t_min: f64, t_max: f64, rec: &mut HitRecord) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sphere {
|
||||||
|
fn new(cen: Point3, r: f64) -> Self {
|
||||||
|
Sphere {
|
||||||
|
center: cen,
|
||||||
|
radius: r,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Hittable for Sphere {
|
||||||
|
fn hit(&self, r: Ray, t_min: f64, t_max: f64, rec: &mut HitRecord) -> bool {
|
||||||
|
let oc = r.origin() - self.center;
|
||||||
|
let a = r.direction().length_squared(); // gleiches Ergebnis wie Skalarprodukt
|
||||||
|
let b = 2.0 * Vec3::dot(oc, r.direction());
|
||||||
|
let c = oc.length_squared() - self.radius * self.radius;
|
||||||
|
|
||||||
|
let discriminant = b * b - 4.0 * a * c;
|
||||||
|
if discriminant < 0.0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let sqrtd = discriminant.sqrt();
|
||||||
|
|
||||||
|
let root = (-b / 2.0 - sqrtd) / a;
|
||||||
|
if root < t_min || t_max < root {
|
||||||
|
let root2 = (-b / 2.0 + sqrtd) / a;
|
||||||
|
if root2 < t_min || t_max < root {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rec.t = root;
|
||||||
|
rec.p = r.at(rec.t);
|
||||||
|
rec.normal = (rec.p - self.center) / self.radius;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
mod color;
|
mod color;
|
||||||
|
mod hittable;
|
||||||
mod ray;
|
mod ray;
|
||||||
mod vec3;
|
mod vec3;
|
||||||
use ray::Ray;
|
use ray::Ray;
|
||||||
|
@ -12,9 +13,9 @@ 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 = r.direction().length_squared(); // gleiches Ergebnis wie Skalarprodukt
|
||||||
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 = oc.length_squared() - 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 {
|
||||||
|
@ -42,7 +43,7 @@ fn main() {
|
||||||
|
|
||||||
// Image
|
// Image
|
||||||
let aspect_ratio = 16.0 / 9.0;
|
let aspect_ratio = 16.0 / 9.0;
|
||||||
let image_width = 400;
|
let image_width = 2000;
|
||||||
let image_height = (image_width as f64 / aspect_ratio) as u64;
|
let image_height = (image_width as f64 / aspect_ratio) as u64;
|
||||||
|
|
||||||
// Camera
|
// Camera
|
||||||
|
|
Loading…
Reference in a new issue