]> Dogcows Code - chaz/yoink/blob - src/Moof/Sphere.hh
f7b6a95fe7a6d89a450291491ad8c1db012fada2
[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 Scalar intersectRay(const Ray<D>& ray,
81 typename Ray<D>::Intersection& intersection)
82 {
83 Vector b = point - ray.point;
84 Scalar z = cml::dot(b, ray.direction);
85
86 // check if the ball is behind the ray
87 if (z < SCALAR(0.0)) return SCALAR(-1.0);
88
89 Scalar d2 = cml::dot(b, b) - z*z;
90 Scalar r2 = radius * radius;
91
92 // check for an intersection
93 if (d2 > r2) return SCALAR(-1.0);
94
95 Scalar t = z - std::sqrt(r2 - d2);
96 ray.solve(intersection.point, t);
97 intersection.normal = intersection.point - point;
98
99 return t;
100 }
101
102
103 //void encloseVertices(const Vector vertices[], unsigned count);
104
105 //void draw(Scalar alpha = 0.0) const;
106 //bool isVisible(const Frustum& frustum) const;
107
108 void encloseVertices(const Vector vertices[], unsigned count)
109 {
110 // TODO
111 }
112
113 void draw(Scalar alpha = 0.0) const;
114 //{
115 //GLUquadricObj* sphereObj = gluNewQuadric();
116 //gluQuadricDrawStyle(sphereObj, GLU_LINE);
117
118 //glPushMatrix();
119
120 //glTranslate(point);
121 //gluSphere(sphereObj, GLdouble(radius), 16, 16);
122
123 //glPopMatrix();
124
125 //gluDeleteQuadric(sphereObj);
126 //}
127
128 bool isVisible(const Frustum& frustum) const
129 {
130 return true;
131 }
132 };
133
134
135 template <>
136 inline bool Sphere<3>::isVisible(const Frustum& frustum) const
137 {
138 return frustum.contains(*this);
139 }
140
141 template <>
142 inline void Sphere<2>::draw(Scalar alpha) const
143 {
144 GLUquadricObj* sphereObj = gluNewQuadric();
145 gluQuadricDrawStyle(sphereObj, GLU_LINE);
146
147 glPushMatrix();
148
149 glTranslate(promote(point));
150 gluSphere(sphereObj, GLdouble(radius), 16, 16);
151
152 glPopMatrix();
153
154 gluDeleteQuadric(sphereObj);
155 }
156
157 template <>
158 inline void Sphere<3>::draw(Scalar alpha) const
159 {
160 GLUquadricObj* sphereObj = gluNewQuadric();
161 gluQuadricDrawStyle(sphereObj, GLU_LINE);
162
163 glPushMatrix();
164
165 glTranslate(point);
166 gluSphere(sphereObj, GLdouble(radius), 16, 16);
167
168 glPopMatrix();
169
170 gluDeleteQuadric(sphereObj);
171 }
172
173 template <int D>
174 inline bool checkCollision(const Sphere<D>& a, const Sphere<D>& b)
175 {
176 Scalar d = (a.point - b.point).length();
177 return d < (a.radius + b.radius);
178 }
179
180
181 } // namespace Mf
182
183 #endif // _MOOF_SPHERE_HH_
184
185 /** vim: set ts=4 sw=4 tw=80: *************************************************/
186
This page took 0.037204 seconds and 3 git commands to generate.