polygon.h

Go to the documentation of this file.
00001 /* file name  : polygon.h
00002  * authors    : Michael Brailsford
00003  * created    : Sun Oct 14 16:41:16 -0500 2007
00004  * copyright  : (c) 2007 Michael Brailsford
00005  */
00006 
00007 #ifndef POLYGON_H
00008 #define POLYGON_H
00009 
00010 #include "raytrace_defs.h"
00011 #include "xml_util.h"
00012 #include "pluginfactory.h"
00013 #include <vector>
00014 #include "point3d.h"
00015 
00016 class Renderable;
00017 Renderable * new_polygon(xmlNode * node);
00018 
00019 class Polygon : public Renderable {
00020     protected:
00022         class StaticInit {
00023             public:
00025                 StaticInit() {
00026                     RenderableFactory::get_instance()->registerPlugin("polygon",  sigc::ptr_fun(new_polygon));
00027                     RenderableFactory::get_instance()->registerPlugin("triangle", sigc::ptr_fun(new_polygon));
00028                     RenderableFactory::get_instance()->registerPlugin("square",   sigc::ptr_fun(new_polygon));
00029                 }
00030         };
00032         static StaticInit m_init;
00033 
00036         Ray m_normal;
00037         float m_d;
00038 
00041         std::vector<Point3D> m_vertices;
00042 
00043     public:
00045         Polygon(std::string color, std::string material, std::string bumpmap);
00046 
00048         //Polygon(Point3D p0, Ray normal);
00049 
00050         virtual ~Polygon() {}
00051 
00057         virtual bool collides_with(const Ray &ray, double &t) const;
00058 
00060         virtual Ray get_normal(const Point3D &p) const;
00061 
00069         bool add_vertex(Point3D vertex);
00070 };
00071 
00072 //{{{
00073 Renderable * new_polygon(xmlNode * node) {
00074     log_debug("Entering new_plane()");
00075     Polygon * rv = NULL;
00076 
00077     xml_properties props = get_properties(node);
00078 
00079     if ( props.empty() == false ) {
00080         rv = new Polygon( props["color"], 
00081                           props["material"],
00082                           props["bumpmap"]);
00083 
00084         // We need at least 3 vertices to define a polygon.
00085         int nvertices = 0;
00086 
00087         // Parse each vertex.
00088         xmlNode * child = node->children;
00089         while(child != node->last) {
00090             log_debug("Found %s.\n", child->name);
00091 
00092             if(strcmp((const char *)child->name, "vertex") == 0) {
00093                 if(rv->add_vertex(parse_vertex(child))) {
00094                     nvertices++;
00095                 }
00096             }
00097 
00098             child = child->next;
00099         }
00100 
00101         if(nvertices < 3) {
00102             log_err("Fewer than 3 vertices specified.  You must specify at least 3 vertices to define a polygon.");
00103             delete rv, rv = NULL;
00104         }
00105     }
00106     else {
00107         log_err("No properties specified for <polygon> tag.");
00108     }
00109 
00110     log_err("Leaving new_polygon()");
00111     return dynamic_cast<Renderable *>(rv);
00112 }
00113 //}}}
00114 //{{{
00115 void delete_plane(Polygon * polygon) {
00116     delete polygon, polygon = NULL;
00117 }
00118 //}}}
00119 #endif
00120 

Generated on Tue Oct 30 22:12:15 2007 for mbrt by  doxygen 1.5.2