Skip to content

Commit

Permalink
finish codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
yichuan520030910320 committed Aug 4, 2021
1 parent 686d105 commit 5e38349
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 53 deletions.
3 changes: 2 additions & 1 deletion raytracer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ rusttype = "0.9"
rand = "0.8.3"
threadpool = "1.8"
tobj = "3.0.1"
raytracer_codegen = { path = "../raytracer_codegen" }
raytracer_codegen = { path = "../raytracer_codegen" }
yaml-rust = "0.4"
42 changes: 6 additions & 36 deletions raytracer/src/hittable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1728,46 +1728,17 @@ impl<T1: StaticHittable, T2: Clone + texture::Texture>
}
}

pub struct StaticBvhNode {
pub left: Arc<dyn StaticHittable>,
pub right: Arc<dyn StaticHittable>,
pub struct StaticBvhNode<T1:StaticHittable,T2:StaticHittable> {
pub left: Arc<T1>,
pub right: Arc<T2>,
pub box1: Aabb,
}

impl StaticBvhNode {
impl<T1:StaticHittable,T2:StaticHittable> StaticBvhNode<T1, T2> {
#[allow(dead_code)]
pub fn new(src_objects: Vec<Arc<dyn StaticHittable>>, time0: f64, time1: f64) -> Self {
let span = src_objects.len();
let mut objects = src_objects;
let axis = rand::thread_rng().gen_range(0..3);

let left: Arc<dyn StaticHittable>;
let right: Arc<dyn StaticHittable>;
if span == 1 {
left = objects.remove(0);
right = left.clone();
} else if span == 2 {
objects.sort_by(|a, b| {
let x = a.bounding_box(time0, time1).unwrap().minimun.get(axis);
let y = b.bounding_box(time0, time1).unwrap().minimun.get(axis);
x.partial_cmp(&y).unwrap()
});
right = objects.remove(1);
left = objects.remove(0);
} else {
objects.sort_by(|a, b| {
let x = a.bounding_box(time0, time1).unwrap().minimun.get(axis);
let y = b.bounding_box(time0, time1).unwrap().minimun.get(axis);
x.partial_cmp(&y).unwrap()
});
let mid = span / 2;
let (object0, object1) = objects.split_at_mut(mid as usize);
left = Arc::new(StaticBvhNode::new(object0.to_vec(), time0, time1));
right = Arc::new(StaticBvhNode::new(object1.to_vec(), time0, time1));
}
pub fn new(left:Arc<T1>,right:Arc<T2>, time0: f64, time1: f64) -> Self {
let box11 = left.bounding_box(time0, time1).unwrap();
let box22 = right.bounding_box(time0, time1).unwrap();

Self {
left,
right,
Expand All @@ -1777,7 +1748,7 @@ impl StaticBvhNode {
}

#[allow(clippy::needless_return)]
impl StaticHittable for StaticBvhNode {
impl <T1:StaticHittable,T2:StaticHittable>StaticHittable for StaticBvhNode<T1, T2> {
fn hit(&self, r: Ray, t_min: f64, t_max: f64) -> Option<StaticHitrecord> {
if !self.box1.hit(&r, t_min, t_max) {
return None;
Expand All @@ -1799,7 +1770,6 @@ impl StaticHittable for StaticBvhNode {
}
}
}

fn bounding_box(&self, _: f64, _: f64) -> Option<Aabb> {
let outout = self.box1;
Some(outout)
Expand Down
11 changes: 9 additions & 2 deletions raytracer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ mod vec3;
#[allow(unused_imports)]
use crate::run::{run, runstatic};
fn main() {
run();
//runstatic();
let num=2;
match num {
1=>run(),
2=>runstatic(),
_ => {
println!("please rechoose the type");
}
}

}
1 change: 0 additions & 1 deletion raytracer/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,6 @@ impl<T: Texture> StaticMaterial for StaticDiffuseLight<T> {
if rec.front_face {
return self.emit.value(u, v, p);
}

return self.emit.value(u, v, p);
}
}
Expand Down
20 changes: 14 additions & 6 deletions raytracer/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use crate::scence::{
my_scence_ball_world, obj, obj_with_texture, random_sence, simple_light, two_berlin_spheres,
two_spheres,
};
use crate::staticscence::{
static_cornell_box, static_earth, static_random_sence, two_texture_static,
};
use crate::staticscence::{static_cornell_box, static_earth, static_random_sence, two_texture_static, static_bvh_random_scence};
use crate::texture::StaticBaseColor;
pub use crate::vec3::Vec3;
use image::{ImageBuffer, RgbImage};
Expand Down Expand Up @@ -111,13 +109,13 @@ pub(crate) fn run() {
// let image_width = 400 as u32;
let mut image_width = 600_u32;
let mut image_heigth = (image_width as f64 / ratio) as u32;
let sample_per_pixel = 800; //ought to be 100 可以做的更大比如500//
let sample_per_pixel = 8; //ought to be 100 可以做的更大比如500//
let max_depth = 50; //an bo modifyed to lessen the time
let mut backgroud = Vec3::new(0.0, 0.0, 0.0);
let mut lookfrom = Vec3::new(278.0, 278.0, -800.0); //13 2 3
let mut lookat = Vec3::new(278.0, 278.0, 0.0);
let mut vfov = 40.0;
let number = 12;
let number = 6;
let mut world = HittableList { objects: vec![] };
match number {
1 => {
Expand Down Expand Up @@ -365,7 +363,7 @@ pub fn runstatic() {
let mut lookfrom = Vec3::new(278.0, 278.0, -800.0); //13 2 3
let mut lookat = Vec3::new(278.0, 278.0, 0.0);
let mut vfov = 40.0;
let number = 4;
let number = 5;
let mut world = StaticHittableList { objects: vec![] };
match number {
1 => {
Expand Down Expand Up @@ -399,6 +397,16 @@ pub fn runstatic() {
lookat = Vec3::new(278.0, 278.0, 0.0);
lookfrom = Vec3::new(278.0, 278.0, -800.0);
}
5=>{
world = static_bvh_random_scence();
lookat = Vec3::new(0.0, 0.0, 0.0);
lookfrom = Vec3::new(13.0, 2.0, 3.0);
backgroud = Vec3::new(0.7, 0.8, 1.0);
vfov = 20.0;
ratio = 3.0 / 2.0;
image_width = 1200_u32;
image_heigth = (image_width as f64 / ratio) as u32;
}

_ => println!("you are wrong !! please choose the wonderful world you want to see again."),
}
Expand Down
105 changes: 100 additions & 5 deletions raytracer/src/staticscence.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::aarect::{StaticXyRect, StaticXzRect, StaticYzRect};
use crate::hittable::{
StaticBox1, StaticHittableList, StaticMovingSphere, StaticRotateY, StaticSphere,
StaticTranslate,
};
use crate::hittable::{StaticBox1, StaticHittableList, StaticMovingSphere, StaticRotateY, StaticSphere, StaticTranslate, StaticHittable, StaticBvhNode};
use crate::material::{
StaticDielectric, StaticDiffuseLight, StaticFlipFace, StaticLambertian, StaticMetal,
};
use crate::perlin::NoiseTexture;
use crate::run::{random_doouble, range_random_double, Vec3};
use crate::run::{random_doouble, range_random_double};
use crate::texture::{CheckerTexture, StaticBaseColor, StaticImageTexture};
use std::sync::Arc;
use crate::vec3::Vec3;


pub fn two_texture_static() -> StaticHittableList {
let mut world = StaticHittableList { objects: vec![] };
Expand Down Expand Up @@ -382,3 +381,99 @@ pub(crate) fn static_cornell_box() -> StaticHittableList {
world.add(light1_bonus);
world
}
use raytracer_codegen::random_scene_static_bvh;
random_scene_static_bvh!{}
pub fn static_bvh_random_scence()->StaticHittableList{

let mut world = StaticHittableList { objects: vec![] };
let checker = CheckerTexture::new(Vec3::new(0.2, 0.3, 0.1), Vec3::new(0.9, 0.9, 0.9));
let m=Vec3::new(1.0,1.0,1.0);
let ground = StaticSphere {
p: Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
},
normal: Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
},
t: 0.0,
center: Vec3 {
x: 0.0,
y: -1000.0,
z: 0.0,
},
radius: 1000.0,
mat_ptr: StaticLambertian::new1(checker),
};
world.add(Arc::new(ground));
let obj:Arc<dyn StaticHittable>=add_bvh_static();
world.add(obj);
let material1 = StaticSphere {
p: Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
},
normal: Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
},
t: 0.0,
center: Vec3 {
x: 0.0,
y: 1.0,
z: 0.0,
},
radius: 1.0,
mat_ptr: StaticDielectric::new(1.5),
};
world.add(Arc::new(material1));
let material2 = StaticSphere {
p: Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
},
normal: Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
},
t: 0.0,
center: Vec3 {
x: -4.0,
y: 1.0,
z: 0.0,
},
radius: 1.0,
mat_ptr: StaticLambertian::<StaticBaseColor>::new(Vec3::new(0.4, 0.2, 0.1)),
};
world.add(Arc::new(material2));

let material3 = StaticSphere {
p: Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
},
normal: Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
},
t: 0.0,
center: Vec3 {
x: 4.0,
y: 1.0,
z: 0.0,
},
radius: 1.0,
mat_ptr: StaticMetal::new(Vec3::new(0.7, 0.6, 0.5), 0.0),
};
world.add(Arc::new(material3));
world
}
7 changes: 7 additions & 0 deletions raytracer/src/vec3.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rand::Rng;
use std::f64::consts::PI;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
//use proc_macro::{TokenStream, Ident, Span};

//let secret_number = ;
fn random_doouble() -> f64 {
Expand All @@ -18,6 +19,12 @@ pub struct Vec3 {
pub z: f64,
}

// impl quote::ToTokens for Vec3 {
// fn to_tokens(&self, tokens: &mut TokenStream) {
// tokens.append(Ident::new("Vec3", Span::call_site()));
// }
// }

#[warn(unused_parens)]
impl Vec3 {
pub fn new(x: f64, y: f64, z: f64) -> Self {
Expand Down
12 changes: 10 additions & 2 deletions raytracer_codegen/src/codegen_vec.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use rand::Rng;
use std::f64::consts::PI;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use proc_macro2::{TokenStream, Span, Ident};
use syn::__private::TokenStreamExt;

//let secret_number = ;
fn random_doouble() -> f64 {
pub(crate) fn random_doouble() -> f64 {
rand::thread_rng().gen_range(1..101) as f64 / 102.0
}

fn range_random_double(min: f64, max: f64) -> f64 {
pub(crate) fn range_random_double(min: f64, max: f64) -> f64 {
min + (max - min) * random_doouble()
}

Expand All @@ -18,6 +20,12 @@ pub struct Vec3 {
pub z: f64,
}

impl quote::ToTokens for Vec3 {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
tokens.append(Ident::new("Vec3", Span::call_site()));
}
}

#[warn(unused_parens)]
impl Vec3 {
pub fn new(x: f64, y: f64, z: f64) -> Self {
Expand Down

0 comments on commit 5e38349

Please sign in to comment.