]>
Dogcows Code - chaz/yoink/blob - src/Moof/Sphere.hh
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
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.
10 **************************************************************************/
12 #ifndef _MOOF_SPHERE_HH_
13 #define _MOOF_SPHERE_HH_
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>
22 // TODO this needs work
32 struct Sphere
: public Cullable
, public Drawable
, public Shape
<D
>
34 typedef cml::vector
< Scalar
, cml::fixed
<D
> > Vector
;
36 // (solution - point)^2 - radius^2 = 0
42 Sphere(const Vector
& p
, Scalar r
) :
46 //Sphere(Scalar x, Scalar y, Scalar z, Scalar r) :
50 void init(const Vector
& p
, Scalar r
)
56 void init(const Vector
& p
, const Vector
& o
)
59 radius
= (o
- p
).length();
63 // a ray inside the sphere will not intersect on its way out
64 bool intersectRay(const Ray
<D
>& ray
, typename Ray
<D
>::Contact
& hit
)
66 Vector b
= point
- ray
.point
;
67 Scalar z
= cml::dot(b
, ray
.direction
);
69 // check if the ball is behind the ray
70 if (z
< SCALAR(0.0)) return false;
72 Scalar d2
= cml::dot(b
, b
) - z
*z
;
73 Scalar r2
= radius
* radius
;
75 // check for an intersection
76 if (d2
> r2
) return false;
78 hit
.distance
= z
- std::sqrt(r2
- d2
);
79 if (hit
.distance
< SCALAR(0.0)) return false;
82 ray
.solve(surfacePoint
, hit
.distance
);
83 hit
.normal
= surfacePoint
- point
;
88 //void encloseVertices(const Vector vertices[], unsigned count);
90 //void draw(Scalar alpha = 0.0) const;
91 //bool isVisible(const Frustum& frustum) const;
93 void encloseVertices(const Vector vertices
[], unsigned count
)
98 void draw(Scalar alpha
= 0.0) const;
100 //GLUquadricObj* sphereObj = gluNewQuadric();
101 //gluQuadricDrawStyle(sphereObj, GLU_LINE);
105 //glTranslate(point);
106 //gluSphere(sphereObj, GLdouble(radius), 16, 16);
110 //gluDeleteQuadric(sphereObj);
113 bool isVisible(const Frustum
& frustum
) const
121 inline bool Sphere
<3>::isVisible(const Frustum
& frustum
) const
123 return frustum
.contains(*this);
127 inline void Sphere
<2>::draw(Scalar alpha
) const
129 GLUquadricObj
* sphereObj
= gluNewQuadric();
130 gluQuadricDrawStyle(sphereObj
, GLU_LINE
);
134 glTranslate(promote(point
));
135 gluSphere(sphereObj
, GLdouble(radius
), 16, 16);
139 gluDeleteQuadric(sphereObj
);
143 inline void Sphere
<3>::draw(Scalar alpha
) const
145 GLUquadricObj
* sphereObj
= gluNewQuadric();
146 gluQuadricDrawStyle(sphereObj
, GLU_LINE
);
151 gluSphere(sphereObj
, GLdouble(radius
), 16, 16);
155 gluDeleteQuadric(sphereObj
);
159 inline bool checkCollision(const Sphere
<D
>& a
, const Sphere
<D
>& b
)
161 Scalar d
= (a
.point
- b
.point
).length();
162 return d
< (a
.radius
+ b
.radius
);
166 typedef Sphere
<2> Sphere2
;
167 typedef Sphere2 Circle
;
168 typedef Sphere
<3> Sphere3
;
173 #endif // _MOOF_SPHERE_HH_
This page took 0.039134 seconds and 4 git commands to generate.