]> Dogcows Code - chaz/yoink/blob - src/Moof/Sphere.hh
reformatting
[chaz/yoink] / src / Moof / Sphere.hh
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #ifndef _MOOF_SPHERE_HH_
13 #define _MOOF_SPHERE_HH_
14
15 #include <Moof/Cullable.hh>
16 #include <Moof/Drawable.hh>
17 #include <Moof/Frustum.hh>
18 #include <Moof/Math.hh>
19 #include <Moof/OpenGL.hh>
20 #include <Moof/Shape.hh>
21
22 // TODO this needs work
23
24 namespace Mf {
25
26
27 /**
28 * A round object.
29 */
30
31 template <int D = 3>
32 struct Sphere : public Cullable, public Drawable, public Shape<D>
33 {
34 typedef cml::vector< Scalar, cml::fixed<D> > Vector;
35
36 // (solution - point)^2 - radius^2 = 0
37 Vector point;
38 Scalar radius;
39
40 Sphere() {}
41
42 Sphere(const Vector& p, Scalar r) :
43 point(p),
44 radius(r) {}
45
46 //Sphere(Scalar x, Scalar y, Scalar z, Scalar r) :
47 //point(x, y, z),
48 //radius(r) {}
49
50 void init(const Vector& p, Scalar r)
51 {
52 point = p;
53 radius = r;
54 }
55
56 void init(const Vector& p, const Vector& o)
57 {
58 point = p;
59 radius = (o - p).length();
60 }
61
62
63 // a ray inside the sphere will not intersect on its way out
64 bool intersectRay(const Ray<D>& ray, typename Ray<D>::Intersection& hit)
65 {
66 Vector b = point - ray.point;
67 Scalar z = cml::dot(b, ray.direction);
68
69 // check if the ball is behind the ray
70 if (z < SCALAR(0.0)) return false;
71
72 Scalar d2 = cml::dot(b, b) - z*z;
73 Scalar r2 = radius * radius;
74
75 // check for an intersection
76 if (d2 > r2) return false;
77
78 hit.distance = z - std::sqrt(r2 - d2);
79 if (hit.distance < SCALAR(0.0)) return false;
80
81 Vector surfacePoint;
82 ray.solve(surfacePoint, hit.distance);
83 hit.normal = surfacePoint - point;
84 return true;
85 }
86
87
88 //void encloseVertices(const Vector vertices[], unsigned count);
89
90 //void draw(Scalar alpha = 0.0) const;
91 //bool isVisible(const Frustum& frustum) const;
92
93 void encloseVertices(const Vector vertices[], unsigned count)
94 {
95 // TODO
96 }
97
98 void draw(Scalar alpha = 0.0) const;
99 //{
100 //GLUquadricObj* sphereObj = gluNewQuadric();
101 //gluQuadricDrawStyle(sphereObj, GLU_LINE);
102
103 //glPushMatrix();
104
105 //glTranslate(point);
106 //gluSphere(sphereObj, GLdouble(radius), 16, 16);
107
108 //glPopMatrix();
109
110 //gluDeleteQuadric(sphereObj);
111 //}
112
113 bool isVisible(const Frustum& frustum) const
114 {
115 return true;
116 }
117 };
118
119
120 template <>
121 inline bool Sphere<3>::isVisible(const Frustum& frustum) const
122 {
123 return frustum.contains(*this);
124 }
125
126 template <>
127 inline void Sphere<2>::draw(Scalar alpha) const
128 {
129 GLUquadricObj* sphereObj = gluNewQuadric();
130 gluQuadricDrawStyle(sphereObj, GLU_LINE);
131
132 glPushMatrix();
133
134 glTranslate(promote(point));
135 gluSphere(sphereObj, GLdouble(radius), 16, 16);
136
137 glPopMatrix();
138
139 gluDeleteQuadric(sphereObj);
140 }
141
142 template <>
143 inline void Sphere<3>::draw(Scalar alpha) const
144 {
145 GLUquadricObj* sphereObj = gluNewQuadric();
146 gluQuadricDrawStyle(sphereObj, GLU_LINE);
147
148 glPushMatrix();
149
150 glTranslate(point);
151 gluSphere(sphereObj, GLdouble(radius), 16, 16);
152
153 glPopMatrix();
154
155 gluDeleteQuadric(sphereObj);
156 }
157
158 template <int D>
159 inline bool checkCollision(const Sphere<D>& a, const Sphere<D>& b)
160 {
161 Scalar d = (a.point - b.point).length();
162 return d < (a.radius + b.radius);
163 }
164
165
166 } // namespace Mf
167
168 #endif // _MOOF_SPHERE_HH_
169
This page took 0.048053 seconds and 4 git commands to generate.