00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #pragma once
00013
00015 #include "nvsgcommon.h"
00016
00017 #include "nvsg/Drawable.h"
00018 #include "nvsg/Path.h"
00019 #include "nvtraverser/ModelViewTraverser.h"
00020
00021 namespace nvsg
00022 {
00023 class Camera;
00024 }
00025
00026 namespace nvtraverser
00027 {
00029
00032 class Intersection
00033 {
00034 public:
00036
00038 NVSG_API Intersection( const nvsg::Path * pPath
00039 , const nvsg::Drawable * pDrawable
00040 , const nvmath::Vec3f & isp
00041 , const float & dist
00042 );
00043
00045
00046 NVSG_API Intersection( const Intersection & rhs
00047 );
00048
00050 NVSG_API virtual ~Intersection(void);
00051
00053 NVSG_API Intersection & operator = (const Intersection & rhs);
00054
00056
00057 NVSG_API const nvsg::Path * getPath() const;
00058
00060
00061 NVSG_API const nvsg::Drawable * getDrawable() const;
00062
00064
00065 NVSG_API const nvmath::Vec3f & getIsp() const;
00066
00068
00069 NVSG_API const float & getDist() const;
00070
00071 protected:
00073
00074 NVSG_API Intersection(void){};
00075
00077
00078 NVSG_API void clone( const Intersection & rhs
00079 );
00080
00081 const nvsg::Drawable * m_pDrawable;
00082 const nvsg::Path * m_pPath;
00083 nvmath::Vec3f m_isp;
00084 float m_dist;
00085 };
00086
00087 inline Intersection::~Intersection(void)
00088 {
00089 if (m_pDrawable)
00090 {
00091 m_pDrawable->removeRef();
00092 m_pDrawable = NULL;
00093 }
00094
00095 if (m_pPath)
00096 {
00097 m_pPath->removeRef();
00098 m_pPath = NULL;
00099 }
00100 }
00101
00102 inline Intersection::Intersection (const nvsg::Path * pPath, const nvsg::Drawable * pDrawable, const nvmath::Vec3f & isp, const float & dist)
00103 {
00104 __ASSERT(pDrawable);
00105 __ASSERT(pPath);
00106
00107 m_isp = isp;
00108 m_dist = dist;
00109
00110 m_pDrawable = pDrawable;
00111 m_pDrawable->addRef();
00112
00113 m_pPath = pPath;
00114 m_pPath->addRef();
00115 }
00116
00117 inline Intersection::Intersection (const Intersection & rhs)
00118 {
00119 clone(rhs);
00120 }
00121
00122 inline Intersection & Intersection::operator = (const Intersection & rhs)
00123 {
00124 clone(rhs);
00125 return (*this);
00126 }
00127
00128 inline void Intersection::clone(const Intersection & rhs)
00129 {
00130 m_isp = rhs.m_isp;
00131 m_dist = rhs.m_dist;
00132
00133 m_pDrawable = rhs.m_pDrawable;
00134 m_pDrawable->addRef();
00135
00136 m_pPath = rhs.m_pPath;
00137 m_pPath->addRef();
00138 }
00139
00140 inline const nvmath::Vec3f & Intersection::getIsp() const
00141 {
00142 return m_isp;
00143 }
00144
00145 inline const float & Intersection::getDist() const
00146 {
00147 return m_dist;
00148 }
00149
00150 inline const nvsg::Drawable * Intersection::getDrawable() const
00151 {
00152 return m_pDrawable;
00153 }
00154
00155 inline const nvsg::Path * Intersection::getPath() const
00156 {
00157 return m_pPath;
00158 }
00159
00160
00162
00168 class RayIntersectTraverser : public ModelViewTraverser
00169 {
00170 public:
00172 NVSG_API RayIntersectTraverser(void);
00173
00175
00177 NVSG_API void release();
00178
00180
00182 NVSG_API void setRay( const nvmath::Vec3f &origin
00183 , const nvmath::Vec3f &dir
00184 );
00185
00187
00189 NVSG_API void setCamClipping( bool b
00190 );
00191
00193
00194 NVSG_API const Intersection & getNearest( void ) const;
00195
00197
00198 NVSG_API const Intersection * getIntersections( void ) const;
00199
00201
00202 NVSG_API size_t getNumberOfIntersections( void ) const;
00203
00205
00206 NVSG_API void setBackFaceCulling( bool enable
00207 );
00208
00210
00212 NVSG_API bool isBackFaceCullingEnabled();
00213
00214 protected:
00216 NVSG_API virtual ~RayIntersectTraverser(void);
00217
00219
00221 NVSG_API virtual void doApply( const nvsg::ViewState *pViewState
00222 , const nvsg::Scene *pScene
00223 );
00224
00225
00226 NVSG_API virtual void handleGeoNode( const nvsg::GeoNode *p );
00227
00228
00229 NVSG_API virtual void handleGroup( const nvsg::Group * p);
00230 NVSG_API virtual void handleLOD( const nvsg::LOD * p);
00231 NVSG_API virtual void handleSwitch( const nvsg::Switch * p);
00232 NVSG_API virtual void handleTransform( const nvsg::Transform * p);
00233
00234
00235 NVSG_API virtual void handleQuads( const nvsg::Quads *p );
00236 NVSG_API virtual void handleQuadStrips( const nvsg::QuadStrips *p );
00237 NVSG_API virtual void handleTriangles( const nvsg::Triangles *p );
00238 NVSG_API virtual void handleTriStrips( const nvsg::TriStrips *p );
00239
00241
00243 NVSG_API virtual bool preTraverseTransform(
00244 const nvsg::Transform *p
00245 );
00246
00248
00249 NVSG_API virtual void postTraverseTransform(
00250 const nvsg::Transform *p
00251 );
00252
00254
00259 enum ISType { _NONE = 0
00260 , _BSTANGENT
00261 , _BSNEAR
00262 , _BSFAR
00263 , _BSNEARFAR
00264 };
00265
00267
00268 NVSG_API bool checkIntersection( const nvmath::Sphere3f &sphere
00269 );
00270
00272
00276 NVSG_API bool intersectTriangle( const nvmath::Vec3f & v0
00277 , const nvmath::Vec3f & v1
00278 , const nvmath::Vec3f & v2
00279 , nvmath::Vec3f & isp
00280 , float & dist
00281 );
00282
00284
00285 NVSG_API void storeIntersection( const nvsg::GeoSet *p
00286 , const nvmath::Vec3f &isp
00287 , float dist
00288 );
00289
00290 #ifdef _DEBUG
00291
00292 void dumpIntersectionList();
00293 #endif
00294
00295 static const nvmath::Vec3f _RAY_ORIGIN_DEFAULT;
00296 static const nvmath::Vec3f _RAY_DIRECTION_DEFAULT;
00297
00298 bool m_backFaceCulling;
00299 bool m_ccw;
00300 bool m_camClipping;
00301 nvmath::Vec3f m_rayOrigin;
00302 nvmath::Vec3f m_rayDir;
00303 const nvsg::Camera * m_camera;
00304 std::stack<nvmath::Vec3f> m_msRayOrigin;
00305 std::stack<nvmath::Vec3f> m_msRayDir;
00306 std::stack<nvmath::Vec3f> m_msCamDir;
00307 std::vector<Intersection> m_intersectionList;
00308 size_t m_nearestIntIdx;
00309 nvsg::Path * m_curPath;
00310 size_t m_currentFrame;
00311 float m_scaleFactor;
00312 };
00313
00314 inline void RayIntersectTraverser::setCamClipping(bool flag)
00315 {
00316 m_camClipping = flag;
00317 }
00318
00319 inline size_t RayIntersectTraverser::getNumberOfIntersections() const
00320 {
00321 return m_intersectionList.size();
00322 }
00323
00324 inline const Intersection & RayIntersectTraverser::getNearest() const
00325 {
00326 __ASSERT(!m_intersectionList.empty());
00327 return m_intersectionList[m_nearestIntIdx];
00328 }
00329
00330 inline const Intersection * RayIntersectTraverser::getIntersections() const
00331 {
00332 __ASSERT(!m_intersectionList.empty());
00333 return &m_intersectionList[0];
00334 }
00335
00336 inline void RayIntersectTraverser::setBackFaceCulling( bool enable)
00337 {
00338 m_backFaceCulling = enable;
00339 }
00340
00341 inline bool RayIntersectTraverser::isBackFaceCullingEnabled()
00342 {
00343 return m_backFaceCulling;
00344 }
00345 }