erebus
view liberebus/src/rt.cc @ 29:fb20e3855740
write the rendered image when pressing '`' and on exit.
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 06 Jun 2014 15:08:09 +0300 |
parents | 4a0a288ffb27 |
children | 53a98c148bf8 |
line source
1 #include <assert.h>
2 #include "rt.h"
3 #include "erebus_impl.h"
5 #define MAX_ITER 6
7 Color ray_trace(struct erebus *ctx, const Ray &ray, int iter)
8 {
9 const Scene *scn = ctx->scn;
10 if(!scn) {
11 return Color(1, 0, 0);
12 }
14 RayHit hit;
15 if(!(scn->intersect(ray, &hit))) {
16 return scn->get_env_color(ray);
17 }
19 return shade(ctx, hit, iter);
20 }
22 Color shade(struct erebus *ctx, const RayHit &hit, int iter)
23 {
24 assert(hit.obj->get_type() == ObjType::geom);
25 int max_iter = erb_getopti(ctx, ERB_OPT_MAX_ITER);
26 const Scene *scn = ctx->scn;
27 const GeomObject *obj = (const GeomObject*)hit.obj;
28 const Material *mtl = &obj->mtl;
29 const Reflectance *brdf = obj->brdf;
30 const Ray &ray = hit.world_ray;
31 //bool entering = true;
33 Vector3 norm = hit.calc_normal();
34 if(dot_product(ray.dir, norm) > 0.0) {
35 //entering = false;
36 norm = -norm;
37 }
39 //return norm * 0.5 + Vector3(0.5, 0.5, 0.5);
40 Vector2 texcoords = hit.calc_texcoords();
42 Color color = mtl->get_attrib_color("diffuse", texcoords.x, texcoords.y);
43 Color specular = mtl->get_attrib_color("specular", texcoords.x, texcoords.y);
44 Color res = mtl->get_attrib_color("emissive", texcoords.x, texcoords.y) +
45 color * scn->get_env().ambient;
46 float shininess = mtl->get_attrib_value("shininess");
48 Vector3 sample_dir;
49 float prob = brdf->sample(norm, -hit.world_ray.dir, &sample_dir);
50 if(iter < max_iter && randf() <= prob && ray.energy * prob > 0.001) {
51 Ray sample_ray;
52 sample_ray.origin = ray.origin + ray.dir * hit.dist;
53 sample_ray.dir = sample_dir;
54 sample_ray.energy = ray.energy * prob;
56 res += ray_trace(ctx, sample_ray, iter + 1) * color;
57 }
59 res.w = 1.0;
60 return res;
61 }