-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: prepare for multiple BVH construction algorithm support
- Loading branch information
Showing
20 changed files
with
186 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use std::time::Instant; | ||
|
||
use std::boxed::Box; | ||
|
||
use clovers::{ | ||
bvh::{BVHNode, BvhAlgorithm}, | ||
camera::{Camera, CameraInit}, | ||
hitable::Hitable, | ||
materials::SharedMaterial, | ||
objects::{object_to_hitable, Object}, | ||
scenes::Scene, | ||
Float, Vec, | ||
}; | ||
|
||
use palette::Srgb; | ||
use tracing::info; | ||
|
||
// TODO: better naming | ||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] | ||
/// A serialized representation of a [Scene]. | ||
pub struct SceneFile { | ||
time_0: Float, | ||
time_1: Float, | ||
background_color: Srgb, | ||
camera: CameraInit, | ||
objects: Vec<Object>, | ||
#[serde(default)] | ||
materials: Vec<SharedMaterial>, | ||
} | ||
|
||
impl SceneFile { | ||
/// Initializes a new [Scene] instance by parsing the contents of a [`SceneFile`] structure and then using those details to construct the [Scene]. | ||
#[must_use] | ||
pub fn initialize<'scene>( | ||
scene_file: SceneFile, | ||
bvh_algorithm: BvhAlgorithm, | ||
width: u32, | ||
height: u32, | ||
) -> Scene<'scene> { | ||
let time_0 = scene_file.time_0; | ||
let time_1 = scene_file.time_1; | ||
let background_color = scene_file.background_color; | ||
|
||
#[allow(clippy::cast_precision_loss)] | ||
let camera = Camera::new( | ||
scene_file.camera.look_from, | ||
scene_file.camera.look_at, | ||
scene_file.camera.up, | ||
scene_file.camera.vertical_fov, | ||
width as Float / height as Float, | ||
scene_file.camera.aperture, | ||
scene_file.camera.focus_distance, | ||
time_0, | ||
time_1, | ||
); | ||
let mut materials = scene_file.materials; | ||
materials.push(SharedMaterial::default()); | ||
let materials = Box::leak(Box::new(materials)); | ||
|
||
info!("Creating a flattened list from the objects"); | ||
let mut hitables: Vec<Hitable> = Vec::new(); | ||
let mut priority_hitables: Vec<Hitable> = Vec::new(); | ||
|
||
// TODO: this isn't the greatest ergonomics, but it gets the job done for now | ||
for object in scene_file.objects { | ||
if match &object { | ||
Object::Boxy(i) => i.priority, | ||
Object::ConstantMedium(i) => i.priority, | ||
Object::MovingSphere(i) => i.priority, | ||
Object::ObjectList(i) => i.priority, | ||
Object::Quad(i) => i.priority, | ||
Object::RotateY(i) => i.priority, | ||
Object::Sphere(i) => i.priority, | ||
Object::STL(i) => i.priority, | ||
Object::GLTF(i) => i.priority, | ||
Object::Translate(i) => i.priority, | ||
Object::Triangle(i) => i.priority, | ||
} { | ||
let hitable = object_to_hitable(object, materials); | ||
hitables.push(hitable.clone()); | ||
priority_hitables.push(hitable); | ||
} else { | ||
let hitable = object_to_hitable(object, materials); | ||
hitables.push(hitable.clone()); | ||
} | ||
} | ||
info!("All objects parsed"); | ||
|
||
info!("BVH tree build starting"); | ||
let start = Instant::now(); | ||
let bvh_root = BVHNode::from_list(bvh_algorithm, hitables); | ||
let end = Instant::now(); | ||
let duration = (end - start).as_millis(); | ||
info!("BVH tree build done in {duration} ms"); | ||
|
||
let mis_bvh_root = Hitable::BVHNode(BVHNode::from_list(bvh_algorithm, priority_hitables)); | ||
|
||
Scene { | ||
camera, | ||
bvh_root, | ||
mis_bvh_root, | ||
background_color, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.