00001
00002
00003
00004
00005
00006 #ifndef SPHERE_H
00007 #define SPHERE_H
00008
00009 #include "renderable.h"
00010 #include "scene.h"
00011 #include "xml_util.h"
00012 #include <string>
00013
00014 #include "solidmaterial.h"
00015
00016 class Sphere;
00017
00018 Renderable * new_sphere(xmlNode * node);
00019 void delete_sphere(Sphere * sphere);
00020
00022 class Sphere : public Renderable {
00023
00024 protected:
00026 class StaticInit {
00027 public:
00029 StaticInit() {
00030 log_debug("Registering 'sphere'");
00031 RenderableFactory::get_instance()->registerPlugin("sphere", sigc::ptr_fun(new_sphere));
00032 log_debug("Sphere registered.");
00033 }
00034 };
00036 static StaticInit m_init;
00037
00039 double m_radius;
00040
00041 public:
00042
00044 Sphere(
00045 Point3D center,
00046 double r)
00047 : m_radius(r)
00048 {
00049 log_debug("Creating a Sphere at (%0.2f, %0.2f, %0.2f) with a radius of %0.2f", center.x, center.y, center.z, r );
00050
00051 m_center = center;
00052
00053 }
00054
00056 virtual ~Sphere();
00057
00112 virtual bool collides_with(const Ray &ray, double &t) const;
00113
00117 virtual Ray get_normal(const Point3D &p) const;
00118 };
00119
00120 Renderable * new_sphere(xmlNode * node) {
00121 log_debug("Entering new_sphere()");
00122 Sphere * rv = NULL;
00123
00124 xml_properties props = get_properties(node);
00125
00126 if ( props.empty() == false ) {
00127 double radius = (double)strtod(props["radius" ].c_str(), NULL);
00128
00129 if(props.find("center") != props.end()) {
00130 rv = new Sphere(Point3D(props["center"]), radius);
00131 }
00132 else {
00133 double x = (double)strtod(props["x"].c_str(), NULL);
00134 double y = (double)strtod(props["y"].c_str(), NULL);
00135 double z = (double)strtod(props["z"].c_str(), NULL);
00136 rv = new Sphere(Point3D(x, y, z), radius);
00137 }
00138 }
00139 else {
00140 log_err("No properties specified for <sphere> tag.");
00141 }
00142
00143 log_debug("Leaving new_sphere()");
00144 return dynamic_cast<Renderable *>(rv);
00145 }
00146
00147 void delete_sphere(Sphere * sphere) {
00148 delete sphere, sphere = NULL;
00149 }
00150
00151 #endif
00152