-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
50 lines (46 loc) · 1 KB
/
main.rs
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
use glam::DVec3;
use indicatif::ProgressIterator;
use itertools::Itertools;
use std::{fs, io};
const IMAGE_HEIGHT: u32 = 256;
const IMAGE_WIDTH: u32 = 256;
const MAX_VALUE: u8 = 255;
fn main() -> io::Result<()> {
let pixels = (0..IMAGE_HEIGHT)
.cartesian_product(0..IMAGE_WIDTH)
.progress_count(
IMAGE_HEIGHT as u64 * IMAGE_WIDTH as u64,
)
.map(|(y, x)| {
let r = x as f64 / (IMAGE_WIDTH - 1) as f64;
let g = y as f64 / (IMAGE_HEIGHT - 1) as f64;
let b = 0.0;
format!(
"{} {} {}",
r * 255.0,
g * 255.0,
b * 255.0
)
})
.join("\n");
fs::write(
"output.ppm",
format!(
"P3
{IMAGE_WIDTH} {IMAGE_HEIGHT}
{MAX_VALUE}
{pixels}
"
),
)?;
Ok(())
}
struct Ray {
origin: DVec3,
direction: DVec3,
}
impl Ray {
fn at(&self, t: f64) -> DVec3 {
self.origin + t * self.direction
}
}