]> Dogcows Code - chaz/rasterize/blob - sphere.hh
basic ray tracing with hard shadows
[chaz/rasterize] / sphere.hh
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #ifndef _SPHERE_HH_
9 #define _SPHERE_HH_
10
11 #include "color.hh"
12 #include "element.hh"
13
14
15 namespace rt {
16
17
18 /*
19 * A class for a sphere object.
20 */
21 class sphere : public element
22 {
23 public:
24
25 color_t material;
26 vec_t origin;
27 scal_t radius;
28
29 sphere(vec_t o, scal_t r = S(1.0), color_t color = COLOR_WHITE) :
30 origin(o), radius(r), material(color)
31 {}
32
33 virtual ~sphere()
34 {}
35
36 virtual bool intersect(ray_t ray, contact_t& hit) const
37 {
38 vec_t b = vec_sub(origin, ray.o);
39 scal_t z = vec_dot(b, ray.d);
40
41 // check if the sphere is behind the ray
42 if (z < S(0.0)) {
43 return false;
44 }
45
46 scal_t d2 = vec_dot(b, b) - z * z;
47 scal_t r2 = radius * radius;
48
49 // check for an intersection
50 if (r2 < d2) {
51 return false;
52 }
53
54 hit.d = z - std::sqrt(r2 - d2);
55 if (hit.d < S(0.0)) {
56 return false;
57 }
58
59 hit.p = ray_solve(ray, hit.d);
60 hit.n = vec_sub(hit.p, origin);
61 return true;
62 }
63
64 virtual color_t color(vec_t point) const
65 {
66 return material;
67 }
68 };
69
70
71 /*
72 * Destroy a new'd sphere, releasing its memory.
73 */
74 INLINE_MAYBE
75 void sphere_destroy(sphere* s)
76 {
77 delete s;
78 }
79
80
81 } // namespace rt
82
83 #endif // _SPHERE_HH_
84
This page took 0.036238 seconds and 4 git commands to generate.