examples: fix compilation of path_tracing.v
parent
f1f4e380a2
commit
93c44a2199
|
@ -30,17 +30,17 @@ import rand
|
||||||
import time
|
import time
|
||||||
|
|
||||||
const (
|
const (
|
||||||
inf = f64(1e+10)
|
inf = 1e+10
|
||||||
eps = f64(1e-4)
|
eps = 1e-4
|
||||||
f_0 = f64(0.0)
|
f_0 = 0.0
|
||||||
)
|
)
|
||||||
|
|
||||||
/***************************** 3D Vector utility struct **********************/
|
/***************************** 3D Vector utility struct **********************/
|
||||||
struct Vec {
|
struct Vec {
|
||||||
mut:
|
mut:
|
||||||
x f64 = f64(0.0)
|
x f64 = 0.0
|
||||||
y f64 = f64(0.0)
|
y f64 = 0.0
|
||||||
z f64 = f64(0.0)
|
z f64 = 0.0
|
||||||
}
|
}
|
||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
|
@ -79,7 +79,7 @@ fn (v Vec) cross (b Vec) Vec{
|
||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
fn (v Vec) norm () Vec {
|
fn (v Vec) norm () Vec {
|
||||||
tmp_norm := f64(1.0) / math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z)
|
tmp_norm := 1.0 / math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z)
|
||||||
return Vec{ v.x * tmp_norm , v.y * tmp_norm, v.z * tmp_norm }
|
return Vec{ v.x * tmp_norm , v.y * tmp_norm, v.z * tmp_norm }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,10 +91,11 @@ struct Image {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_image(w int, h int) Image {
|
fn new_image(w int, h int) Image {
|
||||||
|
vecsize := int(sizeof(Vec))
|
||||||
return Image{
|
return Image{
|
||||||
width: w,
|
width: w,
|
||||||
height: h,
|
height: h,
|
||||||
data: &Vec(vcalloc(sizeof(Vec)*w*h))
|
data: &Vec(vcalloc(vecsize*w*h))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,11 +130,11 @@ enum Refl_t {
|
||||||
|
|
||||||
/********************************* Sphere ************************************/
|
/********************************* Sphere ************************************/
|
||||||
struct Sphere {
|
struct Sphere {
|
||||||
rad f64 = f64(0.0) // radius
|
rad f64 = 0.0 // radius
|
||||||
p Vec // position
|
p Vec // position
|
||||||
e Vec // emission
|
e Vec // emission
|
||||||
c Vec // color
|
c Vec // color
|
||||||
refl Refl_t // reflection type => [diffuse, specular, refractive]
|
refl Refl_t // reflection type => [diffuse, specular, refractive]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (sp Sphere) intersect (r Ray) f64 {
|
fn (sp Sphere) intersect (r Ray) f64 {
|
||||||
|
@ -142,7 +143,7 @@ fn (sp Sphere) intersect (r Ray) f64 {
|
||||||
mut det := b * b - op.dot(op) + sp.rad * sp.rad
|
mut det := b * b - op.dot(op) + sp.rad * sp.rad
|
||||||
|
|
||||||
if det < 0 {
|
if det < 0 {
|
||||||
return f64(0)
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
det = math.sqrt(det)
|
det = math.sqrt(det)
|
||||||
|
@ -156,7 +157,7 @@ fn (sp Sphere) intersect (r Ray) f64 {
|
||||||
if t > eps {
|
if t > eps {
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
return f64(0)
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
/*********************************** Scenes **********************************
|
/*********************************** Scenes **********************************
|
||||||
|
@ -215,12 +216,12 @@ fn clamp(x f64) f64 {
|
||||||
|
|
||||||
[inline]
|
[inline]
|
||||||
fn to_int(x f64) int {
|
fn to_int(x f64) int {
|
||||||
p := math.pow(clamp(x), f64(1.0/2.2))
|
p := math.pow(clamp(x), 1.0/2.2)
|
||||||
return int(p*f64(255.0)+f64(0.5))
|
return int(p*255.0+0.5)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn intersect(r Ray, spheres &Sphere, nspheres int) (bool, f64, int){
|
fn intersect(r Ray, spheres &Sphere, nspheres int) (bool, f64, int){
|
||||||
mut d := f64(0)
|
mut d := 0.0
|
||||||
mut t := inf
|
mut t := inf
|
||||||
mut id := 0
|
mut id := 0
|
||||||
for i:=nspheres-1; i >= 0; i-- {
|
for i:=nspheres-1; i >= 0; i-- {
|
||||||
|
@ -253,7 +254,7 @@ mut:
|
||||||
|
|
||||||
fn new_tabs() Cache {
|
fn new_tabs() Cache {
|
||||||
mut c := Cache{}
|
mut c := Cache{}
|
||||||
inv_len := f64(1.0) / f64(cache_len)
|
inv_len := 1.0 / f64(cache_len)
|
||||||
for i in 0..cache_len {
|
for i in 0..cache_len {
|
||||||
x := f64(i) * math.pi * 2.0 * inv_len
|
x := f64(i) * math.pi * 2.0 * inv_len
|
||||||
c.sin_tab[i] = math.sin(x)
|
c.sin_tab[i] = math.sin(x)
|
||||||
|
@ -272,11 +273,11 @@ fn radiance(r Ray, depthi int, scene_id int) 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
|
||||||
mut t := f64(0) // distance to intersection
|
mut t := 0.0 // distance to intersection
|
||||||
mut id := 0 // id of intersected object
|
mut id := 0 // id of intersected object
|
||||||
mut res := false // result of intersect
|
mut res := false // result of intersect
|
||||||
|
|
||||||
v_1 := f64(1.0)
|
v_1 := 1.0
|
||||||
//v_2 := f64(2.0)
|
//v_2 := f64(2.0)
|
||||||
|
|
||||||
scene := spheres[scene_id]
|
scene := spheres[scene_id]
|
||||||
|
@ -393,9 +394,9 @@ fn ray_trace(w int, h int, samps int, file_name string, scene_id int) Image {
|
||||||
image := new_image(w, h)
|
image := new_image(w, h)
|
||||||
|
|
||||||
// inverse costants
|
// inverse costants
|
||||||
w1 := f64(1.0 / w)
|
w1 := f64(1.0 / f64(w))
|
||||||
h1 := f64(1.0 / h)
|
h1 := f64(1.0 / f64(h))
|
||||||
samps1 := f64(1.0 / samps)
|
samps1 := f64(1.0 / f64(samps))
|
||||||
|
|
||||||
cam := Ray{Vec{50, 52, 295.6}, Vec{0, -0.042612, -1}.norm()} // cam position, direction
|
cam := Ray{Vec{50, 52, 295.6}, Vec{0, -0.042612, -1}.norm()} // cam position, direction
|
||||||
cx := Vec{ f64(w) * 0.5135 / f64(h), 0, 0}
|
cx := Vec{ f64(w) * 0.5135 / f64(h), 0, 0}
|
||||||
|
|
Loading…
Reference in New Issue