erebus
diff liberebus/src/rt.cc @ 8:e2d9bf168a41
semi-works ...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 24 May 2014 06:12:57 +0300 |
parents | bb006fb96f1b |
children | e9da2916bc79 |
line diff
1.1 --- a/liberebus/src/rt.cc Sat May 24 02:27:08 2014 +0300 1.2 +++ b/liberebus/src/rt.cc Sat May 24 06:12:57 2014 +0300 1.3 @@ -1,16 +1,41 @@ 1.4 +#include <assert.h> 1.5 #include "rt.h" 1.6 +#include "erebus_impl.h" 1.7 + 1.8 +#define MAX_ITER 8 1.9 1.10 Color ray_trace(const Ray &ray, const Scene *scn, int iter) 1.11 { 1.12 RayHit hit; 1.13 if(!(scn->intersect(ray, &hit))) { 1.14 - return Color(0, 0, 0, 0); 1.15 + return scn->get_env_color(ray); 1.16 } 1.17 1.18 - return shade(hit, iter); 1.19 + return shade(hit, scn, iter); 1.20 } 1.21 1.22 -Color shade(const RayHit &hit, int iter) 1.23 +Color shade(const RayHit &hit, const Scene *scn, int iter) 1.24 { 1.25 - return Color(1, 0, 0, 1); 1.26 + assert(hit.obj->get_type() == ObjType::geom); 1.27 + const GeomObject *obj = (const GeomObject*)hit.obj; 1.28 + const Material *mtl = &obj->mtl; 1.29 + const Reflectance *brdf = obj->brdf; 1.30 + const Ray &ray = hit.world_ray; 1.31 + 1.32 + Vector3 norm = obj->calc_normal(hit); 1.33 + Vector2 texcoords = obj->calc_texcoords(hit); 1.34 + 1.35 + Color color = mtl->get_attrib_color("albedo", texcoords.x, texcoords.y); 1.36 + Color res = mtl->get_attrib_color("emissive") + color * scn->get_env().ambient; 1.37 + 1.38 + Vector3 sample_dir; 1.39 + float prob = brdf->sample(norm, -hit.world_ray.dir, &sample_dir); 1.40 + if(iter < MAX_ITER && randf() <= prob) { 1.41 + Ray sample_ray; 1.42 + sample_ray.origin = ray.origin + ray.dir * hit.dist; 1.43 + sample_ray.dir = sample_dir; 1.44 + 1.45 + res += ray_trace(sample_ray, scn, iter + 1) * color; 1.46 + } 1.47 + return res; 1.48 }