Draw first real image

This commit is contained in:
hal8174 2024-09-29 20:25:20 +02:00
parent 62b9fdcb56
commit 50d3874467
16 changed files with 565 additions and 56 deletions

View file

@ -0,0 +1,49 @@
use crate::prelude::*;
#[derive(Debug, Clone, Copy)]
pub struct AABB {
min: Pos3,
max: Pos3,
}
impl AABB {
pub fn new_unchecked(min: Pos3, max: Pos3) -> Self {
Self { min, max }
}
pub fn new(a: Pos3, b: Pos3) -> Self {
Self {
min: Pos3::new(
Float::min(a.x, b.x),
Float::min(a.y, b.y),
Float::min(a.z, b.z),
),
max: Pos3::new(
Float::max(a.x, b.x),
Float::max(a.y, b.y),
Float::max(a.z, b.z),
),
}
}
pub fn min(self) -> Pos3 {
self.min
}
pub fn max(self) -> Pos3 {
self.max
}
pub fn contains_point(self, pos: Pos3) -> bool {
self.min.x <= pos.x
&& pos.x <= self.max.x
&& self.min.y <= pos.y
&& pos.y <= self.max.y
&& self.min.z <= pos.z
&& pos.z <= self.max.z
}
pub fn intersect_ray(self, ray: Ray) -> Option<Float> {
todo!()
}
}

View file

@ -1,5 +1,5 @@
use crate::prelude::*;
use std::ops::{Add, AddAssign, Div};
use std::ops::{Add, AddAssign, Div, Mul, MulAssign};
#[derive(Debug, Clone, Copy)]
pub struct Color {
@ -32,6 +32,10 @@ impl Color {
pub fn black() -> Self {
Self::gray(0.0)
}
pub fn white() -> Self {
Self::gray(1.0)
}
}
impl Add for Color {
@ -65,3 +69,35 @@ impl Div<Float> for Color {
}
}
}
impl Mul for Color {
type Output = Color;
fn mul(self, rhs: Self) -> Self::Output {
Color::new(self.r * rhs.r, self.g * rhs.g, self.b * rhs.b)
}
}
impl MulAssign for Color {
fn mul_assign(&mut self, rhs: Self) {
self.r *= rhs.r;
self.g *= rhs.g;
self.b *= rhs.b;
}
}
impl Mul<Float> for Color {
type Output = Color;
fn mul(self, rhs: Float) -> Self::Output {
Color::new(self.r * rhs, self.g * rhs, self.b * rhs)
}
}
impl Mul<Color> for Float {
type Output = Color;
fn mul(self, rhs: Color) -> Self::Output {
Color::new(self * rhs.r, self * rhs.g, self * rhs.b)
}
}

View file

