erebus

annotate liberebus/src/rt.cc @ 21:e49f4d7ad04c

started adding BRDFs
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 28 May 2014 07:06:29 +0300
parents e9da2916bc79
children 56d504cc555a
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@17 7 Color ray_trace(struct erebus *ctx, const Ray &ray, int iter)
nuclear@6 8 {
nuclear@17 9 const Scene *scn = ctx->scn;
nuclear@17 10 if(!scn) {
nuclear@17 11 return Color(1, 0, 0);
nuclear@17 12 }
nuclear@17 13
nuclear@6 14 RayHit hit;
nuclear@6 15 if(!(scn->intersect(ray, &hit))) {
nuclear@8 16 return scn->get_env_color(ray);
nuclear@6 17 }
nuclear@6 18
nuclear@17 19 return shade(ctx, hit, iter);
nuclear@6 20 }
nuclear@6 21
nuclear@17 22 Color shade(struct erebus *ctx, const RayHit &hit, int iter)
nuclear@6 23 {
nuclear@8 24 assert(hit.obj->get_type() == ObjType::geom);
nuclear@17 25 int max_iter = erb_getopti(ctx, ERB_OPT_MAX_ITER);
nuclear@17 26 const Scene *scn = ctx->scn;
nuclear@8 27 const GeomObject *obj = (const GeomObject*)hit.obj;
nuclear@8 28 const Material *mtl = &obj->mtl;
nuclear@8 29 const Reflectance *brdf = obj->brdf;
nuclear@8 30 const Ray &ray = hit.world_ray;
nuclear@8 31
nuclear@17 32 Vector3 norm = hit.calc_normal();
nuclear@18 33 //return norm * 0.5 + Vector3(0.5, 0.5, 0.5);
nuclear@17 34 Vector2 texcoords = hit.calc_texcoords();
nuclear@8 35
nuclear@17 36 Color color = mtl->get_attrib_color("diffuse", texcoords.x, texcoords.y);
nuclear@8 37 Color res = mtl->get_attrib_color("emissive") + color * scn->get_env().ambient;
nuclear@8 38
nuclear@8 39 Vector3 sample_dir;
nuclear@8 40 float prob = brdf->sample(norm, -hit.world_ray.dir, &sample_dir);
nuclear@17 41 if(iter < max_iter && randf() <= prob) {
nuclear@8 42 Ray sample_ray;
nuclear@8 43 sample_ray.origin = ray.origin + ray.dir * hit.dist;
nuclear@8 44 sample_ray.dir = sample_dir;
nuclear@8 45
nuclear@17 46 res += ray_trace(ctx, sample_ray, iter + 1) * color;
nuclear@8 47 }
nuclear@8 48 return res;
nuclear@6 49 }