]> Dogcows Code - chaz/yoink/blob - src/Moof/Sphere.hh
f1847091c6d0afcc9bad1cbbabcddfdb7cb78dfe
[chaz/yoink] / src / Moof / Sphere.hh
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #ifndef _MOOF_SPHERE_HH_
30 #define _MOOF_SPHERE_HH_
31
32 #include <Moof/Cullable.hh>
33 #include <Moof/Drawable.hh>
34 #include <Moof/Frustum.hh>
35 #include <Moof/Math.hh>
36 #include <Moof/OpenGL.hh>
37 #include <Moof/Shape.hh>
38
39
40 namespace Mf {
41
42
43 /**
44 * A round object.
45 */
46
47 template <int D = 3>
48 struct Sphere : public Cullable, public Drawable, public Shape<D>
49 {
50 typedef cml::vector< Scalar, cml::fixed<D> > Vector;
51
52 // (solution - point)^2 - radius^2 = 0
53 Vector point;
54 Scalar radius;
55
56 Sphere() {}
57
58 Sphere(const Vector& p, Scalar r) :
59 point(p),
60 radius(r) {}
61
62 //Sphere(Scalar x, Scalar y, Scalar z, Scalar r) :
63 //point(x, y, z),
64 //radius(r) {}
65
66 void init(const Vector& p, Scalar r)
67 {
68 point = p;
69 radius = r;
70 }
71
72 void init(const Vector& p, const Vector& o)
73 {
74 point = p;
75 radius = (o - p).length();
76 }
77
78
79 // a ray inside the sphere will not intersect on its way out
80 bool intersectRay(const Ray<D>& ray, typename Ray<D>::Intersection& hit)
81 {
82 Vector b = point - ray.point;
83 Scalar z = cml::dot(b, ray.direction);
84
85 // check if the ball is behind the ray
86 if (z < SCALAR(0.0)) return false;
87
88 Scalar d2 = cml::dot(b, b) - z*z;
89 Scalar r2 = radius * radius;
90
91 // check for an intersection
92 if (d2 > r2) return false;
93
94 hit.distance = z - std::sqrt(r2 - d2);
95 if (hit.distance < SCALAR(0.0)) return false;
96
97 Vector surfacePoint;
98 ray.solve(surfacePoint, hit.distance);
99 hit.normal = surfacePoint - point;
100 return true;
101 }
102
103
104 //void encloseVertices(const Vector vertices[], unsigned count);
105
106 //void draw(Scalar alpha = 0.0) const;
107 //bool isVisible(const Frustum& frustum) const;
108
109 void encloseVertices(const Vector vertices[], unsigned count)
110 {
111 // TODO
112 }
113
114 void draw(Scalar alpha = 0.0) const;
115 //{
116 //GLUquadricObj* sphereObj = gluNewQuadric();
117 //gluQuadricDrawStyle(sphereObj, GLU_LINE);
118
119 //glPushMatrix();
120
121 //glTranslate(point);
122 //gluSphere(sphereObj, GLdouble(radius), 16, 16);
123
124 //glPopMatrix();
125
126 //gluDeleteQuadric(sphereObj);
127 //}
128
129 bool isVisible(const Frustum& frustum) const
130 {
131 return true;
132 }
133 };
134
135
136 template <>
137 inline bool Sphere<3>::isVisible(const Frustum& frustum) const
138 {
139 return frustum.contains(*this);
140 }
141
142 template <>
143 inline void Sphere<2>::draw(Scalar alpha) const
144 {
145 GLUquadricObj* sphereObj = gluNewQuadric();
146 gluQuadricDrawStyle(sphereObj, GLU_LINE);
147
148 glPushMatrix();
149
150 glTranslate(promote(point));
151 gluSphere(sphereObj, GLdouble(radius), 16, 16);
152
153 glPopMatrix();
154
155 gluDeleteQuadric(sphereObj);
156 }
157
158 template <>
159 inline void Sphere<3>::draw(Scalar alpha) const
160 {
161 GLUquadricObj* sphereObj = gluNewQuadric();
162 gluQuadricDrawStyle(sphereObj, GLU_LINE);
163
164 glPushMatrix();
165
166 glTranslate(point);
167 gluSphere(sphereObj, GLdouble(radius), 16, 16);
168
169 glPopMatrix();
170
171 gluDeleteQuadric(sphereObj);
172 }
173
174 template <int D>
175 inline bool checkCollision(const Sphere<D>& a, const Sphere<D>& b)
176 {
177 Scalar d = (a.point - b.point).length();
178 return d < (a.radius + b.radius);
179 }
180
181
182 } // namespace Mf
183
184 #endif // _MOOF_SPHERE_HH_
185
186 /** vim: set ts=4 sw=4 tw=80: *************************************************/
187
This page took 0.037011 seconds and 3 git commands to generate.