]> Dogcows Code - chaz/rasterize/blob - plane.hh
basic ray tracing with hard shadows
[chaz/rasterize] / plane.hh
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #ifndef _PLANE_HH_
9 #define _PLANE_HH_
10
11 #include "color.hh"
12 #include "element.hh"
13
14
15 namespace rt {
16
17
18 /*
19 * A class for a plane object.
20 */
21 class plane : public element
22 {
23 public:
24
25 color_t material;
26 vec_t normal;
27 scal_t distance;
28
29 plane(vec_t n, scal_t d, color_t color = COLOR_WHITE) :
30 normal(n), distance(d), material(color)
31 {}
32
33 plane(vec_t p, vec_t n, color_t color = COLOR_WHITE) :
34 normal(vec_normalize(n)), distance(-vec_dot(normal, p)), material(color)
35 {}
36
37 plane(vec_t a, vec_t b, vec_t c, color_t color = COLOR_WHITE) :
38 normal(vec_normalize(vec_cross(vec_sub(b, a), vec_sub(c, a)))),
39 distance(-vec_dot(normal, a)), material(color)
40 {}
41
42 virtual ~plane()
43 {}
44
45 virtual bool intersect(ray_t ray, contact_t& hit) const
46 {
47 // solve: [(ray.point + t*ray.direction) dot normal] + d = 0
48
49 scal_t denom = vec_dot(ray.d, normal);
50
51 // check for parallel condition
52 if (denom == S(0.0)) {
53 if (scal_isequal(vec_dot(ray.o, normal), -distance)) {
54 hit.p = ray.o;
55 hit.d = S(0.0);
56 hit.n = VEC_ZERO;
57 return true; // the ray lies on the plane
58 }
59 return false; // no solution
60 }
61
62 scal_t numer = vec_dot(ray.o, normal) + distance;
63 hit.d = -numer / denom;
64 if (hit.d < S(0.0)) {
65 return false;
66 }
67
68 hit.p = ray_solve(ray, hit.d);
69 if (S(0.0) <= numer) {
70 hit.n = normal;
71 }
72 else {
73 hit.n = vec_neg(normal);
74 }
75 return true;
76 }
77
78 virtual color_t color(vec_t point) const
79 {
80 return material;
81 }
82 };
83
84
85 /*
86 * Destroy a new'd plane, releasing its memory.
87 */
88 INLINE_MAYBE
89 void plane_destroy(plane* p)
90 {
91 delete p;
92 }
93
94
95 } // namespace rt
96
97 #endif // _PLANE_HH_
98
This page took 0.041957 seconds and 4 git commands to generate.