Add solid and checker texture
This commit is contained in:
@ -1,14 +1,18 @@
|
||||
use crate::{Point3, Ray, Vec3};
|
||||
use crate::material::{Material};
|
||||
use crate::aabb::Aabb;
|
||||
use std::f64::consts::PI;
|
||||
|
||||
pub type HittableList = Vec<Box<dyn Hittable + Sync>>;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
//#[derive(Debug, Clone, Copy)]
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct HitRecord<'material> {
|
||||
pub point: Point3,
|
||||
pub normal: Vec3,
|
||||
pub t: f64,
|
||||
pub u: f64,
|
||||
pub v: f64,
|
||||
pub front_face: bool,
|
||||
pub material: &'material Material
|
||||
}
|
||||
@ -18,7 +22,7 @@ pub trait Hittable {
|
||||
fn bounding_box(&self, time0: f64, time1: f64) -> Option<Aabb>;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
//#[derive(Clone)]
|
||||
pub struct Sphere {
|
||||
pub center: Point3,
|
||||
pub radius: f64,
|
||||
@ -28,6 +32,19 @@ impl Sphere {
|
||||
pub fn new(center: Point3, radius: f64, material: Material) -> Sphere {
|
||||
Sphere { center, radius, material }
|
||||
}
|
||||
pub fn uv(point: &Point3) -> (f64, f64) {
|
||||
// p: a given point on the sphere of radius one, centered at the origin.
|
||||
// u: returned value [0,1] of angle around the Y axis from X=-1.
|
||||
// v: returned value [0,1] of angle from Y=-1 to Y=+1.
|
||||
// <1 0 0> yields <0.50 0.50> <-1 0 0> yields <0.00 0.50>
|
||||
// <0 1 0> yields <0.50 1.00> < 0 -1 0> yields <0.50 0.00>
|
||||
// <0 0 1> yields <0.25 0.50> < 0 0 -1> yields <0.75 0.50>
|
||||
|
||||
let theta = -(point.y()).acos();
|
||||
let phi = -(point.z()).atan2(point.x()) + PI;
|
||||
|
||||
(phi / (2.0 * PI), theta / PI)
|
||||
}
|
||||
}
|
||||
impl Default for Sphere {
|
||||
fn default() -> Self {
|
||||
@ -57,11 +74,14 @@ impl Hittable for Sphere {
|
||||
let normal = (point - self.center) / self.radius;
|
||||
let dot = ray.direction().dot(&normal);
|
||||
let front_face = dot < 0.0;
|
||||
let (u, v) = Sphere::uv(&normal);
|
||||
let normal = if front_face { normal } else { -normal };
|
||||
Some(HitRecord {
|
||||
point,
|
||||
normal,
|
||||
t: root,
|
||||
u,
|
||||
v,
|
||||
front_face,
|
||||
material: &self.material
|
||||
})
|
||||
@ -75,7 +95,7 @@ impl Hittable for Sphere {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
//#[derive(Clone)]
|
||||
pub struct MovableSphere {
|
||||
pub center0: Point3,
|
||||
pub center1: Point3,
|
||||
@ -130,13 +150,16 @@ impl Hittable for MovableSphere {
|
||||
let normal = (point - self.center(ray.time())) / self.radius;
|
||||
let dot = ray.direction().dot(&normal);
|
||||
let front_face = dot < 0.0;
|
||||
let (u, v) = Sphere::uv(&normal);
|
||||
let normal = if front_face { normal } else { -normal };
|
||||
Some(HitRecord {
|
||||
point,
|
||||
normal,
|
||||
t: root,
|
||||
u,
|
||||
v,
|
||||
front_face,
|
||||
material: &self.material
|
||||
material: &self.material,
|
||||
})
|
||||
}
|
||||
fn bounding_box(&self, time0: f64, time1: f64) -> Option<Aabb> {
|
||||
|
Reference in New Issue
Block a user