ft (wip): textures

This commit is contained in:
2026-05-12 18:20:06 +02:00
parent 383f739808
commit 6a1e50fb7a
13 changed files with 222 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
use core::f32::math::sqrt;
use std::f32::consts::PI;
use std::fmt::{self, Debug};
use std::sync::Arc;
@@ -14,7 +15,6 @@ pub struct Sphere {
material: Arc<dyn Material>,
}
impl Debug for Sphere {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Sphere")
@@ -59,13 +59,26 @@ impl Hittable for Sphere {
let t = if tl > 0.001 { tl } else { tr };
let p = r.at(t);
let out_n = (p - self.center) / self.radius;
let uv = self.to_uv(&p);
Some(Hit::new(
t,
p,
(p - self.center).get_unit(),
self.material.clone(),
out_n.dot(r.dir()) < 0.,
*uv.x(),
*uv.y(),
))
}
}
fn to_uv(&self, point: &Vec3) -> Vec3 {
let p = *point - self.center;
// TODO: add rotated texture support
return Vec3::new(
0.5 + p.y().atan2(*p.x()) / (2. * PI),
1. - (p.z() / self.radius).acos() / PI,
0.0,
);
}
}