|
1 | 1 | use nalgebra::{Point3, Vector3};
|
| 2 | +use obj::Material; |
| 3 | +use ordered_float::OrderedFloat; |
2 | 4 |
|
3 |
| -use crate::scene::Scene; |
4 |
| -//use scene::object::Object; |
5 |
| -use std::f64; |
6 |
| -use rand::Rng; |
7 |
| - |
8 |
| -use crate::scene::object::Object; |
9 |
| - |
| 5 | +use crate::{scene::Scene, Color}; |
10 | 6 |
|
| 7 | +#[derive(Debug, Clone, Copy, PartialEq)] |
11 | 8 | pub struct Ray {
|
12 |
| - pub origin: Point3<f64>, |
13 |
| - pub direction: Vector3<f64>, |
14 |
| -} |
15 |
| -pub struct Intersection { |
16 |
| - pub point: Point3<f64>, |
17 |
| - pub normal: Vector3<f64>, |
18 |
| - pub t: f64, |
| 9 | + pub origin: Point3<f32>, |
| 10 | + pub direction: Vector3<f32>, |
19 | 11 | }
|
20 | 12 |
|
21 |
| -pub struct Raytracer<'a> { |
22 |
| - pub scene: &'a Scene, |
23 |
| - pub width: u32, |
24 |
| - pub height: u32, |
25 |
| - pub fov: f64, |
26 |
| - pub background_color: Vector3<f64>, |
27 |
| - pub max_depth: u32, |
| 13 | +#[derive(Debug, PartialEq)] |
| 14 | +pub struct Hit<'a> { |
| 15 | + pub point: Point3<f32>, |
| 16 | + pub normal: Vector3<f32>, |
| 17 | + pub material: Option<&'a Material>, |
28 | 18 | }
|
29 | 19 |
|
30 |
| -impl Intersection { |
31 |
| - pub fn new(point: Point3<f64>, normal: Vector3<f64>, t: f64) -> Self { |
32 |
| - Intersection { point, normal, t } |
33 |
| - } |
| 20 | +pub struct Raytracer<'a> { |
| 21 | + scene: &'a Scene, |
| 22 | + background_color: Color, |
34 | 23 | }
|
35 | 24 |
|
36 |
| - |
37 |
| -impl Raytracer<'_> { |
38 |
| - pub fn trace_ray(&self, ray: &Ray, depth: u32) -> Vector3<f64> { |
39 |
| - if depth > self.max_depth { |
40 |
| - return self.background_color; |
41 |
| - } |
42 |
| - |
43 |
| - let mut closest_intersection: Option<Intersection> = None; |
44 |
| - let mut closest_object: Option<&Object> = None; |
45 |
| - |
46 |
| - for object in self.scene.objects.iter() { |
47 |
| - if let Some(intersection) = object.intersect(ray) { |
48 |
| - if closest_intersection.is_none() || intersection.t < closest_intersection.unwrap().t { |
49 |
| - closest_intersection = Some(intersection); |
50 |
| - closest_object = Some(object); |
51 |
| - } |
52 |
| - } |
53 |
| - } |
54 |
| - |
55 |
| - if let Some(intersection) = closest_intersection { |
56 |
| - let object = closest_object.unwrap(); |
57 |
| - let mut color = Vector3::new(0.0, 0.0, 0.0); |
58 |
| - |
59 |
| - for light in self.scene.lights.iter() { |
60 |
| - let light_direction = (light.position - intersection.point).normalize(); |
61 |
| - let shadow_ray = Ray { |
62 |
| - origin: intersection.point + intersection.normal * 0.0001, |
63 |
| - direction: light_direction, |
64 |
| - }; |
65 |
| - |
66 |
| - let mut in_shadow = false; |
67 |
| - for object in self.scene.objects.iter() { |
68 |
| - if let Some(shadow_intersection) = object.intersect(&shadow_ray) { |
69 |
| - if shadow_intersection.t < (light.position - intersection.point).norm() { |
70 |
| - in_shadow = true; |
71 |
| - break; |
72 |
| - } |
73 |
| - } |
74 |
| - } |
75 |
| - |
76 |
| - if !in_shadow { |
77 |
| - let diffuse = object.material.diffuse; |
78 |
| - let specular = object.material.specular; |
79 |
| - let shininess = object.material.shininess; |
80 |
| - |
81 |
| - let light_intensity = light.intensity / (light.position - intersection.point).norm_squared(); |
82 |
| - let diffuse_intensity = light_intensity * diffuse * intersection.normal.dot(&light_direction).max(0.0); |
83 |
| - let view_direction = -ray.direction.normalize(); |
84 |
| - let half_vector = (light_direction + view_direction).normalize(); |
85 |
| - let specular_intensity = light_intensity * specular * intersection.normal.dot(&half_vector).max(0.0).powf(shininess); |
86 |
| - |
87 |
| - color += light.color.component_mul(&(diffuse_intensity + specular_intensity)); |
88 |
| - } |
89 |
| - } |
90 |
| - |
91 |
| - let reflection_ray = Ray { |
92 |
| - origin: intersection.point + intersection.normal * 0.0001, |
93 |
| - direction: ray.direction - 2.0 * ray.direction.dot(&intersection.normal) * intersection.normal, |
94 |
| - }; |
95 |
| - |
96 |
| - let reflection_color = self.trace_ray(&reflection_ray, depth + 1); |
97 |
| - let reflection_intensity = object.material.reflection; |
98 |
| - color = color * (1.0 - reflection_intensity) + reflection_color * reflection_intensity; |
99 |
| - |
100 |
| - color |
101 |
| - } else { |
102 |
| - self.background_color |
| 25 | +impl<'a> Raytracer<'a> { |
| 26 | + pub fn new(scene: &'a Scene, background_color: Vector3<f32>) -> Raytracer<'a> { |
| 27 | + Raytracer { |
| 28 | + scene, |
| 29 | + background_color, |
103 | 30 | }
|
104 | 31 | }
|
105 | 32 |
|
106 |
| - pub fn render(&self) -> Vec<Vector3<f64>> { |
107 |
| - let mut rng = rand::thread_rng(); |
108 |
| - let mut pixels = vec![Vector3::new(0.0, 0.0, 0.0); (self.width * self.height) as usize]; |
109 |
| - |
110 |
| - for y in 0..self.height { |
111 |
| - for x in 0..self.width { |
112 |
| - let u = (x as f64 + rng.gen::<f64>()) / self.width as f64; |
113 |
| - let v = (y as f64 + rng.gen::<f64>()) / self.height as f64; |
114 |
| - |
115 |
| - let aspect_ratio = self.width as f64 / self.height as f64; |
116 |
| - let fov_adjustment = (self.fov.to_radians() / 2.0).tan(); |
117 |
| - let sensor_x = (((x as f64 + 0.5) / self.width as f64) * 2.0 - 1.0) * aspect_ratio * fov_adjustment; |
118 |
| - let sensor_y = (1.0 - ((y as f64 + 0.5) / self.height as f64) * 2.0) * fov_adjustment; |
119 |
| - |
120 |
| - let direction = Vector3::new(sensor_x, sensor_y, -1.0).normalize(); |
121 |
| - let ray = Ray { |
122 |
| - origin: Point3::new(0.0, 0.0, 0.0), |
123 |
| - direction: direction, |
124 |
| - }; |
125 |
| - |
126 |
| - let color = self.trace_ray(&ray, 0); |
127 |
| - pixels[(y * self.width + x) as usize] = color; |
128 |
| - } |
129 |
| - } |
| 33 | + fn raycast(&self, ray: Ray) -> Option<Hit> { |
| 34 | + self.scene |
| 35 | + .objects |
| 36 | + .iter() |
| 37 | + .filter_map(|o| o.intersect(ray)) |
| 38 | + .min_by_key(|h| OrderedFloat((h.point - ray.origin).norm())) |
| 39 | + } |
130 | 40 |
|
131 |
| - pixels |
| 41 | + pub fn render(&self, ray: Ray) -> Color { |
| 42 | + self.raycast(ray) |
| 43 | + .map(|_| Color::new(1.0, 0.0, 0.0)) |
| 44 | + .unwrap_or(self.background_color) |
132 | 45 | }
|
133 | 46 | }
|
134 |
| - |
0 commit comments