refactor: scene/raytracer/camera logic
This commit is contained in:
1 parent
549707fbb3
commit
383f739808
5 files changed
+82
-20
No files matched your search
+10
-10
@@ -2,7 +2,7 @@ use image::{ExtendedColorType, ImageEncoder, codecs::png::PngEncoder};
|
||||
use log::{error, info};
|
||||
use rayon::iter::IntoParallelIterator;
|
||||
use rayon::prelude::*;
|
||||
use std::{fs::File, sync::Arc};
|
||||
use std::{fs::File, sync::Arc, time::Instant};
|
||||
|
||||
use crate::{
|
||||
objects::{hit::Hit, traits::Hittable},
|
||||
@@ -24,8 +24,7 @@ pub fn write_image(filename: &str, pixels: &[u8], width: u32, height: u32) {
|
||||
}
|
||||
|
||||
pub fn render(scene: &mut Scene) {
|
||||
let camera = &mut scene.camera;
|
||||
camera.init(scene.image_width as f32, scene.image_height as f32);
|
||||
scene.init();
|
||||
|
||||
// TODO: currently splits per vertical line, but could be more granular (per chunk)
|
||||
let mut pixels = vec![0 as u8; (scene.image_width * scene.image_height * 3) as usize];
|
||||
@@ -34,9 +33,11 @@ pub fn render(scene: &mut Scene) {
|
||||
.enumerate()
|
||||
.collect();
|
||||
|
||||
let now = Instant::now();
|
||||
scanlines.into_par_iter().for_each(|(i, chunk)| {
|
||||
render_chunk(chunk, scene, i as u32);
|
||||
});
|
||||
info!("rendering took {}.{} seconds.", now.elapsed().as_secs(), now.elapsed().subsec_nanos());
|
||||
|
||||
info!("Writing image file...");
|
||||
write_image(
|
||||
@@ -48,23 +49,22 @@ pub fn render(scene: &mut Scene) {
|
||||
}
|
||||
|
||||
pub fn render_chunk(chunk: &mut [u8], scene: &Scene, y: u32) {
|
||||
let camera = &scene.camera;
|
||||
let camera = &scene.get_camera();
|
||||
let aa_rate = &scene.get_camera().get_anti_alias_rate();
|
||||
|
||||
for i in 0..scene.image_width {
|
||||
let pixel_tl = camera.pixel00_loc + (i * camera.pixel_delta_u) + (y * camera.pixel_delta_v);
|
||||
// THOUGHT: the above seems more efficient (esp for larger aa rates) but can test for a
|
||||
// get_ray_no_tl() function and compare performance.
|
||||
let pixel_tl = camera.get_pixel_tl(i, y);
|
||||
|
||||
let mut pixel_colour = Colour::default();
|
||||
for y in 1..(camera.anti_alias_rate + 1) {
|
||||
for x in 1..(camera.anti_alias_rate + 1) {
|
||||
for y in 1..(aa_rate + 1) {
|
||||
for x in 1..(aa_rate + 1) {
|
||||
let r = camera.get_ray(pixel_tl, x, y);
|
||||
pixel_colour += ray_colour(&scene.objects, &r, scene.max_depth);
|
||||
}
|
||||
}
|
||||
|
||||
let (r, g, b) =
|
||||
(pixel_colour / (camera.anti_alias_rate * camera.anti_alias_rate) as f32).output();
|
||||
(pixel_colour / (aa_rate * aa_rate) as f32).output();
|
||||
chunk[(i * 3) as usize] = r;
|
||||
chunk[(i * 3) as usize + 1] = g;
|
||||
chunk[(i * 3) as usize + 2] = b;
|
||||
|
||||