erebus

view liberebus/src/rt.cc @ 9:d38e13d6063c

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 24 May 2014 17:22:53 +0300
parents bb006fb96f1b
children e9da2916bc79
line source
1 #include <assert.h>
2 #include "rt.h"
3 #include "erebus_impl.h"
5 #define MAX_ITER 8
7 Color ray_trace(const Ray &ray, const Scene *scn, int iter)
8 {
9 RayHit hit;
10 if(!(scn->intersect(ray, &hit))) {
11 return scn->get_env_color(ray);
12 }
14 return shade(hit, scn, iter);
15 }
17 Color shade(const RayHit &hit, const Scene *scn, int iter)
18 {
19 assert(hit.obj->get_type() == ObjType::geom);
20 const GeomObject *obj = (const GeomObject*)hit.obj;
21 const Material *mtl = &obj->mtl;
22 const Reflectance *brdf = obj->brdf;
23 const Ray &ray = hit.world_ray;
25 Vector3 norm = obj->calc_normal(hit);
26 Vector2 texcoords = obj->calc_texcoords(hit);
28 Color color = mtl->get_attrib_color("albedo", texcoords.x, texcoords.y);
29 Color res = mtl->get_attrib_color("emissive") + color * scn->get_env().ambient;
31 Vector3 sample_dir;
32 float prob = brdf->sample(norm, -hit.world_ray.dir, &sample_dir);
33 if(iter < MAX_ITER && randf() <= prob) {
34 Ray sample_ray;
35 sample_ray.origin = ray.origin + ray.dir * hit.dist;
36 sample_ray.dir = sample_dir;
38 res += ray_trace(sample_ray, scn, iter + 1) * color;
39 }
40 return res;
41 }