00001
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
00017
00018
00019
00020
00021
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;
00040 float _minRayWeight;
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,
00050 REFR_DIR_REFRACTION
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),
00072 Intersector * i = NULL,
00073 unsigned d = 1,
00074 float w = .001,
00075 float f = REFR_AIR)
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
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
00124
00125
00126
00127
00128
00129
00130
00131
00132 virtual Color3f* illuminatePoint(PointEnv & );
00133
00134 void setIntersector (Intersector * i) { _pIntersector = i; }
00135 };
00136
00137
00138 }
00139
00140 #endif // __RAY_TRACING_H