summaryrefslogtreecommitdiff
path: root/src/renderer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/renderer.rs')
-rw-r--r--src/renderer.rs21
1 files changed, 10 insertions, 11 deletions
diff --git a/src/renderer.rs b/src/renderer.rs
index 59a6b7b..7e46834 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -1,16 +1,15 @@
use crate::calculus::calculus::{Point3, Ray, Vec3};
-use crate::global::{DisplayBuffer, Pixel, CAMERA_CENTER, FOCAL_LENGTH, IMG_HEIGHT, IMG_WIDTH, VIEWPORT_HEIGHT, VIEWPORT_WIDTH};
+use crate::global::{Color, DisplayBuffer, Pixel, CAMERA_CENTER, FOCAL_LENGTH, IMG_HEIGHT, IMG_WIDTH, VIEWPORT_HEIGHT, VIEWPORT_WIDTH};
-fn ray_color(ray: &Ray) -> Vec3 {
+fn ray_color(ray: &Ray) -> Color {
if hit_sphere(&Point3 {x: 0f32, y: 0f32, z: -1f32}, 0.5, ray) {
- return Vec3 {x: 1f32, y: 0f32, z: 0f32}
+ return Color {r: 1f32, g: 0f32, b: 0f32}
}
let unit_direction = ray.direction.unit();
let a = 0.5 * (unit_direction.y + 1.0);
- let vec_1 = Vec3 { x:1.0, y: 1.0, z: 1.0 };
- let vec_2 = Vec3 { x: 0.5, y: 0.7, z: 1.0 };
- vec_1.scalar_mul(1.0 - a)
- .add(&vec_2.scalar_mul(a))
+ let color1 = Color::new(1.0, 1.0, 1.0).mul_scalar(1.0 - a);
+ let color2 = Color::new(0.5, 0.7, 1.0).mul_scalar(a);
+ color1.add(&color2)
}
fn hit_sphere(center: &Point3, radius: f32, ray: &Ray) -> bool {
@@ -26,8 +25,8 @@ pub fn render(display_buffer: &mut DisplayBuffer) {
let viewport_hor_vector = Vec3{ x: VIEWPORT_WIDTH as f32, y: 0.0, z: 0.0 };
let viewport_ver_vector = Vec3 { x: 0.0, y: -1f32 * VIEWPORT_HEIGHT as f32, z: 0.0 };
- let delta_pixel_u = viewport_hor_vector.scalar_mul(1.0 / VIEWPORT_WIDTH as f32);
- let delta_pixel_v = viewport_ver_vector.scalar_mul(1.0 / VIEWPORT_WIDTH as f32);
+ let delta_pixel_u = viewport_hor_vector.scalar_mul(1.0 / IMG_WIDTH as f32);
+ let delta_pixel_v = viewport_ver_vector.scalar_mul(1.0 / IMG_HEIGHT as f32);
let viewport_upper_left = CAMERA_CENTER
.sub(&Vec3 { x: 0f32, y: 0f32, z: FOCAL_LENGTH})
@@ -47,8 +46,8 @@ pub fn render(display_buffer: &mut DisplayBuffer) {
origin: &CAMERA_CENTER,
direction: &ray_direction,
};
- let Vec3 {x, y, z} = ray_color(&ray);
- display_buffer[j][i] = Pixel::from_frac(x, y, z);
+ let color = ray_color(&ray);
+ display_buffer[j][i] = Pixel::from_color(&color);
})
})
} \ No newline at end of file