examples: add overflow protection in path_tracing.v, fix randomization

pull/5395/head
Delyan Angelov 2020-06-15 19:56:19 +03:00
parent 62a872b8b5
commit 41e0561b05
1 changed files with 6 additions and 3 deletions

View File

@ -236,11 +236,10 @@ fn intersect(r Ray, spheres &Sphere, nspheres int) (bool, f64, int){
// some casual random function, try to avoid the 0 // some casual random function, try to avoid the 0
fn rand_f64() f64 { fn rand_f64() f64 {
x := (rand.intn(cache_len)+1) & 0x3FFF_FFFF x := rand.u32() & 0x3FFF_FFFF
return f64(x)/f64(0x3FFF_FFFF) return f64(x)/f64(0x3FFF_FFFF)
} }
const( const(
cache_len = 65536 // the 2*pi angle will be splitted in 65536 part cache_len = 65536 // the 2*pi angle will be splitted in 65536 part
cache_mask = cache_len - 1 // mask to speed-up the module process cache_mask = cache_len - 1 // mask to speed-up the module process
@ -270,6 +269,10 @@ const (
/******************* main function for the radiance calculation **************/ /******************* main function for the radiance calculation **************/
fn radiance(r Ray, depthi int, scene_id int) Vec { fn radiance(r Ray, depthi int, scene_id int) Vec {
if depthi > 1024 {
eprintln('depthi: $depthi')
return Vec{}
}
sin_tab := &f64( tabs.sin_tab ) sin_tab := &f64( tabs.sin_tab )
cos_tab := &f64( tabs.cos_tab ) cos_tab := &f64( tabs.cos_tab )
mut depth := depthi // actual depth in the reflection tree mut depth := depthi // actual depth in the reflection tree
@ -318,7 +321,7 @@ fn radiance(r Ray, depthi int, scene_id int) Vec {
//r1 := f64(2.0 * math.pi) * rand_f64() //r1 := f64(2.0 * math.pi) * rand_f64()
// tabbed speed-up // tabbed speed-up
r1 := rand.intn(cache_len) & cache_mask r1 := rand.u32() & cache_mask
r2 := rand_f64() r2 := rand_f64()
r2s := math.sqrt(r2) r2s := math.sqrt(r2)