From c0fc3926e06acddcc02ef086fbda688d4709e5ba Mon Sep 17 00:00:00 2001 From: lamp Date: Sun, 21 Jan 2024 13:44:51 +0000 Subject: refactor display backends, add pixelflut backend --- src/display/image.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++ src/display/mod.rs | 17 ++++++++++++ src/display/pixel.rs | 14 ++++++++++ src/display/pixelflut.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 src/display/image.rs create mode 100644 src/display/mod.rs create mode 100644 src/display/pixel.rs create mode 100644 src/display/pixelflut.rs (limited to 'src/display') diff --git a/src/display/image.rs b/src/display/image.rs new file mode 100644 index 0000000..77d8c83 --- /dev/null +++ b/src/display/image.rs @@ -0,0 +1,68 @@ +use std::io::Write; + +use crate::vec3::Color; +use crate::display::{Display, Pixel}; + +pub struct Image { + width: usize, + height: usize, + data: Vec, +} + +impl Image { + pub fn new(width: usize, height: usize) -> Image { + let data = vec![ + Pixel { + color: Color { + x: 0.0, + y: 0.0, + z: 0.0 + }, + sample_count: 0 + }; + width * height + ]; + Image { + width, + height, + data, + } + } +} + +impl Display for Image { + fn add_sample(&mut self, x: usize, y: usize, color: Color) { + self.data + .get_mut((y * self.width) + x) + .unwrap() + .update(color); + } + + fn maybe_write(&self, output: &mut impl Write) { + output.write_fmt(format_args!("P3\n{} {}\n255\n", self.width, self.height)).unwrap(); + for y in (0..self.height).rev() { + for x in 0..self.width { + let pixel = self.data.get((y * self.width) + x).unwrap(); + let mut r = pixel.color.x; + let mut g = pixel.color.y; + let mut b = pixel.color.z; + + // Divide by the number of samples and perform gamma correction for gamma 2 + let scale = 1.0 / pixel.sample_count as f64; + r = (r * scale).sqrt(); + g = (g * scale).sqrt(); + b = (b * scale).sqrt(); + + output + .write_fmt(format_args!( + "{} {} {}\n", + (256.0 * r.clamp(0.0, 0.999)) as u32, + (256.0 * g.clamp(0.0, 0.999)) as u32, + (256.0 * b.clamp(0.0, 0.999)) as u32, + )) + .unwrap(); + } + } + } + fn maybe_update(&mut self) {} +} diff --git a/src/display/mod.rs b/src/display/mod.rs new file mode 100644 index 0000000..0a18e48 --- /dev/null +++ b/src/display/mod.rs @@ -0,0 +1,17 @@ +mod image; +pub use image::Image; +mod pixelflut; +pub use pixelflut::Pixelflut; + +mod pixel; +use pixel::Pixel; + +use std::io::Write; + +use crate::vec3::Color; + +pub trait Display { + fn add_sample(&mut self, x: usize, y: usize, color: Color); + fn maybe_write(&self, output: &mut impl Write); + fn maybe_update(&mut self); +} diff --git a/src/display/pixel.rs b/src/display/pixel.rs new file mode 100644 index 0000000..c9113d9 --- /dev/null +++ b/src/display/pixel.rs @@ -0,0 +1,14 @@ +use crate::vec3::Color; + +#[derive(Clone)] +pub struct Pixel { + pub color: Color, + pub sample_count: u32, +} + +impl Pixel { + pub fn update(&mut self, color: Color) { + self.color += color; + self.sample_count += 1; + } +} diff --git a/src/display/pixelflut.rs b/src/display/pixelflut.rs new file mode 100644 index 0000000..3a62f8d --- /dev/null +++ b/src/display/pixelflut.rs @@ -0,0 +1,69 @@ +use std::io::Write; +use std::net::{TcpStream}; + +use crate::vec3::Color; +use crate::display::{Display, Pixel}; + +pub struct Pixelflut { + socket: TcpStream, + x: usize, + y: usize, + width: usize, + height: usize, + data: Vec, +} + +impl Pixelflut { + pub fn new(addr: &str, x: usize, y: usize, width: usize, height: usize) -> Self { + let data = vec![ + Pixel { + color: Color { + x: 0.0, + y: 0.0, + z: 0.0 + }, + sample_count: 0 + }; + width * height + ]; + let mut ret = Self { + socket: TcpStream::connect(addr).expect("Could not connect to Pixelflut!"), + x, + y, + width, + height, + data, + }; + ret.maybe_update(); + ret + } +} + +impl Display for Pixelflut { + fn add_sample(&mut self, x: usize, y: usize, color: crate::vec3::Color) { + self.data + .get_mut((y * self.width) + x) + .unwrap() + .update(color); + } + + fn maybe_write(&self, _: &mut impl std::io::prelude::Write) {} + + fn maybe_update(&mut self) { + for y in 0..self.height { + for x in 0..self.width { + let pixel = self.data.get((y * self.width) + x).unwrap(); + let scale = 1.0 / pixel.sample_count as f64; + let mut r = (pixel.color.x * scale).sqrt(); + let mut g = (pixel.color.y * scale).sqrt(); + let mut b = (pixel.color.z * scale).sqrt(); + self.socket.write(format!("PX {} {} {:02x}{:02x}{:02x}\n", + self.x + x, + self.y + self.height - y, + (256.0 * r.clamp(0.0, 0.999)) as u32, + (256.0 * g.clamp(0.0, 0.999)) as u32, + (256.0 * b.clamp(0.0, 0.999)) as u32).as_bytes()); + } + } + } +} -- cgit v1.2.3