@ -1,3 +1,4 @@
pub mod aabb;
pub mod camera;
pub mod color;
pub mod material;
@ -8,6 +9,7 @@ pub mod scene;
pub mod prelude {
pub type Float = f32;
pub use crate::aabb::AABB;
pub use crate::color::Color;
pub use crate::material::Material;
pub use crate::math::*;

View file

@ -2,14 +2,53 @@ use crate::prelude::*;
use rand::Rng;
/// All calculations for the material are done a tangent space of the intersection.
pub trait Material<R: Rng> {
fn eval(&self, w_in: Dir3, w_out: Dir3, rng: &mut R) -> Color;
pub trait Material<R: Rng>: Sync {
fn eval(&self, w_in: Dir3, w_out: Dir3, rng: &mut R) -> Option<Color> {
None
}
fn emit(&self, w_in: Dir3, rng: &mut R) -> Option<Color> {
None
}
}
pub struct DefaultMaterial {}
impl<R: Rng> Material<R> for DefaultMaterial {
fn eval(&self, _w_in: Dir3, _w_out: Dir3, rng: &mut R) -> Color {
Color::black()
// evaluates the bsdf
fn eval(&self, _w_in: Dir3, _w_out: Dir3, _rng: &mut R) -> Option<Color> {
None
}
}
pub struct DiffuseMaterial {
color: Color,
}
impl DiffuseMaterial {
pub fn new(color: Color) -> Self {
Self { color }
}
}
impl<R: Rng> Material<R> for DiffuseMaterial {
fn eval(&self, _w_in: Dir3, _w_out: Dir3, _rng: &mut R) -> Option<Color> {
Some(self.color)
}
}
pub struct AreaLight {
color: Color,
}
impl AreaLight {
pub fn new(color: Color) -> Self {
Self { color }
}
}
impl<R: Rng> Material<R> for AreaLight {
fn emit(&self, _w_in: Dir3, _rng: &mut R) -> Option<Color> {
Some(self.color)
}
}

View file

@ -1,4 +1,5 @@
use crate::prelude::*;
use rand::Rng;
use std::ops::{Add, Div, Mul, Neg, Sub};
#[derive(Debug, Clone, Copy)]
@ -60,6 +61,17 @@ impl Dir3 {
self.x * rhs.y - self.y * rhs.x,
)
}
pub fn generate_uniform_hemisphere<R: Rng>(rng: &mut R) -> Self {
let theta = Float::acos(1.0 - rng.gen::<Float>());
let phi = 2.0 * FloatConsts::PI * rng.gen::<Float>();
let r = Float::sin(theta);
Dir3::new(Float::sin(phi) * r, Float::cos(theta), Float::cos(phi) * r)
}
pub fn generate_cosine_hemisphere<R: Rng>(rng: &mut R) -> Self {
todo!()
}
}
impl Add for Dir3 {

View file

@ -0,0 +1,46 @@
use crate::prelude::*;
#[derive(Debug, Clone, Copy)]
pub struct Frame {
m: Mat3,
}
impl Frame {
pub fn from_normal(n: Dir3) -> Self {
let n = n.normalize();
let x = if n.x().abs() > n.y().abs() {
Dir3::new(n.z(), 0.0, -n.x())
} else {
Dir3::new(0.0, n.z(), -n.y())
}
.normalize();
Self {
m: Mat3::new([x, n, Dir3::cross(x, n)]),
}
}
pub fn to_frame(&self, dir: Dir3) -> Dir3 {
self.m.transform_transposed(dir)
}
pub fn to_world(&self, dir: Dir3) -> Dir3 {
self.m.transform(dir)
}
}
#[cfg(test)]
mod test {
use super::Frame;
use crate::prelude::*;
#[test]
fn frame() {
let f = Frame::from_normal(Dir3::new(-1.0, 0.0, 0.0));
dbg!(f);
dbg!(f.to_world(Dir3::new(0.0, 1.0, 0.0)));
panic!()
}
}

View file

@ -0,0 +1,24 @@
use crate::prelude::*;
#[derive(Debug, Clone, Copy)]
pub struct Mat3 {
v: [Dir3; 3],
}
impl Mat3 {
pub fn new(v: [Dir3; 3]) -> Self {
Self { v }
}
pub fn transform(&self, dir: Dir3) -> Dir3 {
self.v[0] * dir.x + self.v[1] * dir.y + self.v[2] * dir.z
}
pub fn transform_transposed(&self, dir: Dir3) -> Dir3 {
Dir3::new(
Dir3::dot(self.v[0], dir),
Dir3::dot(self.v[1], dir),
Dir3::dot(self.v[2], dir),
)
}
}

View file

@ -1,5 +1,9 @@
pub mod dir3;
pub mod frame;
pub mod mat3;
pub mod pos3;
pub use dir3::Dir3;
pub use frame::Frame;
pub use mat3::Mat3;
pub use pos3::Pos3;

View file

@ -23,4 +23,8 @@ impl Ray {
pub fn time(self) -> Float {
self.time
}
pub fn at(self, t: Float) -> Pos3 {
self.start + self.dir * t
}
}

View file

@ -9,6 +9,82 @@ pub trait ClassicalRenderer<R: Rng> {
fn height(&self) -> u32;
}
pub struct PathTracer<S, C, R>
where
S: Scene<R>,
C: Camera<R>,
R: Rng,
{
scene: S,
camera: C,
rng: PhantomData<R>,
}
impl<S, C, R> PathTracer<S, C, R>
where
S: Scene<R>,
C: Camera<R>,
R: Rng,
{
pub fn new(scene: S, camera: C) -> Self {
Self {
scene,
camera,
rng: PhantomData {},
}
}
}
impl<S, C, R> ClassicalRenderer<R> for PathTracer<S, C, R>
where
S: Scene<R>,
C: Camera<R>,
R: Rng,
{
fn render_pixel(&self, x: u32, y: u32, rng: &mut R) -> Color {
let mut sum = Color::black();
let mut alpha = Color::white();
let mut r = self.camera.forward(x, y, rng);
let mut count = 0;
while let Some(i) = self.scene.intersect(r, 0.001, Float::INFINITY) {
if count > 4 {
break;
}
let frame = i.tangent_frame();
let w_in = frame.to_frame(-r.dir());
let w_out = Dir3::generate_uniform_hemisphere(rng);
let mat = i.material();
if let Some(c) = mat.emit(w_in, rng) {
sum += alpha * c * w_out.y();
}
if let Some(c) = mat.eval(w_in, w_out, rng) {
alpha *= c;
} else {
return sum;
}
r = Ray::new(r.at(i.t()), frame.to_world(w_out), r.time());
count += 1;
}
sum
}
fn width(&self) -> u32 {
self.camera.width()
}
fn height(&self) -> u32 {
self.camera.height()
}
}
pub struct DepthRenderer<S, C, R>
where
S: Scene<R>,
@ -47,6 +123,7 @@ where
if let Some(i) = self.scene.intersect(r, 0.0, Float::INFINITY) {
// Color::gray(1.0 / i.t())
let c = 0.5 * (i.normal() + Dir3::new(1.0, 1.0, 1.0));
// let c = i.normal();
Color::new(c.x, c.y, c.z)
} else {
Color::black()

View file

@ -31,4 +31,8 @@ impl<'sc, R: Rng> Intersection<'sc, R> {
pub fn material(&self) -> &'sc dyn Material<R> {
self.material
}
pub fn tangent_frame(&self) -> Frame {
Frame::from_normal(self.normal)
}
}