erebus

annotate liberebus/src/rt.cc @ 10:506e114b7ca2

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