X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FSphere.hh;h=f5285545b17a3faa66ba3f4fdd0a250b1bf50a99;hp=d42bfd8a98fa8e643494c7b242f8e659c1893da9;hb=00612586426be6d9a976f141a25ffc1f0d284501;hpb=76b3f4be992514a740ac03cdbdd57844142a0b4c diff --git a/src/Moof/Sphere.hh b/src/Moof/Sphere.hh index d42bfd8..f528554 100644 --- a/src/Moof/Sphere.hh +++ b/src/Moof/Sphere.hh @@ -12,6 +12,7 @@ #ifndef _MOOF_SPHERE_HH_ #define _MOOF_SPHERE_HH_ +#include #include #include #include @@ -28,24 +29,22 @@ namespace Mf { * A round object. */ + template struct Sphere : public Cullable, public Drawable, public Shape { typedef cml::vector< Scalar, cml::fixed > 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) { @@ -59,9 +58,55 @@ struct Sphere : public Cullable, public Drawable, public Shape 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& sphere, Contact& 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& 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& ray, typename Ray::Contact& hit) + bool intersect(const Ray& ray, typename Ray::Contact& hit) const { Vector b = point - ray.point; Scalar z = cml::dot(b, ray.direction); @@ -83,37 +128,6 @@ struct Sphere : public Cullable, public Drawable, public Shape 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; - } };