erebus

view liberebus/src/rt.cc @ 42:b9294cd6b9dc

console input history with up/down
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 10 Jun 2014 16:15:08 +0300
parents 53a98c148bf8
children ed18af9da8f7
line source
1 #include <assert.h>
2 #include "rt.h"
3 #include "erebus_impl.h"
5 Color ray_trace(struct erebus *ctx, const Ray &ray, int iter)
6 {
7 const Scene *scn = ctx->scn;
8 if(!scn) {
9 return Color(1, 0, 0);
10 }
12 RayHit hit;
13 if(!(scn->intersect(ray, &hit))) {
14 return scn->get_env_color(ray);
15 }
17 return shade(ctx, hit, iter);
18 }
20 Color shade(struct erebus *ctx, const RayHit &hit, int iter)
21 {
22 assert(hit.obj->get_type() == ObjType::geom);
23 int max_iter = erb_getopti(ctx, ERB_OPT_MAX_ITER);
24 const Scene *scn = ctx->scn;
25 const GeomObject *obj = (const GeomObject*)hit.obj;
26 const Material *mtl = &obj->mtl;
27 const Reflectance *brdf = obj->brdf;
28 const Ray &ray = hit.world_ray;
29 //bool entering = true;
31 Vector3 norm = hit.calc_normal();
32 if(dot_product(ray.dir, norm) > 0.0) {
33 //entering = false;
34 norm = -norm;
35 }
37 //return norm * 0.5 + Vector3(0.5, 0.5, 0.5);
38 Vector2 texcoords = hit.calc_texcoords();
40 Color color = mtl->get_attrib_color("diffuse", texcoords.x, texcoords.y);
41 Color specular = mtl->get_attrib_color("specular", texcoords.x, texcoords.y);
42 Color res = mtl->get_attrib_color("emissive", texcoords.x, texcoords.y) +
43 color * scn->get_env().ambient;
44 float shininess = mtl->get_attrib_value("shininess");
46 Vector3 sample_dir;
47 float prob = brdf->sample(SurfaceGeometry(norm), -hit.world_ray.dir, &sample_dir);
48 if(iter < max_iter && randf() <= prob && ray.energy * prob > 0.001) {
49 Ray sample_ray;
50 sample_ray.origin = ray.origin + ray.dir * hit.dist;
51 sample_ray.dir = sample_dir;
52 sample_ray.energy = ray.energy * prob;
54 res += ray_trace(ctx, sample_ray, iter + 1) * color;
55 }
57 res.w = 1.0;
58 return res;
59 }