From 977b0e4152433b2a68e2b97fe5fe2c0ff6fb20d8 Mon Sep 17 00:00:00 2001 From: Julian T Date: Wed, 3 Feb 2021 17:29:27 +0100 Subject: Can render a simple sphere, without shading --- src/camera/film.rs | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) (limited to 'src/camera/film.rs') diff --git a/src/camera/film.rs b/src/camera/film.rs index 30fd2fe..3586374 100644 --- a/src/camera/film.rs +++ b/src/camera/film.rs @@ -1,5 +1,6 @@ use crate::core::*; use crate::Float; +use image::{RgbImage, Rgb}; /// Contains the necesary values when doing calculations /// @@ -14,7 +15,7 @@ pub struct Pixel { pub struct Film { size: Vector2i, - drawing_bound: Bound2i, + pub frame: Bound2i, pixels: Vec, } @@ -24,14 +25,12 @@ pub struct Film { /// This means that multiple threads can work on the same area and commit their changed when they /// are done. pub struct FilmTile { - bounds: Bound2i, - size: Vector2i, + pub bounds: Bound2i, + pub size: Vector2i, pixels: Vec, } -//const HalfPixel = Vector2f::new(0.5); - impl Pixel { fn new() -> Pixel { Pixel { @@ -44,6 +43,15 @@ impl Pixel { self.rgb += &(c * weight); self.samples += 1; } + + fn finalize_rgb(&self) -> [u8; 3] { + let (r, g, b) = self.rgb.to_rgb(255.0); + [ + r as u8, + g as u8, + b as u8, + ] + } } impl std::ops::AddAssign<&Self> for Pixel { @@ -58,7 +66,7 @@ impl Film { let area = size.x * size.y; Film { size, - drawing_bound: Bound2i::new(&Vector2i::new(0), &size), + frame: Bound2i::new(&Vector2i::new(0), &size), pixels: vec![Pixel::new(); area as usize], } } @@ -79,18 +87,34 @@ impl Film { pub fn commit_tile(&mut self, tile: &FilmTile) { let offset = tile.bounds.min; - for y in 0 ..= tile.size.y { + for y in 0 .. tile.size.y { let rowindex = (offset.y + y) * self.size.x; let prowindex = y * tile.size.x; - for x in 0 ..= tile.size.x { + for x in 0 .. tile.size.x { let index = offset.x + x + rowindex; let pindex: i32 = x + prowindex; self.pixels[index as usize] += &tile.pixels[pindex as usize]; } } + } + + pub fn finalize_image(&self) -> RgbImage { + let mut img = RgbImage::new(self.size.x as u32, self.size.y as u32); + + for y in 0..self.size.y { + let index = y * self.size.x; + for x in 0..self.size.x { + img.put_pixel( + x as u32, + y as u32, + Rgb(self.pixels[(index + x) as usize].finalize_rgb()), + ); + } + } + img } } -- cgit v1.2.3