Multiple colors supported.

This commit is contained in:
Madeline Pace
2021-12-20 15:07:30 -05:00
parent 8ce5ae94b5
commit f909b12962
+29 -22
View File
@@ -83,13 +83,13 @@ impl Material {
struct Color { struct Color {
red: f32, red: f64,
green: f32, green: f64,
blue: f32 blue: f64
} }
impl Color { impl Color {
fn new(red: f32, green: f32, blue: f32) -> Color { fn new(red: f64, green: f64, blue: f64) -> Color {
Color { Color {
red: red, red: red,
green: green, green: green,
@@ -145,16 +145,18 @@ impl<'a> Intersection<'a> {
} }
fn get_color(camera: &OrthoCamera, ray: &Ray, intersection: &Intersection) -> f64 { //fn get_color(camera: &OrthoCamera, ray: &Ray, intersection: &Intersection) -> Color {
let hit_point = ray.at(intersection.distance); // let hit_point = ray.at(intersection.distance);
let normal = intersection.object.pos - hit_point; // let normal = intersection.object.pos - hit_point;
let light_vec = hit_point - camera.light.pos; // let light_vec = hit_point - camera.light.pos;
//
let light_intensity = camera.light.intensity; // let light_intensity = camera.light.intensity;
let light_power = (normal.normalize().dot(&light_vec.normalize()) as f64).max(0.0) * light_intensity; // let light_power = (normal.normalize().dot(&light_vec.normalize()) as f64).max(0.0) * light_intensity;
let light_reflected = 2.0 / std::f64::consts::PI; // let light_reflected = 2.0 / std::f64::consts::PI;
return light_power * light_reflected; // let total_light: f32 = light_power * light_reflected;
} //
// return color;
//}
fn main() { fn main() {
let mut camera = OrthoCamera { let mut camera = OrthoCamera {
@@ -175,10 +177,13 @@ fn main() {
let y: f64 = rng.gen::<f64>() * 250.0; let y: f64 = rng.gen::<f64>() * 250.0;
let z: f64 = rng.gen::<f64>() * 250.0; let z: f64 = rng.gen::<f64>() * 250.0;
let radius: f64 = rng.gen::<f64>() * 40.0; let radius: f64 = rng.gen::<f64>() * 40.0;
let red: f64 = rng.gen::<f64>() * 100.0;
let green: f64 = rng.gen::<f64>() * 100.0;
let blue: f64 = rng.gen::<f64>() * 100.0;
let sphere = Sphere { let sphere = Sphere {
pos: Vec3::new(x, y, 100.0), pos: Vec3::new(x, y, 100.0),
radius: radius, radius: radius,
material: Material::new(Color::new(120.0, 0.0, 0.0), 2.0, SurfaceType::Reflective { reflectivity: 1.0 }) material: Material::new(Color::new(red, green, blue), 2.0, SurfaceType::Reflective { reflectivity: 1.0 })
}; };
camera.spheres.push(sphere); camera.spheres.push(sphere);
//camera.spheres.push(Sphere::new(Vec3::new(x, y, 100.0), radius)); //camera.spheres.push(Sphere::new(Vec3::new(x, y, 100.0), radius));
@@ -193,7 +198,7 @@ fn main() {
let hit_point = ray.at(intersection.distance); let hit_point = ray.at(intersection.distance);
let normal = hit_point - intersection.object.pos; let normal = hit_point - intersection.object.pos;
let light_dir = hit_point - camera.light.pos; let light_dir = hit_point - camera.light.pos;
let light_color = get_color(&camera, &ray, &intersection); let light_color = &intersection.object.material.coloration;
let shadow_ray = Ray { let shadow_ray = Ray {
pos: hit_point + (normal.normalize()), pos: hit_point + (normal.normalize()),
dir: -light_dir.normalize() dir: -light_dir.normalize()
@@ -203,12 +208,14 @@ fn main() {
let in_light = camera.trace(&shadow_ray).is_none(); let in_light = camera.trace(&shadow_ray).is_none();
let light_intensity = if in_light { camera.light.intensity } else { 0.0 }; let light_intensity = if in_light { camera.light.intensity } else { 0.0 };
if in_light { let light_power = (normal.normalize().dot(&-light_dir.normalize()) as f64).max(0.0) * light_intensity;
println!("in light"); let light_reflected = 2.0 / std::f64::consts::PI;
} else {
println!("in shadow"); let red = light_color.red * light_power * light_reflected;
} let green = light_color.green * light_power * light_reflected;
camera.plane.set_pixel(x, y, px!(light_color * light_intensity, 0, 0)) let blue = light_color.blue * light_power * light_reflected;
camera.plane.set_pixel(x, y, px!(red, green, blue))
}, },
None => { } None => { }
} }