From 11ceb6ebc768f9f8b6c731ec36cad0dc83c2d4b4 Mon Sep 17 00:00:00 2001 From: CDaut Date: Fri, 17 Nov 2023 20:44:21 +0100 Subject: [PATCH] main render loop --- Cargo.toml | 5 ++--- src/geometry.rs | 27 +++++++++++++++++++++++++++ src/main.rs | 14 ++++++++++---- src/renderer.rs | 24 +++++++++++++++++++++++- 4 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 src/geometry.rs diff --git a/Cargo.toml b/Cargo.toml index 7665320..c2e914f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,5 @@ edition = "2021" [dependencies] easy-gltf = "1.1.1" clap = { version = "4.4.8", features = ["derive"] } - -[alias] -runx = "run /home/clemens/repositorys/raytrace-rs/scenes/cube_on_plane.glb 0" \ No newline at end of file +cgmath = "0.18.0" +rayon = "1.8.0" \ No newline at end of file diff --git a/src/geometry.rs b/src/geometry.rs new file mode 100644 index 0000000..ad7626f --- /dev/null +++ b/src/geometry.rs @@ -0,0 +1,27 @@ +use cgmath::Vector3; +use easy_gltf::Camera; +use easy_gltf::model::Triangle; + +pub struct Ray { + source: Vector3, + direction: Vector3, +} + +pub trait Intersectable { + /* + tests whether the ray intersects the Intersectable. + returns: The intersection point or empty if there is none + */ + fn test_isec(&self, ray: &Ray) -> Option>; +} + +impl Intersectable for Triangle { + //perform muller trumbore intersection + fn test_isec(&self, ray: &Ray) -> Option> { + todo!() + } +} + +pub fn construct_rays(camera: &Camera) -> Vec { + todo!() +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1e49cba..29b1d1b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,21 @@ mod renderer; +mod geometry; use std::string::String; use clap::{Parser}; +use easy_gltf::Scene; use crate::renderer::render; #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] struct Args { gltf_file_path: String, - //which scene to use in the gltf file - scene_index: u32, + /// which scene to use in the gltf file + #[arg(short, long)] + scene_index: usize, + /// which camera to render from + #[arg(short, long)] + camera_index: usize, } fn main() { @@ -17,10 +23,10 @@ fn main() { let args = Args::parse(); //load gltf scene - let scenes = easy_gltf::load( + let scenes: &Vec = &easy_gltf::load( &args.gltf_file_path) .expect(&*format!("Failed to load glTF file {}", &args.gltf_file_path)); - render(scenes, args.scene_index) + render(scenes, args.scene_index, args.camera_index) } diff --git a/src/renderer.rs b/src/renderer.rs index 1ea7884..401d6b6 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -1,4 +1,26 @@ +use rayon::iter::{IntoParallelIterator, ParallelIterator}; +use easy_gltf::model::{Mode}; use easy_gltf::Scene; +use crate::geometry::{construct_rays, Intersectable, Ray}; -pub fn render(scenes: Vec, scene_idx: u32) { + +pub fn render(scenes: &Vec, scene_idx: usize, camera_idx: usize) { + //construct all rays and cast them all + let rays: Vec = construct_rays(&scenes[scene_idx].cameras[camera_idx]); + rays.into_par_iter().for_each(|ray| { + //test intersection with all models in the scene + //TODO: Improve, to avoid iterating all models + scenes[scene_idx].models.iter().for_each(|model| { + match model.mode() { + Mode::Triangles => { + //in triangle mode there will always be a triangle vector + let triangles = model.triangles().unwrap(); + triangles.iter().for_each(|triangle| { + triangle.test_isec(&ray); + }); + } + _ => { panic!("Unable to render model in mode {:?}", model.mode()) } + } + }); + }); } \ No newline at end of file