]> Dogcows Code - chaz/rasterize/blob - element.hh
basic ray tracing with hard shadows
[chaz/rasterize] / element.hh
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #ifndef _ELEMENT_HH_
9 #define _ELEMENT_HH_
10
11 #include "color.hh"
12 #include "contact.hh"
13 #include "light.hh"
14 #include "ray.hh"
15 #include "scene.hh"
16
17
18 namespace rt {
19
20
21 /*
22 * A base class for an object in a scene.
23 */
24 class element
25 {
26 public:
27
28 virtual ~element()
29 {}
30
31 /*
32 * Determine if a ray intersects this element.
33 */
34 virtual bool intersect(ray_t ray, contact_t& hit) const = 0;
35
36 /*
37 * Get the material color of the element at a surface point.
38 */
39 virtual color_t color(vec_t point) const
40 {
41 return COLOR_WHITE;
42 }
43
44 /*
45 * Cast a ray to this element, returning the correct color value.
46 */
47 color_t cast(const contact_t& hit, const scene_t* scene) const
48 {
49 color_t Ia = color_mult(scene_ambient(scene), color(hit.p));
50 color_t Id = diffuse(hit, scene);
51 return color_clamp(color_add(Ia, Id));
52 }
53
54 /*
55 * Get whether or not a particular surface point is shadowed by other
56 * objects for a given light source.
57 */
58 bool is_shadowed(const contact_t& hit, const scene_t* scene, const light_t* light) const
59 {
60 #if SHADOWS
61 ray_t ray = ray_normalize(ray_new(hit.p, vec_sub(light->v, hit.p)));
62 for (list_t* i = scene_elements(scene); i; i = i->link) {
63 rt::element* obj = (rt::element*)i->val;
64 contact_t hit;
65 if (this != obj && obj->intersect(ray, hit)) {
66 return true;
67 }
68 }
69 #endif
70
71 return false;
72 }
73
74 /*
75 * Get the color of the element at a surface point after diffuse
76 * lighting.
77 */
78 color_t diffuse(const contact_t& hit, const scene_t* scene) const
79 {
80 color_t kd = color(hit.p);
81 vec_t n = vec_normalize(hit.n);
82
83 color_t c = COLOR_BLACK;
84
85 for (list_t* i = scene_lights(scene); i; i = i->link) {
86 light_t* l = (light_t*)i->val;
87 if (!is_shadowed(hit, scene, l)) {
88 vec_t I = vec_normalize(vec_sub(l->v, hit.p));
89 scal_t dot = scal_max(vec_dot(I, n), S(0.0));
90 color_t Id = color_scale2(kd, l->d, dot);
91 c = color_add(c, Id);
92 }
93 }
94
95 return c;
96 }
97 };
98
99
100 } // namespace rt
101
102 #endif // _ELEMENT_HH_
103
This page took 0.036307 seconds and 4 git commands to generate.