ft (wip): textures
This commit is contained in:
@@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user