Skip to content

Commit

Permalink
Added documentation, camera settings
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Nov 23, 2022
1 parent 05b3cb6 commit 0ca8310
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
This is a port of the excellent [GLSL_Pathtracer](https://github.com/knightcrawler25/GLSL-PathTracer) to Rust utilizing an abstracted, trait based backend. Perfect for rendering procedural content.

![Spheres](https://github.com/markusmoenig/rust-pathtracer/blob/master/images/spheres.png)
![Spheres](images/spheres.png)

### Rust Features

Expand All @@ -14,6 +14,7 @@ This is a port of the excellent [GLSL_Pathtracer](https://github.com/knightcrawl
* Implement a denoiser.
* Emitters are untested right now.
* Only spherical analytical lights right now.
* Document classes for crate.
* Implement a SDF based example scene.
* Implement a mesh based example scene. PRs welcome.
* Enhance the example renderer app with more options.
Expand Down
2 changes: 0 additions & 2 deletions rust-pathtracer/Readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
This is a port of the excellent [GLSL_Pathtracer](https://github.com/knightcrawler25/GLSL-PathTracer) to Rust utilizing an abstracted, trait based backend. Perfect for rendering procedural content.

![Spheres](https://github.com/markusmoenig/rust-pathtracer/blob/master/images/spheres.png)

### Rust Features

* Multi threaded using [rayon](https://github.com/rayon-rs/rayon).
Expand Down
3 changes: 2 additions & 1 deletion rust-pathtracer/src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

use crate::prelude::*;

/// A color buffer holding an array of either f32 or f64 pixels (as defined by PTF).
pub struct ColorBuffer {

pub width : usize,
Expand All @@ -23,7 +24,7 @@ impl ColorBuffer {
}
}

/// Convert the pixel buffer to an Vec<u8>
/// Convert the pixel buffer to an Vec<u8> and converts gamma the colors from linear into gamma space.
pub fn convert_to_u8(&self, frame: &mut [u8]) {
for y in 0..self.height {
for x in 0..self.width {
Expand Down
10 changes: 9 additions & 1 deletion rust-pathtracer/src/camera/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ pub mod pinhole;

use crate::prelude::*;

/// Trait for abstracting cameras.
#[allow(unused)]
pub trait Camera3D : Sync + Send {

fn new() -> Self where Self: Sized;
fn gen_ray(&self, p: Vector2<PTF>, fov: PTF, offset: PTF2, width: PTF, height: PTF) -> Ray;

/// Set the origin and center of the camera.
fn set(&mut self, origin: PTF3, center: PTF3);
/// Set the fov of the camera.
fn set_fov(&mut self, fov: PTF);

/// Generate a ray.
fn gen_ray(&self, p: Vector2<PTF>, offset: PTF2, width: PTF, height: PTF) -> Ray;
}
21 changes: 18 additions & 3 deletions rust-pathtracer/src/camera/pinhole.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

use crate::prelude::*;

/// A traditional pinhole camera with an origin, center and fov.
pub struct Pinhole {
origin : PTF3,
center : PTF3,

fov : PTF,
}

impl Camera3D for Pinhole {
Expand All @@ -15,17 +18,29 @@ impl Camera3D for Pinhole {

Self {
origin,
center
center,

fov : 80.0,
}
}

fn set(&mut self, origin: PTF3, center: PTF3) {
self.origin = origin;
self.center = center;
}

fn set_fov(&mut self, fov: PTF) {
self.fov = fov;
}


#[inline(always)]
fn gen_ray(&self, p: Vector2<PTF>, fov: PTF, offset: PTF2, width: PTF, height: PTF) -> [Vector3<PTF>; 2] {
fn gen_ray(&self, p: Vector2<PTF>, offset: PTF2, width: PTF, height: PTF) -> [Vector3<PTF>; 2] {
let ratio = width / height;

let pixel_size = PTF2::new( 1.0 / width, 1.0 / height);

let half_width = (fov.to_radians() * 0.5).tan();
let half_width = (self.fov.to_radians() * 0.5).tan();
let half_height = half_width / ratio;

let up_vector = PTF3::new(0.0, 1.0, 0.0);
Expand Down
8 changes: 6 additions & 2 deletions rust-pathtracer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//! This is a port of the excellent [GLSL_Pathtracer](https://github.com/knightcrawler25/GLSL-PathTracer) to Rust utilizing an abstracted, trait based backend.
//! Perfect for rendering procedural content.
//!
extern crate nalgebra_glm as glm;

/// Either f32 or f64
pub type PTF = f64;
pub type PTF2 = glm::DVec2;
pub type PTF3 = glm::DVec3;
Expand All @@ -16,7 +21,7 @@ const TWO_PI : PTF = std::f64::consts::PI * 2.0;
// const INV_PI : PTF = 1.0 / std::f32::consts::PI;
// const TWO_PI : PTF = std::f32::consts::PI * 2.0;

pub type Color = [PTF; 4];
/// Rays are stored in an array [origin, direction]
pub type Ray = [PTF3; 2];

pub mod tracer;
Expand All @@ -32,7 +37,6 @@ pub mod prelude {
pub use crate::PTF2;
pub use crate::PTF3;
pub use crate::PTF4;
pub use crate::Color;
pub use crate::Ray;

pub use nalgebra::*;
Expand Down
2 changes: 2 additions & 0 deletions rust-pathtracer/src/light.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

use crate::prelude::*;

/// An analytical light.
pub struct AnalyticalLight {
pub light : Light,
}

impl AnalyticalLight {

/// Creates a spherical light based on its position, radius and emission.
pub fn spherical(position: PTF3, radius: PTF, emission: PTF3) -> Self {

let light = Light {
Expand Down
3 changes: 3 additions & 0 deletions rust-pathtracer/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum MediumType {
Emissive
}

/// The Medium struct for volumetric objects.
#[derive(PartialEq, Clone, Debug)]
pub struct Medium {
pub medium_type : MediumType,
Expand Down Expand Up @@ -40,6 +41,7 @@ pub enum AlphaMode
Mask
}

/// The material struct holds all BSDF properties as well as the Medium.
#[derive(PartialEq, Clone, Debug)]
pub struct Material {
pub base_color : PTF3,
Expand All @@ -55,6 +57,7 @@ pub struct Material {
pub sheen_tint : PTF,
pub clearcoat : PTF,
pub clearcoat_gloss : PTF,
/// Do not use clearcoat_roughness directly, it is for internal use only. Use clearcoat_gloss.
pub clearcoat_roughness : PTF,

pub spec_trans : PTF,
Expand Down
1 change: 1 addition & 0 deletions rust-pathtracer/src/scene.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::prelude::*;

/// A trait based scene abstraction.
#[allow(unused)]
pub trait Scene : Sync + Send {

Expand Down
2 changes: 1 addition & 1 deletion rust-pathtracer/src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Tracer {
let mut rng = thread_rng();
let cam_offset = PTF2::new(rng.gen(), rng.gen());
let coord = Vector2::new(xx, 1.0 - yy);
let mut ray = self.scene.camera().gen_ray(coord, 80.0, cam_offset, width as PTF, height);
let mut ray = self.scene.camera().gen_ray(coord, cam_offset, width as PTF, height);

// -

Expand Down

0 comments on commit 0ca8310

Please sign in to comment.