@@ 1,25 1,50 @@
mod vec3;
use vec3::*;
+mod ray;
+use ray::*;
+
+fn ray_color(r: &Ray) -> Color {
+ let unit_direction = 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() {
let stdout = std::io::stdout();
let mut handle = stdout.lock();
// Image
- let image_width = 256i32;
- let image_height = 256i32;
-
+ let aspect_ratio = 16.0 / 9.0;
+ let image_width = 400;
+ let image_height = (image_width as f64 / aspect_ratio) as i32;
+
+ // Camera
+
+ let viewport_height = 2.0;
+ let viewport_width = aspect_ratio * viewport_height;
+ let focal_length = 1.0;
+
+ let origin = Point3::new(0f64, 0f64, 0f64);
+ let horizontal = Vec3::new(viewport_width, 0f64, 0f64);
+ let vertical = Vec3::new(0f64, viewport_height, 0f64);
+ let lower_left_corner =
+ origin - horizontal / 2f64 - vertical / 2f64 - Vec3::new(0f64, 0f64, focal_length);
+
// Render
-
+
print!("P3\n{} {}\n255\n", image_width, image_height);
- for j in (0..=(image_height-1)).rev() {
+ for j in (0..=(image_height - 1)).rev() {
eprint!("\rScanlines remaining: {} ", j);
for i in 0..image_width {
- let pixel_color = Color::new(
- i as f64 / (image_width-1) as f64,
- j as f64 / (image_height-1) as f64,
- 0.25);
+ let u = i as f64 / (image_width - 1) as f64;
+ let v = j as f64 / (image_height - 1) as f64;
+ let r = Ray::new(
+ &origin,
+ &(lower_left_corner + u * horizontal + v * vertical - origin)
+ );
+ let pixel_color = ray_color(&r);
write_color(&mut handle, pixel_color);
}
}
@@ 56,26 56,26 @@ impl std::ops::IndexMut<usize> for Vec3 {
}
}
-impl std::ops::Add<&Vec3> for &Vec3 {
+impl std::ops::Add<Vec3> for Vec3 {
type Output = Vec3;
- fn add(self, rhs: &Vec3) -> Vec3 {
+ fn add(self, rhs: Vec3) -> Vec3 {
let mut c = self.clone();
c += rhs;
c
}
}
-impl std::ops::AddAssign<&Vec3> for Vec3 {
- fn add_assign(&mut self, rhs: &Vec3) {
+impl std::ops::AddAssign<Vec3> for Vec3 {
+ fn add_assign(&mut self, rhs: Vec3) {
self.e[0] += rhs.e[0];
self.e[1] += rhs.e[1];
self.e[2] += rhs.e[2];
}
}
-impl std::ops::Sub<&Vec3> for &Vec3 {
+impl std::ops::Sub<Vec3> for Vec3 {
type Output = Vec3;
- fn sub(self, rhs: &Vec3) -> Vec3 {
+ fn sub(self, rhs: Vec3) -> Vec3 {
Vec3 {
e: [
self.e[0] - rhs.e[0],
@@ 86,9 86,9 @@ impl std::ops::Sub<&Vec3> for &Vec3 {
}
}
-impl std::ops::Mul<&Vec3> for &Vec3 {
+impl std::ops::Mul<Vec3> for Vec3 {
type Output = Vec3;
- fn mul(self, rhs: &Vec3) -> Vec3 {
+ fn mul(self, rhs: Vec3) -> Vec3 {
Vec3 {
e: [
self.e[0] * rhs.e[0],
@@ 99,7 99,7 @@ impl std::ops::Mul<&Vec3> for &Vec3 {
}
}
-impl std::ops::Mul<f64> for &Vec3 {
+impl std::ops::Mul<f64> for Vec3 {
type Output = Vec3;
fn mul(self, rhs: f64) -> Vec3 {
Vec3 {
@@ 108,9 108,9 @@ impl std::ops::Mul<f64> for &Vec3 {
}
}
-impl std::ops::Mul<&Vec3> for f64 {
+impl std::ops::Mul<Vec3> for f64 {
type Output = Vec3;
- fn mul(self, rhs: &Vec3) -> Vec3 {
+ fn mul(self, rhs: Vec3) -> Vec3 {
rhs * self
}
}
@@ 129,7 129,7 @@ impl std::ops::DivAssign<f64> for Vec3 {
}
}
-impl std::ops::Div<f64> for &Vec3 {
+impl std::ops::Div<f64> for Vec3 {
type Output = Vec3;
fn div(self, rhs: f64) -> Vec3 {
@@ 159,13 159,17 @@ pub fn cross(u: &Vec3, v: &Vec3) -> Vec3 {
)
}
-pub fn unit_vector(v: &Vec3) -> Vec3 {
+pub fn unit_vector(v: Vec3) -> Vec3 {
v / v.lenght()
}
pub fn write_color(out: &mut impl std::io::Write, pixel_color: Color) {
- write!(out, "{} {} {}\n",
+ write!(
+ out,
+ "{} {} {}\n",
(255.999 * pixel_color.x()) as i32,
(255.999 * pixel_color.y()) as i32,
- (255.999 * pixel_color.z()) as i32).unwrap();
+ (255.999 * pixel_color.z()) as i32
+ )
+ .unwrap();
}