Add more scenes
This commit is contained in:
parent
7d38e87f6a
commit
b11e7a7a78
8 changed files with 116 additions and 20 deletions
|
|
@ -23,5 +23,5 @@ Ka 20 20 20
|
|||
Kd 1 1 1
|
||||
Ks 0 0 0
|
||||
|
||||
newmtl glass
|
||||
newmtl mirror
|
||||
type glass
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ v 343.0 548.0 227.0
|
|||
v 343.0 548.0 332.0
|
||||
v 213.0 548.0 332.0
|
||||
v 213.0 548.0 227.0
|
||||
#f -4 -3 -2 -1
|
||||
f -4 -3 -2 -1
|
||||
|
||||
o ceiling
|
||||
usemtl white
|
||||
|
|
@ -72,7 +72,7 @@ v 556.0 548.8 0.0
|
|||
f -4 -3 -2 -1
|
||||
|
||||
o short_block
|
||||
usemtl glass
|
||||
usemtl mirror
|
||||
|
||||
v 130.0 165.0 65.0
|
||||
v 82.0 165.0 225.0
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
use crate::{
|
||||
parse_obj::ObjData,
|
||||
triangle_bvh::{BVHMaterial, Triangle, TriangleBVH},
|
||||
};
|
||||
use ray_tracing_core::{light::AreaLight, prelude::*};
|
||||
use ray_tracing_material::{diffuse::DiffuseMaterial, mirror::Mirror};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::triangle_bvh::{BVHMaterial, Triangle, TriangleBVH};
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub struct ExampleScene<R: Rng> {
|
||||
pub scene: TriangleBVH<R>,
|
||||
|
|
@ -12,18 +15,47 @@ pub struct ExampleScene<R: Rng> {
|
|||
pub horizontal_fov: Float,
|
||||
}
|
||||
|
||||
pub fn example_scenes<R: Rng>() -> HashMap<&'static str, fn() -> ExampleScene<R>> {
|
||||
pub fn example_scenes<R: Rng + Debug>() -> HashMap<&'static str, fn() -> ExampleScene<R>> {
|
||||
let mut map: HashMap<&str, fn() -> ExampleScene<R>> = HashMap::new();
|
||||
map.insert("basic_cornel", basic_cornel::<R>);
|
||||
// map.insert("cornel2", cornel2::<R>);
|
||||
map.insert("basic_cornel", basic_cornell::<R>);
|
||||
map.insert("cornel2", cornell2::<R>);
|
||||
map
|
||||
}
|
||||
|
||||
pub fn cornel2<R: Rng>() -> ExampleScene<R> {
|
||||
todo!()
|
||||
pub fn cornell2<R: Rng + Debug>() -> ExampleScene<R> {
|
||||
let obj = ObjData::new("ray-tracing-scene/obj/cornell_box.obj").unwrap();
|
||||
|
||||
let triangles = obj
|
||||
.triangle_faces()
|
||||
.map(|t| Triangle::new(t.v, t.mat.unwrap()))
|
||||
.collect();
|
||||
|
||||
let materials = obj
|
||||
.materials()
|
||||
.iter()
|
||||
.map(|m| match m.name.as_str() {
|
||||
"white" => BVHMaterial::new_material(DiffuseMaterial::new(Color::white())),
|
||||
"light" => BVHMaterial::new_light(AreaLight::new(Color::white() * 100.0)),
|
||||
"blue" => BVHMaterial::new_material(DiffuseMaterial::new(Color::new(0.0, 0.0, 1.0))),
|
||||
"red" => BVHMaterial::new_material(DiffuseMaterial::new(Color::new(1.0, 0.0, 0.0))),
|
||||
"green" => BVHMaterial::new_material(DiffuseMaterial::new(Color::new(0.0, 1.0, 0.0))),
|
||||
"mirror" => BVHMaterial::new_material(Mirror::new(Color::white())),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
let scene = dbg!(TriangleBVH::new(obj.vertices, triangles, materials));
|
||||
|
||||
ExampleScene {
|
||||
scene,
|
||||
camera_pos: Pos3::new(275.0, 275.0, -800.0),
|
||||
camera_dir: Dir3::new(0.0, 0.0, 1.0),
|
||||
camera_up: Dir3::up(),
|
||||
horizontal_fov: 90.0_f32.to_radians(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn basic_cornel<R: Rng>() -> ExampleScene<R> {
|
||||
pub fn basic_cornell<R: Rng>() -> ExampleScene<R> {
|
||||
let side_length = 1.5;
|
||||
let light_size = 0.5;
|
||||
let light_offset = 0.01;
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@ pub struct ObjData {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct ObjMaterial {
|
||||
name: String,
|
||||
map: HashMap<String, String>,
|
||||
pub name: String,
|
||||
pub map: HashMap<String, String>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -34,6 +34,13 @@ pub struct Face {
|
|||
pub mat: Option<u32>,
|
||||
}
|
||||
|
||||
pub struct TriangleFace {
|
||||
pub v: [u32; 3],
|
||||
pub t: Option<[u32; 3]>,
|
||||
pub n: Option<[u32; 3]>,
|
||||
pub mat: Option<u32>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RelativeFace {
|
||||
pub v: Vec<i32>,
|
||||
|
|
@ -400,4 +407,27 @@ impl ObjData {
|
|||
materials,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn vertices(&self) -> &Vec<Pos3> {
|
||||
&self.vertices
|
||||
}
|
||||
|
||||
pub fn faces(&self) -> &Vec<Face> {
|
||||
&self.faces
|
||||
}
|
||||
|
||||
pub fn triangle_faces(&self) -> impl Iterator<Item = TriangleFace> + '_ {
|
||||
self.faces.iter().flat_map(|f| {
|
||||
(1..f.v.len() - 1).map(|i| TriangleFace {
|
||||
v: [f.v[0], f.v[i], f.v[i + 1]],
|
||||
t: f.t.as_ref().map(|t| [t[0], t[i], t[i + 1]]),
|
||||
n: f.n.as_ref().map(|n| [n[0], n[i], n[i + 1]]),
|
||||
mat: f.mat,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub fn materials(&self) -> &Vec<ObjMaterial> {
|
||||
&self.materials
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,39 @@ pub struct BVHMaterial<R: Rng> {
|
|||
pub light: Option<Box<dyn Light<R>>>,
|
||||
}
|
||||
|
||||
impl<R: Rng> BVHMaterial<R> {
|
||||
pub fn new_material<M: Material<R> + 'static>(material: M) -> Self {
|
||||
Self {
|
||||
material: Some(Box::new(material)),
|
||||
light: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_light<L: Light<R> + 'static>(light: L) -> Self {
|
||||
Self {
|
||||
material: None,
|
||||
light: Some(Box::new(light)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_material_light<M: Material<R> + 'static, L: Light<R> + 'static>(
|
||||
material: M,
|
||||
light: L,
|
||||
) -> Self {
|
||||
Self {
|
||||
material: Some(Box::new(material)),
|
||||
light: Some(Box::new(light)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_empty() -> Self {
|
||||
Self {
|
||||
material: None,
|
||||
light: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum Node {
|
||||
Inner {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue