Blender V2.61 - r43446

ntl_ray.h

Go to the documentation of this file.
00001 
00004 /******************************************************************************
00005  *
00006  * El'Beem - Free Surface Fluid Simulation with the Lattice Boltzmann Method
00007  * Copyright 2003-2006 Nils Thuerey
00008  *
00009  * ray class
00010  *
00011  *****************************************************************************/
00012 #ifndef NTL_RAY_H
00013 #define NTL_RAY_H
00014 
00015 #include <sstream>
00016 #include "ntl_vector3dim.h"
00017 #include "ntl_lighting.h"
00018 #include "ntl_geometryobject.h"
00019 #include "ntl_bsptree.h"
00020 
00021 class ntlTriangle;
00022 class ntlRay;
00023 class ntlTree;
00024 class ntlScene;
00025 class ntlRenderGlobals;
00026 class ntlGeometryObject;
00027 
00029 // NOT YET USED
00030 class ntlIntersection {
00031     public:
00032 
00033         ntlIntersection() :
00034             distance(-1.0), normal(0.0),
00035             ray(NULL), tri(NULL), flags(0) { };
00036 
00037         gfxReal distance;
00038         ntlVec3Gfx normal;
00039         ntlRay *ray; 
00040         ntlTriangle *tri;
00041         char flags;
00042 };
00043 
00045 class ntlRay
00046 {
00047 public:
00048   // CONSTRUCTORS
00050   ntlRay();
00052   ntlRay(const ntlRay &r);
00054   ntlRay(const ntlVec3Gfx &o, const ntlVec3Gfx &d, unsigned int i, gfxReal contrib, ntlRenderGlobals *glob);
00056   ~ntlRay();
00057 
00059   inline void setRefracted(unsigned char set) { mIsRefracted = set; }
00060   inline void setReflected(unsigned char set) { mIsReflected = set; }
00061 
00063 
00067   const ntlColor shade() /*const*/;
00068 
00070     void tracePhoton(ntlColor) const;
00071 
00073   void intersectFrontAABB(ntlVec3Gfx mStart, ntlVec3Gfx mEnd, gfxReal &t, ntlVec3Gfx &normal, ntlVec3Gfx &retcoord) const;
00074   void intersectBackAABB(ntlVec3Gfx mStart, ntlVec3Gfx mEnd, gfxReal &t, ntlVec3Gfx &normal, ntlVec3Gfx &retcoord) const;
00075   void intersectCompleteAABB(ntlVec3Gfx mStart, ntlVec3Gfx mEnd, gfxReal &tmin, gfxReal &tmax) const;
00076     // intersection routines in bsptree.cpp
00078   inline void intersectTriangle(vector<ntlVec3Gfx> *mpV, ntlTriangle *tri, gfxReal &t, gfxReal &u, gfxReal &v) const;
00080   inline void intersectTriangleX(vector<ntlVec3Gfx> *mpV, ntlTriangle *tri, gfxReal &t, gfxReal &u, gfxReal &v) const;
00082   inline void intersectTriangleFront(vector<ntlVec3Gfx> *mpV, ntlTriangle *tri, gfxReal &t, gfxReal &u, gfxReal &v) const;
00084   inline void intersectTriangleBack(vector<ntlVec3Gfx> *mpV, ntlTriangle *tri, gfxReal &t, gfxReal &u, gfxReal &v) const;
00085 
00086   // access methods
00088   inline ntlVec3Gfx getOrigin() const { return ntlVec3Gfx(mOrigin); }
00090   inline ntlVec3Gfx getDirection() const { return ntlVec3Gfx(mDirection); }
00092   inline ntlVec3Gfx getNormal() const { return ntlVec3Gfx(mvNormal); }
00094   inline unsigned char getRefracted() const  { return mIsRefracted; }
00095   inline unsigned char getReflected() const  { return mIsReflected; }
00097   inline ntlVec3Gfx getPositionAt(gfxReal t) const { return (mOrigin+(mDirection*t)); }
00099     inline ntlRenderGlobals *getRenderglobals( void ) const { return mpGlob; }
00101     inline int getID( void ) const { return mID; }
00102 
00104   inline void setOrigin(ntlVec3Gfx set) { mOrigin = set; }
00106   inline void setDirection(ntlVec3Gfx set) { mDirection = set; }
00108   inline void setNormal(ntlVec3Gfx set) { mvNormal = set; }
00109 
00110 protected:
00111   /* Calulates the Lambertian and Specular color for
00112    * the given reflection and returns it */
00113   const ntlColor getShadedColor(ntlLightObject *light, const ntlRay &reflectedray, 
00114                                                                 const ntlVec3Gfx &normal, ntlMaterial *surf) const;
00115   
00116 private:
00118   ntlVec3Gfx     mOrigin;
00120   ntlVec3Gfx     mDirection;
00122   ntlVec3Gfx     mvNormal;
00124   unsigned int mDepth;
00126     gfxReal mContribution;
00127 
00129   ntlRenderGlobals *mpGlob;
00130 
00135   unsigned char mIsRefracted;
00136   unsigned char mIsReflected;
00137 
00139     int mID;
00140 
00141 };
00142 
00143 
00144 /******************************************************************************
00145  *
00146  * a single triangle
00147  *
00148  *****************************************************************************/
00149 
00150 // triangle intersection code in bsptree.cpp
00151 // intersectTriangle(vector<ntlVec3Gfx> *mpV, ntlTriangle *tri, gfxReal &t, gfxReal &u, gfxReal &v);
00152 
00154 #define TRI_GEOMETRY      (1<<0)
00155 #define TRI_CASTSHADOWS   (1<<1)
00156 
00157 
00158 class ntlTriangle
00159 {
00160 public:
00161   /* CONSTRUCTORS */
00163   inline ntlTriangle( void );
00165   inline ntlTriangle(int *p, bool smooth, int obj, ntlVec3Gfx norm, int setflags);
00167   inline ntlTriangle(const ntlTriangle &tri);
00169   inline ~ntlTriangle() {}
00170 
00171     /* Access methods */
00172 
00174     inline int *getPoints( void ) { return mPoints; }
00176     inline bool getSmoothNormals( void ) const { return mSmoothNormals; }
00177     inline void setSmoothNormals( bool set){ mSmoothNormals = set; }
00179     inline int getObjectId( void ) const { return mObjectId; }
00180     inline void setObjectId( int set) { mObjectId = set; }
00182     inline ntlVec3Gfx getNormal( void ) const { return mNormal; }
00183     inline void setNormal( ntlVec3Gfx set ) { mNormal = set; }
00185     inline int getFlags( void ) const { return mFlags; }
00186     inline void setFlags( int set ) { mFlags = set; }
00188     inline int  getLastRay( void ) const { return mLastRay; }
00189     inline void setLastRay( int set ) { mLastRay = set; }
00191     inline int getBBoxId( void ) const { return mBBoxId; }
00192     inline void setBBoxId( int set ) { mBBoxId = set; }
00193 
00195     inline gfxReal getAverage( int axis ) const;
00196 
00198     inline friend bool operator<(const ntlTriangle &lhs, const ntlTriangle &rhs);
00200     inline friend bool operator>(const ntlTriangle &lhs, const ntlTriangle &rhs);
00201 
00202 protected:
00203 
00204 private:
00205 
00207     int mPoints[3];
00208 
00210     int mBBoxId;
00211 
00213     bool mSmoothNormals;
00214 
00216     int mObjectId;
00217 
00219     //int mNormalIndex; ??
00220     ntlVec3Gfx mNormal;
00221 
00223     int mFlags;
00224 
00226     int mLastRay;
00227 
00228 };
00229 
00230 
00231     
00232 
00233 /******************************************************************************
00234  * Default Constructor
00235  *****************************************************************************/
00236 ntlTriangle::ntlTriangle( void ) :
00237     mBBoxId(-1),
00238     mLastRay( 0 )
00239 {
00240     mPoints[0] = mPoints[1] = mPoints[2] = 0;
00241     mSmoothNormals = 0;
00242     mObjectId = 0;
00243     mNormal = ntlVec3Gfx(0.0);
00244     mFlags = 0;
00245 }
00246 
00247 
00248 /******************************************************************************
00249  * Constructor
00250  *****************************************************************************/
00251 ntlTriangle::ntlTriangle(int *p, bool smooth, int obj, ntlVec3Gfx norm, int setflags) :
00252     mBBoxId(-1),
00253     mLastRay( 0 )
00254 {
00255     mPoints[0] = p[0];
00256     mPoints[1] = p[1];
00257     mPoints[2] = p[2];
00258     mSmoothNormals = smooth;
00259     mObjectId = obj;
00260     mNormal = norm;
00261     mFlags = setflags;
00262 }
00263 
00264 
00265 /******************************************************************************
00266  * Copy Constructor
00267  *****************************************************************************/
00268 ntlTriangle::ntlTriangle(const ntlTriangle &tri) :
00269     mBBoxId(-1),
00270     mLastRay( 0 )
00271 {
00272     mPoints[0] = tri.mPoints[0];
00273     mPoints[1] = tri.mPoints[1];
00274     mPoints[2] = tri.mPoints[2];
00275     mSmoothNormals = tri.mSmoothNormals;
00276     mObjectId      = tri.mObjectId;
00277     mNormal        = tri.mNormal;
00278     mFlags         = tri.mFlags;
00279 }
00280 
00281 
00282 
00283 
00284 /******************************************************************************
00285  * Triangle sorting functions
00286  *****************************************************************************/
00287 
00288 /* variables imported from ntl_bsptree.cc, necessary for using the stl sort funtion */
00289 /* Static global variable for sorting direction */
00290 extern int globalSortingAxis;
00291 /* Access to points array for sorting */
00292 extern vector<ntlVec3Gfx> *globalSortingPoints;
00293     
00294 
00295 gfxReal ntlTriangle::getAverage( int axis ) const
00296 { 
00297     return ( ( (*globalSortingPoints)[ mPoints[0] ][axis] + 
00298                          (*globalSortingPoints)[ mPoints[1] ][axis] + 
00299                          (*globalSortingPoints)[ mPoints[2] ][axis] )/3.0);
00300 }
00301 
00302 bool operator<(const ntlTriangle &lhs,const ntlTriangle &rhs)
00303 {
00304     return ( lhs.getAverage(globalSortingAxis) < 
00305                      rhs.getAverage(globalSortingAxis) );
00306 }
00307 
00308 bool operator>(const ntlTriangle &lhs,const ntlTriangle &rhs)
00309 {
00310     return ( lhs.getAverage(globalSortingAxis) > 
00311                      rhs.getAverage(globalSortingAxis) );
00312 }
00313 
00314 
00315 
00316 /******************************************************************************
00317  *
00318  * Scene object, that contains and manages all geometry objects
00319  *
00320  *****************************************************************************/
00321 
00322 
00323 
00324 class ntlScene
00325 {
00326 public:
00327   /* CONSTRUCTORS */
00329   ntlScene( ntlRenderGlobals *glob, bool del=true );
00331   ~ntlScene();
00332 
00334     inline void addGeoClass(ntlGeometryClass *geo) { 
00335         mGeos.push_back( geo ); 
00336         geo->setObjectId(mGeos.size());
00337     }
00339     inline void addGeoObject(ntlGeometryObject *geo) { mObjects.push_back( geo ); }
00340 
00342     inline ntlGeometryObject *getObject(int id) { 
00343         if(!mSceneBuilt) { errFatal("ntlScene::getObject","Scene not inited!", SIMWORLD_INITERROR); }
00344         return mObjects[id]; }
00345 
00347     inline vector<ntlGeometryObject*> *getObjects() { 
00348         if(!mSceneBuilt) { errFatal("ntlScene::getObjects[]","Scene not inited!", SIMWORLD_INITERROR); }
00349         return &mObjects; }
00350 
00352     inline vector<ntlGeometryClass*> *getGeoClasses() { 
00353         if(!mSceneBuilt) { errFatal("ntlScene::getGeoClasses[]","Scene not inited!", SIMWORLD_INITERROR); }
00354         return &mGeos; }
00355 
00357     //void draw();
00358     
00360     void buildScene(double time, bool firstInit);
00361     
00363     void prepareScene(double time);
00365     void cleanupScene( void );
00366 
00368     void intersectScene(const ntlRay &r, gfxReal &distance, ntlVec3Gfx &normal, ntlTriangle *&tri, int flags) const;
00369 
00371     ntlVec3Gfx getVertex(int index) { return mVertices[index]; } 
00372 
00373     // for tree generation 
00375     vector<ntlVec3Gfx> *getVertexPointer( void ) { return &mVertices; }
00377     vector<ntlVec3Gfx> *getVertexNormalPointer( void ) { return &mVertNormals; }
00379     vector<ntlTriangle> *getTrianglePointer( void ) { return &mTriangles; }
00380 
00381 private:
00382 
00384     ntlRenderGlobals *mpGlob;
00385 
00387     bool mSceneDel;
00388 
00390   vector<ntlGeometryClass *> mGeos;
00391 
00393   vector<ntlGeometryObject *> mObjects;
00394 
00396   vector<ntlTriangle> mTriangles;
00398   vector<ntlVec3Gfx>  mVertices;
00400   vector<ntlVec3Gfx>  mVertNormals;
00402   vector<ntlVec3Gfx>  mTriangleNormals;
00403 
00405     ntlTree *mpTree;
00406 
00408     int mDisplayListId;
00409 
00411     bool mSceneBuilt;
00412 
00414     bool mFirstInitDone;
00415 
00416 };
00417 
00418 
00419 #endif
00420