main render loop

This commit is contained in:
CDaut 2023-11-17 20:44:21 +01:00
parent af215f8291
commit 11ceb6ebc7
4 changed files with 62 additions and 8 deletions

View file

@ -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"
cgmath = "0.18.0"
rayon = "1.8.0"

27
src/geometry.rs Normal file
View file

@ -0,0 +1,27 @@
use cgmath::Vector3;
use easy_gltf::Camera;
use easy_gltf::model::Triangle;
pub struct Ray {
source: Vector3<f64>,
direction: Vector3<f64>,
}
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<Vector3<f32>>;
}
impl Intersectable for Triangle {
//perform muller trumbore intersection
fn test_isec(&self, ray: &Ray) -> Option<Vector3<f32>> {
todo!()
}
}
pub fn construct_rays(camera: &Camera) -> Vec<Ray> {
todo!()
}

View file

@ -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<Scene> = &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)
}

View file

@ -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>, scene_idx: u32) {
pub fn render(scenes: &Vec<Scene>, scene_idx: usize, camera_idx: usize) {
//construct all rays and cast them all
let rays: Vec<Ray> = 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()) }
}
});
});
}