RayTracing.h

Go to the documentation of this file.
00001 /* $Id:$ */
00002 
00003 #ifndef __RAY_TRACING_H
00004 #define __RAY_TRACING_H
00005 
00006 #include <gra/reflection/ReflectionModel.h>
00007 #include <gra/reflection/LocalReflection.h>
00008 #include <esg/Intersector.h>
00009 #include <esg/RefrCoefs.h>
00010 #include <esg/visitor/MatVisitor.h>
00011 #include <esg/explorer/RayIntExplorer.h>
00012 #include <esg/texture/Texture.h>
00013 
00014 
00015 /*
00016  * Global reflection model solving the rendering equation employing
00017  * the classical Whitted ray-tracing.
00018  *
00019  * Default medium refraction is refraction of surrounding environment.
00020  * This is the refraction of air by default (RERF_AIR) but arbitrary value can
00021  * be used, e.g. refraction of water for underwater scenes (REFR_WATER_20C).
00022  */
00023 
00024 using namespace esg;
00025 
00026 namespace gra {
00027 
00028 class GRA_EXPORT RayTracing : public ReflectionModel {
00029 #ifdef ESG_STATISTICS
00030 protected:
00031     unsigned long _numReflections;
00032     unsigned long _numRefractions;
00033 #endif // ESG_STATISTICS
00034     
00035 protected:
00036     AutoPtr<LocalReflection> * _pAReflection;
00037     Color3f                    _background;
00038     Intersector              * _pIntersector;
00039     unsigned                   _maxDepth;     // Maximal depth for recursion
00040     float                      _minRayWeight; // Min. treshold for refl rays
00041     float                      _defaultMediumRefraction;
00042     LocalReflection          * _pLocalReflection;
00043     RayIntExplorer             _riExplorer;
00044     Texture                  * _pTexture;
00045 
00046     Sgi::hash_map<SceneGraphObject::OID, MatVisitor*> _matCache;
00047 
00048     enum RefrDir {
00049         REFR_DIR_REFLECTION,  // reflection inside medium
00050         REFR_DIR_REFRACTION   // refraction into another medium
00051     };
00052 
00053 protected:
00054     void _check_point_of_intersection (PointEnv&) const;
00055     MatVisitor* _check_material       (SceneGraphObject*, bool);
00056     PointEnv*   _cast_ray             (const Vector3&, const Vector3&);
00057     void        _apply_texture        (PointEnv&, Color3f&);
00058     void        _apply_texture        (PointEnv&, Vector3&);
00059     void        _direct_illumination  (PointEnv&, MatVisitor&,
00060                                        unsigned,Color3f&);
00061     
00062     RefrDir _refraction_dir (PointEnv&, MatVisitor&, bool, Vector3&);
00063     void    _reflection_dir (PointEnv&, Vector3&);
00064 
00065     virtual void _cast_secondary_rays (PointEnv&, unsigned, bool, Color3f&,
00066                                        MatVisitor&);
00067     virtual void _illuminate (PointEnv&, unsigned, bool, Color3f&);
00068 
00069 public:
00070     RayTracing (AutoPtr<LocalReflection> * r = NULL,
00071                 const Color3f            & bg= Color3f(0,0,0), /*Background  */
00072                 Intersector              * i = NULL,
00073                 unsigned                   d = 1,   /*Max depth of recursion */
00074                 float                      w = .001,/*Minimal weight of rays */
00075                 float                      f = REFR_AIR/* default refraction*/)
00076         : _pAReflection(r),
00077           _background(bg),
00078           _pIntersector(i),
00079           _maxDepth(d),
00080           _minRayWeight(w),
00081           _defaultMediumRefraction(f),
00082           _riExplorer(Vector3(0,0,0), Vector3(0,0,0), NULL),
00083           _pTexture(NULL)
00084     {
00085         if (_pAReflection) {
00086             _pAReflection->registerReferer(this);
00087             _pLocalReflection = _pAReflection->origObject();
00088         } else
00089             _pLocalReflection = NULL;
00090     }
00091 
00092     virtual ~RayTracing () {
00093         if (_pAReflection)
00094             AutoPtr<LocalReflection>::destroy(_pAReflection, this);
00095         // TO DO: _matCache !!!
00096     }
00097 
00098     virtual void setDiffuseBRDF (DiffuseBRDF * b)
00099     {
00100         ReflectionModel::setDiffuseBRDF(b);
00101         if (_pLocalReflection) _pLocalReflection->setDiffuseBRDF(b);
00102     }
00103 
00104     virtual void setSpecularBRDF (SpecularBRDF * b)
00105     {
00106         ReflectionModel::setSpecularBRDF(b);
00107         if (_pLocalReflection) _pLocalReflection->setSpecularBRDF(b);
00108     }
00109     
00110     virtual void setEmittance (Emittance * e)
00111     {
00112         ReflectionModel::setEmittance(e);
00113         if (_pLocalReflection) _pLocalReflection->setEmittance(e);
00114     }
00115     
00116     virtual void setScene (Scene * s)
00117     {
00118         ReflectionModel::setScene(s);
00119         if (_pLocalReflection) _pLocalReflection->setScene(s);
00120     }
00121 
00122     /*
00123      * Required input parametters:
00124      *   PointEnv.intersection  = point of interest (PoI)
00125      *   PointEnv.pVisitableObj = object of interest
00126      *   PointEnv.normal        = surface normal at PoI
00127      *   PointEnv.viewerDir     = normalized direction from PoI to viewer
00128      *   PointEnv.distance      = distance between PoI and viewer 
00129      * [ PointEnv.trMat         = possible transformation ]
00130      * [ PointEnv.energy        = initial ray energy, default = (1,1,1) ]
00131      */
00132     virtual Color3f* illuminatePoint(PointEnv & /* point of interest */);
00133 
00134     void setIntersector (Intersector * i) { _pIntersector = i; }
00135 };
00136 
00137 
00138 } // namespace
00139 
00140 #endif // __RAY_TRACING_H

Generated on Tue Nov 21 15:11:42 2006 for gra by  doxygen 1.4.6