]>
Dogcows Code - chaz/yoink/blob - src/Moof/Sphere.hh
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
29 #ifndef _MOOF_SPHERE_HH_
30 #define _MOOF_SPHERE_HH_
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>
48 struct Sphere
: public Cullable
, public Drawable
, public Shape
<D
>
50 typedef cml::vector
< Scalar
, cml::fixed
<D
> > Vector
;
52 // (solution - point)^2 - radius^2 = 0
58 Sphere(const Vector
& p
, Scalar r
) :
62 //Sphere(Scalar x, Scalar y, Scalar z, Scalar r) :
66 void init(const Vector
& p
, Scalar r
)
72 void init(const Vector
& p
, const Vector
& o
)
75 radius
= (o
- p
).length();
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
)
82 Vector b
= point
- ray
.point
;
83 Scalar z
= cml::dot(b
, ray
.direction
);
85 // check if the ball is behind the ray
86 if (z
< SCALAR(0.0)) return false;
88 Scalar d2
= cml::dot(b
, b
) - z
*z
;
89 Scalar r2
= radius
* radius
;
91 // check for an intersection
92 if (d2
> r2
) return false;
94 hit
.distance
= z
- std::sqrt(r2
- d2
);
95 if (hit
.distance
< SCALAR(0.0)) return false;
98 ray
.solve(surfacePoint
, hit
.distance
);
99 hit
.normal
= surfacePoint
- point
;
104 //void encloseVertices(const Vector vertices[], unsigned count);
106 //void draw(Scalar alpha = 0.0) const;
107 //bool isVisible(const Frustum& frustum) const;
109 void encloseVertices(const Vector vertices
[], unsigned count
)
114 void draw(Scalar alpha
= 0.0) const;
116 //GLUquadricObj* sphereObj = gluNewQuadric();
117 //gluQuadricDrawStyle(sphereObj, GLU_LINE);
121 //glTranslate(point);
122 //gluSphere(sphereObj, GLdouble(radius), 16, 16);
126 //gluDeleteQuadric(sphereObj);
129 bool isVisible(const Frustum
& frustum
) const
137 inline bool Sphere
<3>::isVisible(const Frustum
& frustum
) const
139 return frustum
.contains(*this);
143 inline void Sphere
<2>::draw(Scalar alpha
) const
145 GLUquadricObj
* sphereObj
= gluNewQuadric();
146 gluQuadricDrawStyle(sphereObj
, GLU_LINE
);
150 glTranslate(promote(point
));
151 gluSphere(sphereObj
, GLdouble(radius
), 16, 16);
155 gluDeleteQuadric(sphereObj
);
159 inline void Sphere
<3>::draw(Scalar alpha
) const
161 GLUquadricObj
* sphereObj
= gluNewQuadric();
162 gluQuadricDrawStyle(sphereObj
, GLU_LINE
);
167 gluSphere(sphereObj
, GLdouble(radius
), 16, 16);
171 gluDeleteQuadric(sphereObj
);
175 inline bool checkCollision(const Sphere
<D
>& a
, const Sphere
<D
>& b
)
177 Scalar d
= (a
.point
- b
.point
).length();
178 return d
< (a
.radius
+ b
.radius
);
184 #endif // _MOOF_SPHERE_HH_
186 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.045614 seconds and 5 git commands to generate.