]> Dogcows Code - chaz/rasterize/blob - sphere.hh
finishing fifth project
[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 "element.hh"
12
13
14 namespace rt {
15
16
17 /*
18 * A class for a sphere object.
19 */
20 class sphere : public element
21 {
22 vec_t origin;
23 scal_t radius;
24
25 public:
26
27 sphere(vec_t o, scal_t r = S(1.0)) :
28 origin(o), radius(r)
29 {}
30
31 virtual ~sphere()
32 {}
33
34 virtual bool intersect(ray_t ray, contact_t& hit) const
35 {
36 vec_t b = vec_sub(origin, ray.o);
37 scal_t z = vec_dot(b, ray.d);
38
39 // check if the sphere is behind the ray
40 if (z < S(0.0)) {
41 return false;
42 }
43
44 scal_t d2 = vec_dot(b, b) - z * z;
45 scal_t r2 = radius * radius;
46
47 // check for an intersection
48 if (r2 < d2) {
49 return false;
50 }
51
52 hit.d = z - std::sqrt(r2 - d2);
53 if (hit.d < S(0.0)) {
54 return false;
55 }
56
57 hit.p = ray_solve(ray, hit.d);
58 hit.n = vec_sub(hit.p, origin);
59 return true;
60 }
61
62 virtual vec_t txcoord(vec_t point) const
63 {
64 vec_t uv = VEC_ZERO;
65
66 scal_t theta = scal_atan2(point.z, point.x);
67 scal_t phi = asin(point.y / radius);
68
69 uv.x = theta / (S(2.0) * M_PI) + S(0.5);
70 uv.y = phi / M_PI + S(0.5);
71
72 return uv;
73 }
74 };
75
76
77 /*
78 * Destroy a new'd sphere, releasing its memory.
79 */
80 INLINE_MAYBE
81 void sphere_destroy(sphere* s)
82 {
83 delete s;
84 }
85
86
87 } // namespace rt
88
89 #endif // _SPHERE_HH_
90
This page took 0.037497 seconds and 4 git commands to generate.