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@17
|
5 Color ray_trace(struct erebus *ctx, const Ray &ray, int iter)
|
nuclear@6
|
6 {
|
nuclear@17
|
7 const Scene *scn = ctx->scn;
|
nuclear@17
|
8 if(!scn) {
|
nuclear@17
|
9 return Color(1, 0, 0);
|
nuclear@17
|
10 }
|
nuclear@17
|
11
|
nuclear@6
|
12 RayHit hit;
|
nuclear@6
|
13 if(!(scn->intersect(ray, &hit))) {
|
nuclear@8
|
14 return scn->get_env_color(ray);
|
nuclear@6
|
15 }
|
nuclear@6
|
16
|
nuclear@17
|
17 return shade(ctx, hit, iter);
|
nuclear@6
|
18 }
|
nuclear@6
|
19
|
nuclear@17
|
20 Color shade(struct erebus *ctx, const RayHit &hit, int iter)
|
nuclear@6
|
21 {
|
nuclear@8
|
22 assert(hit.obj->get_type() == ObjType::geom);
|
nuclear@17
|
23 int max_iter = erb_getopti(ctx, ERB_OPT_MAX_ITER);
|
nuclear@17
|
24 const Scene *scn = ctx->scn;
|
nuclear@8
|
25 const GeomObject *obj = (const GeomObject*)hit.obj;
|
nuclear@8
|
26 const Material *mtl = &obj->mtl;
|
nuclear@8
|
27 const Reflectance *brdf = obj->brdf;
|
nuclear@8
|
28 const Ray &ray = hit.world_ray;
|
nuclear@26
|
29 //bool entering = true;
|
nuclear@8
|
30
|
nuclear@17
|
31 Vector3 norm = hit.calc_normal();
|
nuclear@23
|
32 if(dot_product(ray.dir, norm) > 0.0) {
|
nuclear@26
|
33 //entering = false;
|
nuclear@23
|
34 norm = -norm;
|
nuclear@23
|
35 }
|
nuclear@23
|
36
|
nuclear@18
|
37 //return norm * 0.5 + Vector3(0.5, 0.5, 0.5);
|
nuclear@17
|
38 Vector2 texcoords = hit.calc_texcoords();
|
nuclear@8
|
39
|
nuclear@17
|
40 Color color = mtl->get_attrib_color("diffuse", texcoords.x, texcoords.y);
|
nuclear@28
|
41 Color specular = mtl->get_attrib_color("specular", texcoords.x, texcoords.y);
|
nuclear@28
|
42 Color res = mtl->get_attrib_color("emissive", texcoords.x, texcoords.y) +
|
nuclear@28
|
43 color * scn->get_env().ambient;
|
nuclear@28
|
44 float shininess = mtl->get_attrib_value("shininess");
|
nuclear@8
|
45
|
nuclear@8
|
46 Vector3 sample_dir;
|
nuclear@31
|
47 float prob = brdf->sample(SurfaceGeometry(norm), -hit.world_ray.dir, &sample_dir);
|
nuclear@28
|
48 if(iter < max_iter && randf() <= prob && ray.energy * prob > 0.001) {
|
nuclear@8
|
49 Ray sample_ray;
|
nuclear@8
|
50 sample_ray.origin = ray.origin + ray.dir * hit.dist;
|
nuclear@8
|
51 sample_ray.dir = sample_dir;
|
nuclear@28
|
52 sample_ray.energy = ray.energy * prob;
|
nuclear@8
|
53
|
nuclear@17
|
54 res += ray_trace(ctx, sample_ray, iter + 1) * color;
|
nuclear@8
|
55 }
|
nuclear@29
|
56
|
nuclear@29
|
57 res.w = 1.0;
|
nuclear@8
|
58 return res;
|
nuclear@6
|
59 }
|