Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TRY-34 szenen raytracen (provisorisch) #12

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ image = { version = "0.24.7", default-features = false, features = [
log = "0.4.20"
nalgebra = { version = "0.32.3", features = ["serde", "serde-serialize"] }
obj = "0.10.2"
ordered-float = "4.1.1"
simplelog = "0.12.1"
serde = { version = "1", features = ["derive"] }
serde_yaml = "0.8"
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use nalgebra::Vector3;
use simplelog::*;
use std::fs::File;

mod raytracer;
mod scene;

pub type Color = Vector3<f32>;
Expand Down
46 changes: 46 additions & 0 deletions src/raytracer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use nalgebra::{Point3, Vector3};
use obj::Material;
use ordered_float::OrderedFloat;

use crate::{scene::Scene, Color};

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Ray {
pub origin: Point3<f32>,
pub direction: Vector3<f32>,
}

#[derive(Debug, PartialEq)]
pub struct Hit<'a> {
pub point: Point3<f32>,
pub normal: Vector3<f32>,
pub material: Option<&'a Material>,
}

pub struct Raytracer<'a> {
scene: &'a Scene,
background_color: Color,
}

impl<'a> Raytracer<'a> {
pub fn new(scene: &'a Scene, background_color: Vector3<f32>) -> Raytracer<'a> {
Raytracer {
scene,
background_color,
}
}

fn raycast(&self, ray: Ray) -> Option<Hit> {
self.scene
.objects
.iter()
.filter_map(|o| o.intersect(ray))
.min_by_key(|h| OrderedFloat((h.point - ray.origin).norm()))
}

pub fn render(&self, ray: Ray) -> Color {
self.raycast(ray)
.map(|_| Color::new(1.0, 0.0, 0.0))
.unwrap_or(self.background_color)
}
}
8 changes: 4 additions & 4 deletions src/scene/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use serde::Deserialize;

#[derive(Debug, Clone, PartialEq)]
pub struct Camera {
position: Point3<f32>,
direction: Vector3<f32>,
up: Vector3<f32>,
fov: f32,
pub position: Point3<f32>,
pub direction: Vector3<f32>,
pub up: Vector3<f32>,
pub fov: f32,
}

mod yaml {
Expand Down
6 changes: 3 additions & 3 deletions src/scene/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::Color;

#[derive(Debug, Clone, PartialEq)]
pub struct Light {
position: Point3<f32>,
color: Color,
intensity: f32,
pub position: Point3<f32>,
pub color: Color,
pub intensity: f32,
}

impl<'de> Deserialize<'de> for Light {
Expand Down
8 changes: 4 additions & 4 deletions src/scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ mod triangle;

#[derive(Debug, Clone, PartialEq)]
pub struct Scene {
objects: Vec<Object>,
lights: Vec<Light>,
camera: Camera,
settings: Settings,
pub objects: Vec<Object>,
pub lights: Vec<Light>,
pub camera: Camera,
pub settings: Settings,
}

impl<'de> Deserialize<'de> for Scene {
Expand Down
38 changes: 37 additions & 1 deletion src/scene/object.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use anyhow::Context;
use log::warn;
use nalgebra::{Point, Similarity3, Vector3};
use nalgebra::{Point, Point3, Similarity3, Vector3};
use obj::Material;
use ordered_float::OrderedFloat;

use crate::raytracer::{Hit, Ray};

use super::triangle::Triangle;

Expand Down Expand Up @@ -130,3 +133,36 @@ impl Object {
})
}
}

impl Object {
pub fn intersect(&self, ray: Ray) -> Option<Hit> {
// Transform ray into object space
let ray = Ray {
origin: self.transform.inverse_transform_point(&ray.origin),
direction: self.transform.inverse_transform_vector(&ray.direction),
};

self.triangles
.iter()
.filter_map(|t| t.intersect(ray).map(|h| (t, h)))
.map(|(t, (u, v, w))| {
let material = t.material_index.map(|i| &self.materials[i]);
let normal = ((t.a_normal * u) + (t.b_normal * v) + (t.c_normal * w)).normalize();
let point = Point3::from((t.a.coords * u) + (t.b.coords * v) + (t.c.coords * w));
Hit {
point,
normal,
material,
}
})
.min_by_key(|h| OrderedFloat((h.point - ray.origin).norm()))
.map(|h| {
// Transform hit back into world space
Hit {
point: self.transform.transform_point(&h.point),
normal: self.transform.transform_vector(&h.normal),
..h
}
})
}
}
4 changes: 2 additions & 2 deletions src/scene/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Settings {
max_bounces: usize,
samples: usize,
pub max_bounces: u32,
pub samples: u32,
}
41 changes: 41 additions & 0 deletions src/scene/triangle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use nalgebra::{Point3, Vector3};

use crate::raytracer::Ray;

#[derive(Debug, Clone, PartialEq)]
pub struct Triangle {
pub a: Point3<f32>,
Expand All @@ -10,3 +12,42 @@ pub struct Triangle {
pub c_normal: Vector3<f32>,
pub material_index: Option<usize>,
}

impl Triangle {
/// return barycentric coordinates if ray intersects triangle
pub fn intersect(&self, ray: Ray) -> Option<(f32, f32, f32)> {
let edge1 = self.b - self.a;
let edge2 = self.c - self.a;
let h = ray.direction.cross(&edge2);
let a = edge1.dot(&h);

if a.abs() < 1e-8 {
return None; // This ray is parallel to this triangle.
}

let f = 1.0 / a;
let s = ray.origin - self.a;
let u = f * s.dot(&h);

if u < 0.0 || u > 1.0 {
return None; // The intersection point is outside the triangle.
}

let q = s.cross(&edge1);
let v = f * ray.direction.dot(&q);

if v < 0.0 || u + v > 1.0 {
return None; // The intersection point is outside the triangle.
}

let t = f * edge2.dot(&q);

if t > 1e-8 {
let w = 1.0 - u - v;

Some((u, v, w))
} else {
None
}
}
}