Object.h

Go to the documentation of this file.
00001 
00007 #ifndef OBJECT_H
00008 #define OBJECT_H OBJECT_H
00009 
00010 #include "TexturedSmoothTriangle.h"
00011 #include "SmoothTriangle.h"
00012 #include "KDTree.h"
00013 #include "Shader.h"
00014 
00019 class Object
00020 {
00025     std::vector<Primitive *> primitive; 
00026 public:
00027     
00033     void Add(Primitive *prim)
00034     {
00035         primitive.push_back(prim);
00036     };
00037 private:
00038     
00043     KDTree* kdTree;
00044     
00049     bool m_useAcc;
00050     
00055     Box boundingBox;
00056     
00061     ColorRGBA color;
00062     
00067     bool m_castShadows;
00068     
00073     bool _visible;
00074 
00075 public:
00076 
00081     Vector3D center;
00082 
00087     class PrimitiveFactory
00088     {
00089     public:
00090         virtual ~PrimitiveFactory(){}
00095         virtual Primitive* Create(Vector3D va, Vector3D vb, Vector3D vc, Vector3D na, Vector3D nb, Vector3D nc, PAIR ta, PAIR tb, PAIR tc) const = 0;
00096     };
00097 
00102     class TriangleFactory : public PrimitiveFactory
00103     {
00113         Primitive* Create(Vector3D va, Vector3D vb, Vector3D vc, Vector3D na, Vector3D nb, Vector3D nc, PAIR ta, PAIR tb, PAIR tc) const
00114         {
00115             return new Triangle(va, vb, vc);
00116         }
00117     };
00118 
00123     class SmoothTriangleFactory : public PrimitiveFactory
00124     {
00137         Primitive* Create(Vector3D va, Vector3D vb, Vector3D vc, Vector3D na, Vector3D nb, Vector3D nc, PAIR ta, PAIR tb, PAIR tc) const
00138         {
00139             return new SmoothTriangle(va, vb, vc, na, nb, nc);
00140         }
00141     };
00142 
00147     class TexturedSmoothTriangleFactory : public PrimitiveFactory
00148     {
00162         Primitive* Create(Vector3D va, Vector3D vb, Vector3D vc, Vector3D na, Vector3D nb, Vector3D nc, PAIR ta, PAIR tb, PAIR tc) const
00163         {
00164             return new TexturedSmoothTriangle(va, vb, vc, na, nb, nc, ta, tb, tc);
00165         }
00166     };
00167 
00172     Object(bool useAcc)
00173     {
00174         m_useAcc = useAcc;
00175         color = Vector3D(0,0,1);
00176         kdTree = NULL;
00177         m_castShadows = true;
00178         center = Vector3D(0.0,0.0,0.0);
00179         _visible = true;
00180     };
00181 
00182     ~Object() 
00183     {
00184         // cleanup primitives
00185         for (unsigned int i=0;i<primitive.size();i++)
00186             delete primitive[i];
00187                 
00188         // delete tree
00189         if (kdTree) delete kdTree;
00190     };
00191 
00196     void setVisible(bool val) { _visible = val; }
00197 
00202     const ColorRGBA& getColor() const { return color; }
00203 
00208     void setColor(const ColorRGBA& v) { color = v; }
00209 
00214     void setShader(Shader* sh) 
00215     {
00216         for (unsigned int i=0;i<primitive.size();i++)
00217             primitive[i]->setShader(sh);
00218     }
00219 
00226     void ParseOBJ(char *fileName, const PrimitiveFactory& factory = TriangleFactory() );
00227 
00234     bool Intersect(Ray &ray)
00235     {
00236         if (_visible) 
00237         {
00238             if(m_useAcc)
00239             {
00240                 return kdTree->Intersect(ray);
00241             }
00242             else
00243             {
00244                 bool hit = false;
00245 
00246                 for (unsigned int i=0;i<primitive.size();i++)
00247                     hit |= primitive[i]->Intersect(ray);
00248                 return hit;
00249             }
00250             return false;
00251         }
00252         else 
00253             return false;
00254     };
00255 
00262     bool Occluded(Ray &ray)
00263     {
00264         if (_visible) 
00265         {
00266             if(m_useAcc)
00267             {
00268                 return kdTree->Intersect(ray);
00269             }
00270             else
00271             {
00272                 for (unsigned int i=0;i<primitive.size();i++)
00273                     if (primitive[i]->Occluded(ray))
00274                         return true;
00275 
00276                 return false;
00277             }
00278             return false;
00279         }
00280         else 
00281             return false;
00282     };
00283 
00289     Box CalcBounds()
00290     {
00291         boundingBox.Clear();
00292 
00293         for (int i=0;i<(int)primitive.size();i++) 
00294         {
00295             Box box = primitive[i]->GetBounds();
00296             boundingBox.Extend(box);
00297         }
00298         center = boundingBox.min;
00299         return boundingBox;
00300     }
00301 
00306     void BuildAccelStructure()
00307     {
00308         if (!m_useAcc) return;
00309 
00310         // compute the bounding box
00311         boundingBox = CalcBounds();
00312 
00313         // recreate the tree if already created
00314         if (kdTree) delete kdTree;
00315         kdTree = new KDTree();
00316 
00317         // build acceleration data structure
00318         kdTree->BuildTree(primitive, boundingBox);
00319     }
00320 
00325     inline bool isCastShadows() const { return m_castShadows; }
00326     
00331     inline void setCastShadows(bool b)
00332     {
00333         // this object's cast shadow flag
00334         m_castShadows = b;
00335 
00336         // set to each primitive 
00337         for (int i=0;i<(int)primitive.size();i++) 
00338         {
00339             primitive[i]->castShadows() = b;
00340         }
00341     }
00342 
00343 };
00344 
00345 #endif

Generated on Thu Jan 31 21:48:49 2008 for RayTracer by  doxygen 1.5.4