-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
198 lines (163 loc) · 6.12 KB
/
main.rs
File metadata and controls
198 lines (163 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#![feature(portable_simd)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::many_single_char_names)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::cast_possible_truncation)]
mod camera;
mod color;
mod hittable;
mod hittable_list;
mod material;
mod ray;
mod sphere;
mod util;
mod vec;
use core::f32;
use std::{fs, sync::Arc};
use indicatif::ProgressBar;
use rayon::prelude::*;
use crate::{
camera::Camera,
color::Color,
hittable::Hittable,
hittable_list::HittableList,
material::Dielectric,
material::{Lambertian, Metal},
ray::Ray,
sphere::Sphere,
vec::{Point3, Vec3},
};
#[allow(dead_code)]
fn random_scene() -> HittableList {
let mut world = HittableList::new();
//let ground_material = Arc::new(Lambertian::new(Color::new(0.5, 0.5, 0.5)));
let ground_material = make_material!(Lambertian, 0.5, 0.5, 0.5);
add_to_world!(world, ground_material, 0.0, -1000.0, 0.0, 1000.0);
for a in -11..11 {
for b in -11..11 {
let choose_mat = util::random_double();
let center = Point3::new(
0.9f32.mul_add(util::random_double(), a as f32),
0.2,
0.9f32.mul_add(util::random_double(), b as f32),
);
if (center - Point3::new(4.0, 0.2, 0.0)).length() > 0.9 {
if choose_mat < 0.8 {
// Diffuse
let albedo = Color::random() * Color::random();
let sphere_material = make_material!(Lambertian, albedo);
add_to_world!(world, sphere_material, center, 0.2);
} else if choose_mat < 0.95 {
// Metal
let albedo = Color::random_range(0.5, 1.0);
let fuzz = util::random_double_range(0.0, 0.5);
let sphere_material = make_material!(Metal, albedo, fuzz);
add_to_world!(world, sphere_material, center, 0.02);
} else {
// Glass
let sphere_material = make_material!(Dielectric, 1.5);
add_to_world!(world, sphere_material, center, 0.2);
}
}
}
}
let material1 = make_material!(Dielectric, 1.5);
add_to_world!(world, material1, 0.0, 1.0, 0.0, 1.0);
let material2 = make_material!(Lambertian, 0.5, 0.2, 0.1);
add_to_world!(world, material2, -4.0, 1.0, 0.0, 1.0);
let material3 = make_material!(Metal, 0.7, 0.6, 0.6, 0.0);
add_to_world!(world, material3, 4.0, 1.0, 0.0, 1.0);
world
}
/// Generates the scene used in the perfomance benchmarks
#[allow(dead_code)]
fn benchmark_scene() -> HittableList {
let mut world = HittableList::new();
let ground_material = make_material!(Lambertian, 0.5, 0.5, 0.5);
add_to_world!(world, ground_material, 0.0, -1000.0, 0.0, 1000.0);
let material1 = make_material!(Dielectric, 1.5);
add_to_world!(world, material1, 0.0, 1.0, 0.0, 1.0);
let material2 = make_material!(Lambertian, 0.5, 0.2, 0.1);
add_to_world!(world, material2, -4.0, 1.0, 0.0, 1.0);
let material3 = make_material!(Metal, 0.7, 0.6, 0.5, 0.0);
add_to_world!(world, material3, 4.0, 1.0, 0.0, 1.0);
world
}
fn ray_color(r: &Ray, world: &dyn Hittable, depth: i32) -> Color {
// Make sure there is no stack overflow
if depth <= 0 {
return Color::new(0.0, 0.0, 0.0);
}
if let Some(hit_rec) = world.hit(r, 0.001, f32::INFINITY) {
if let Some(scatter_rec) = hit_rec.mat.scatter(r, &hit_rec) {
return scatter_rec.attenuation * ray_color(&scatter_rec.scattered, world, depth - 1);
}
return Color::new(0.0, 0.0, 0.0);
}
let unit_direction = vec::unit_vector(r.direction());
let t = 0.5 * (unit_direction.y() + 1.0);
(1.0 - t) * Color::new(1.0, 1.0, 1.0) + t * Color::new(0.5, 0.7, 1.0)
}
fn main() {
// Image
const ASPECT_RATIO: f32 = 3.0 / 2.0;
const IMAGE_WIDTH: i32 = 1200;
const IMAGE_HEIGHT: i32 = (IMAGE_WIDTH as f32 / ASPECT_RATIO) as i32;
const SAMPLES_PER_PIXEL: i32 = 100;
const MAX_DEPTH: i32 = 50;
// World
let world = benchmark_scene();
// Camera
let from = Point3::new(13.0, 2.0, 3.0);
let lookat = Point3::new(0.0, 0.0, 0.0);
let vup = Point3::new(0.0, 1.0, 0.0);
let dist_to_focus = 10.0;
let aperture = 0.1;
let cam = Camera::new(
from,
lookat,
vup,
20.0,
ASPECT_RATIO,
aperture,
dist_to_focus,
);
let viewport_height = 2.0;
let viewport_width = ASPECT_RATIO * viewport_height;
let focal_length = 1.0;
let origin = Point3::new(0.0, 0.0, 0.0);
let horizontal = Vec3::new(viewport_width, 0.0, 0.0);
let vertical = Vec3::new(0.0, viewport_height, 0.0);
let _lower_left_corner =
origin - horizontal / 2.0 - vertical / 2.0 - Vec3::new(0.0, 0.0, focal_length);
// Render
let total_scanlines = 0..IMAGE_HEIGHT;
let bar = ProgressBar::new(total_scanlines.len() as u64);
let mut ppm_bytes: Vec<u8> = Vec::new();
ppm_bytes.append(&mut format!("P3\n{IMAGE_WIDTH} {IMAGE_HEIGHT}\n255\n").into_bytes());
println!("Image Rendering");
for j in total_scanlines.rev() {
bar.inc(1);
let pixel_colors: Vec<_> = (0..IMAGE_WIDTH)
.into_par_iter()
.map(|i| {
let mut pixel_color = Color::new(0.0, 0.0, 0.0);
for _ in 0..SAMPLES_PER_PIXEL {
let u = ((i as f32) + util::random_double()) / (IMAGE_WIDTH - 1) as f32;
let v = ((j as f32) + util::random_double()) / (IMAGE_HEIGHT - 1) as f32;
let r = cam.get_ray(u, v);
pixel_color += ray_color(&r, &world, MAX_DEPTH);
}
pixel_color
})
.collect();
for pixel_color in pixel_colors {
ppm_bytes.append(&mut color::write_color(pixel_color, SAMPLES_PER_PIXEL));
}
}
bar.finish();
println!("\n\nFinished Rendering");
println!("Saving Image to image.png");
fs::write("image.ppm", ppm_bytes).expect("Could not write to file image.ppm");
println!("Image saved");
}