#ifndef _MOOF_SPHERE_HH_
#define _MOOF_SPHERE_HH_
+#include <Moof/Contact.hh>
#include <Moof/Cullable.hh>
#include <Moof/Drawable.hh>
#include <Moof/Frustum.hh>
* A round object.
*/
+
template <int D = 3>
struct Sphere : public Cullable, public Drawable, public Shape<D>
{
typedef cml::vector< Scalar, cml::fixed<D> > Vector;
- // (solution - point)^2 - radius^2 = 0
Vector point;
Scalar radius;
+
Sphere() {}
Sphere(const Vector& p, Scalar r) :
point(p),
radius(r) {}
- //Sphere(Scalar x, Scalar y, Scalar z, Scalar r) :
- //point(x, y, z),
- //radius(r) {}
void init(const Vector& p, Scalar r)
{
radius = (o - p).length();
}
+ //void encloseVertices(const Vector vertices[], unsigned count);
+
+ //void draw(Scalar alpha = 0.0) const;
+ //bool isVisible(const Frustum& frustum) const;
+
+ void encloseVertices(const Vector vertices[], unsigned count)
+ {
+ // TODO
+ }
+
+ void draw(Scalar alpha = 0.0) const;
+
+ bool isVisible(const Frustum& frustum) const
+ {
+ return true;
+ }
+
+
+ bool intersect(const Sphere<D>& sphere, Contact<D>& hit) const
+ {
+ Vector n = sphere.point - point;
+ Scalar distance = n.length();
+ Scalar limit = radius + sphere.radius;
+
+ if (distance > limit) return false;
+
+ hit.normal = n.normalize();
+ hit.distance = limit - distance;
+ hit.point = hit.normal * radius;
+
+ return true;
+ }
+
+ bool intersect(const Vector& point2, Contact<D>& hit) const
+ {
+ Vector n = point2 - point;
+ Scalar distance = n.length();
+
+ if (distance > radius) return false;
+
+ hit.normal = n.normalize();
+ hit.distance = radius - distance;
+ hit.point = point2;
+
+ return true;
+ }
// a ray inside the sphere will not intersect on its way out
- bool intersectRay(const Ray<D>& ray, typename Ray<D>::Intersection& hit)
+ bool intersect(const Ray<D>& ray, typename Ray<D>::Contact& hit) const
{
Vector b = point - ray.point;
Scalar z = cml::dot(b, ray.direction);
hit.normal = surfacePoint - point;
return true;
}
-
-
- //void encloseVertices(const Vector vertices[], unsigned count);
-
- //void draw(Scalar alpha = 0.0) const;
- //bool isVisible(const Frustum& frustum) const;
-
- void encloseVertices(const Vector vertices[], unsigned count)
- {
- // TODO
- }
-
- void draw(Scalar alpha = 0.0) const;
- //{
- //GLUquadricObj* sphereObj = gluNewQuadric();
- //gluQuadricDrawStyle(sphereObj, GLU_LINE);
-
- //glPushMatrix();
-
- //glTranslate(point);
- //gluSphere(sphereObj, GLdouble(radius), 16, 16);
-
- //glPopMatrix();
-
- //gluDeleteQuadric(sphereObj);
- //}
-
- bool isVisible(const Frustum& frustum) const
- {
- return true;
- }
};
}
+typedef Sphere<2> Sphere2;
+typedef Sphere2 Circle;
+typedef Sphere<3> Sphere3;
+
+
} // namespace Mf
#endif // _MOOF_SPHERE_HH_