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
|
use crate::calculus::calculus::Point3;
// "Ideal" aspect ratio, allowing fraction
pub const ASPECT_RATIO: f32 = 16.0f32 / 9.0f32;
pub const IMG_WIDTH: usize = 400;
const IMG_HEIGHT_TMP: usize = (IMG_WIDTH as f32 / ASPECT_RATIO) as usize;
pub const IMG_HEIGHT: usize = if (IMG_HEIGHT_TMP < 1) { 1 } else { IMG_HEIGHT_TMP };
pub const VIEWPORT_HEIGHT: f32 = 2.0;
pub const VIEWPORT_WIDTH: f32 = VIEWPORT_HEIGHT * (IMG_WIDTH as f32 / IMG_HEIGHT as f32);
pub const FOCAL_LENGTH: f32 = 1.0;
pub const CAMERA_CENTER: Point3 = Point3 { x: 0f32, y: 0f32, z: 0f32 };
#[derive(Debug, Copy, Clone)]
pub struct Pixel {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl Pixel {
pub fn from_frac(r: f32, g: f32, b: f32) -> Self {
Self {
r: (255.999 * r) as u8,
g: (255.999 * g) as u8,
b: (255.999 * b) as u8,
}
}
pub fn from_color(color: &Color) -> Self {
let (r, g, b) = (color.r, color.g, color.b);
Self::from_frac(r, g, b)
}
}
impl Default for Pixel {
fn default() -> Self {
Pixel { r:0, g:0, b:0 }
}
}
pub type DisplayBuffer = [[Pixel; IMG_WIDTH]; IMG_HEIGHT];
pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
}
impl Color {
pub fn new(r: f32, g: f32, b: f32) -> Self {
Self { r, g, b }
}
pub fn mul_scalar(&self, scalar: f32) -> Color {
Self {
r: self.r * scalar,
g: self.g * scalar,
b: self.b * scalar,
}
}
pub fn add(&self, other: &Self) -> Color {
Self {
r: self.r + other.r,
g: self.g + other.g,
b: self.b + other.b
}
}
}
|