Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

GLTraverser.h

Go to the documentation of this file.
00001 // Copyright NVIDIA Corporation 2002-2004
00002 // TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
00003 // *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
00004 // OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
00005 // AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
00006 // BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
00007 // WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
00008 // BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS)
00009 // ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS
00010 // BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES 
00011 
00012 #pragma once
00013 
00015 #include "nvsgcommon.h"
00016 #include <list>
00017 #include "nvgl/GLContext.h"
00018 #include "nvtraverser/RenderTraverser.h"
00019 #include "nvsg/GeoSet.h"
00020 
00021 namespace nvgl
00022 {
00023   class GLContext;
00024   class PBuffer;
00025 }
00026 namespace nvsg
00027 {
00028   class Texture;
00029 }
00030 
00031 namespace nvtraverser
00032 {
00033 
00034 // common device context helper data types for Windows and Linux
00035 #if defined(_WIN32)
00036   typedef HDC   DEVICE_CONTEXT;
00037   typedef HGLRC RENDER_CONTEXT;      
00038 #elif defined(LINUX)
00039   typedef Drawable   DEVICE_CONTEXT;
00040   typedef GLXContext RENDER_CONTEXT;
00041 #endif
00042   
00044 #if defined(_WIN32) && defined(_DEBUG)
00045   #define __ASSERT_CONTEXT() assertContext(); 
00046 #else
00047   #define __ASSERT_CONTEXT()
00048 #endif
00049 
00051   class GLTraverser : public RenderTraverser
00052   {
00053     public:
00055       NVSG_API GLTraverser(void);
00056 
00057       //--  Functions specific to GLTraverser --
00058 
00059       //--  Functions implemented from RenderTraverser  --
00060       NVSG_API virtual JitterTechnique getJitterTechnique( void ) const;
00061       NVSG_API virtual bool isJitterTechniqueSupported( JitterTechnique jt               
00062                                                       ) const;
00063       NVSG_API virtual bool setJitterTechnique( JitterTechnique jt                       
00064                                               );
00065       NVSG_API virtual RenderTechnique getRenderTechnique( void ) const;
00066       NVSG_API virtual bool isRenderTechniqueSupported( RenderTechnique rt               
00067                                                       ) const;
00068       NVSG_API virtual bool setRenderTechnique( RenderTechnique rt                       
00069                                               );
00070       NVSG_API virtual TransparencyTechnique getTransparencyTechnique( void ) const;
00071       NVSG_API virtual bool isTransparencyTechniqueSupported( TransparencyTechnique tt   
00072                                                             ) const;
00073       NVSG_API virtual bool setTransparencyTechnique( TransparencyTechnique tt           
00074                                                     );
00075       NVSG_API virtual OITTechnique getOITTechnique( void ) const;
00076       NVSG_API virtual bool isOITTechniqueSupported( OITTechnique ot                     
00077                                                    ) const;
00078       NVSG_API virtual bool setOITTechnique( OITTechnique ot                             
00079                                            );
00080       NVSG_API virtual size_t getOITMaxPasses( void ) const;
00081       NVSG_API virtual bool setOITMaxPasses( size_t maxPasses                            
00082                                            );
00083       NVSG_API virtual float getOITThreshold( void ) const;
00084       NVSG_API virtual bool setOITThreshold( float threshold  
00085                                            );
00086       NVSG_API virtual GeometryCacheTechnique getGeometryCacheTechnique( void ) const;
00087       NVSG_API virtual bool isGeometryCacheTechniqueSupported( GeometryCacheTechnique gct  
00088                                                              ) const;
00089       NVSG_API virtual bool setGeometryCacheTechnique( GeometryCacheTechnique gct          
00090                                                      );
00091       NVSG_API virtual void getTextureSizeLimits( size_t &width, size_t &height ) const;
00092       NVSG_API virtual void setTextureSizeLimits( size_t width, size_t height );
00093 
00094       // CachedGeometryCreator interface
00095       NVSG_API virtual void onReleaseDAL(nvsg::HDAL hDAL);
00096 
00097     protected:
00099       NVSG_API virtual ~GLTraverser(void);
00100 
00101       // doApply override
00102       NVSG_API virtual void doApply( const nvsg::ViewState *pViewState, const nvsg::Scene *pScene );
00103      
00104       // handler routines overrides
00105 
00107       NVSG_API virtual void handleTransform(const nvsg::Transform * p);
00109       NVSG_API virtual void handleDirectedLight(const nvsg::DirectedLight * p);
00111       NVSG_API virtual void handlePointLight(const nvsg::PointLight * p);
00113       NVSG_API virtual void handleSpotLight(const nvsg::SpotLight * p);
00115       NVSG_API virtual void handleQuads(const nvsg::Quads * p);
00117       NVSG_API virtual void handleAnimatedQuads(const nvsg::AnimatedQuads * p);
00119       NVSG_API virtual void handleQuadStrips(const nvsg::QuadStrips * p);
00121       NVSG_API virtual void handleTriangles(const nvsg::Triangles * p);
00123       NVSG_API virtual void handleAnimatedTriangles(const nvsg::AnimatedTriangles * p);
00125       NVSG_API virtual void handleSkinnedTriangles(const nvsg::SkinnedTriangles * p);
00127       NVSG_API virtual void handleTriStrips(const nvsg::TriStrips * p);
00129       NVSG_API virtual void handleStateSet(const nvsg::StateSet * p);
00131       NVSG_API virtual void handleCgFx(const nvsg::CgFx * p);
00133       NVSG_API virtual void handleFaceAttribute(const nvsg::FaceAttribute * p);
00135       NVSG_API virtual void handleMaterial(const nvsg::Material * p);
00137       NVSG_API virtual void handleTextureAttribute(const nvsg::TextureAttribute * p);
00139       NVSG_API virtual void handleTextureAttributeItem(const nvsg::TextureAttributeItem * p, size_t tu);
00141       NVSG_API virtual void handleJitterCamera(const nvsg::JitterCamera * p, const nvsg::Node * root);
00143       NVSG_API virtual void handleParallelCamera(const nvsg::ParallelCamera * p, const nvsg::Node * root);
00145       NVSG_API virtual void handlePerspectiveCamera(const nvsg::PerspectiveCamera * p, const nvsg::Node * root);
00147       NVSG_API virtual void handleStereoCamera(const nvsg::StereoCamera * p, const nvsg::Node * root);
00149       NVSG_API virtual void handleHighlightBox(const nvsg::HighlightBox * p, const nvsg::GeoSet * geoSet);
00151       NVSG_API virtual void handleHighlightEdges(const nvsg::HighlightEdges * p, const nvsg::GeoSet * geoSet);
00152 
00154 
00156       NVSG_API virtual bool preTraverseTransform( const nvsg::Transform *p);
00157 
00159 
00160       NVSG_API virtual bool preTraverseRoot( const nvsg::SimpleCamera *p, const nvsg::Node *root);
00161 
00163 
00164       NVSG_API virtual bool preTraverseDrawable( const nvsg::Drawable *p );
00165 
00166       // This function is called from the RenderTraverser framework whenever a LightSource is to be disabled.
00167       NVSG_API virtual void disableLightSource(const nvsg::LightSource *p);
00168 
00169 #if defined(_DEBUG) && defined(_WIN32)
00170       void assertContext();
00171 #endif
00172 
00173     protected:
00174       const nvsg::CgFx      * m_effect;                 // current effect attrib
00175       bool                    m_resetStateAttribute;    // used with internal state caching
00176 
00177     private:
00178 #if defined(_DEBUG) && defined(_WIN32)
00179       HGLRC m_hglrc; // only for asserting the right current context
00180 #endif
00181 
00182       void  adjustRenderTechnique( bool on );
00183 
00184       void  initGLState( void );
00185 
00186       void  syncCgFxTweakables( const nvsg::CgFx * p);
00187       void  syncCgFxTransforms(const nvsg::CgFx * p);
00188 
00189       bool  loadTexture(const nvsg::Texture * pTex, GLuint texHdl);
00190       void  uploadTexture1D( const nvsg::Texture *p );
00191       void  uploadTexture2D( const nvsg::Texture *p, unsigned int imageIndex = 0, GLenum target = GL_TEXTURE_2D );
00192       void  uploadTexture3D( const nvsg::Texture *p );
00193 
00194       void  clearJitterTechnique( void );
00195       void  initJitterTechnique( void );
00196       void  initJitterTechniquePBuffer( void );
00197       void  jitterAccum( const nvsg::JitterCamera *p, const nvsg::Node *root );
00198       void  jitterPBuffer( const nvsg::JitterCamera *p, const nvsg::Node *root );
00199       void  jitterReadPixels( const nvsg::JitterCamera *p, const nvsg::Node *root );
00200 
00201       void  clearTransparencyTechnique( void );
00202       void  initTransparencyTechnique( void );
00203       void  clearOITTechnique( void );
00204       void  initOITTechnique( void );
00205       void  allocOITPBuffers( void );
00206       void  freeOITPBuffers( void );
00207       void  allocOITTextures( void );
00208       void  freeOITTextures( void );
00209       void  setupOITRendering( void );
00210 
00211       void  prepareHighlight( const nvmath::Vec3f &color, float lineWidth );
00212 
00213       void  freeDeviceDependentData(std::vector<nvsg::HDAL>& data, unsigned int dataID);
00214 
00216       GLenum  glTraverse( const nvsg::LightSource *p        
00217                         , size_t index                
00218                         );
00219 
00220       // re-routing to base implementation used with glTraverse template functions below
00221       void handlePerspectiveCameraCallBaseImplementation(const nvsg::PerspectiveCamera * p, const nvsg::Node *root);
00222       void handleParallelCameraCallBaseImplementation(const nvsg::ParallelCamera * p, const nvsg::Node *root);
00223 
00224       template<typename camera, typename defaultHandler>
00225       void  glTraverse( const camera *p, const nvsg::Node *root, defaultHandler defHandler );
00226 
00227       template<typename camera, typename defaultHandler>
00228       void  glTraverseOIT( const camera *p, const nvsg::Node *root, defaultHandler defHandler );
00229 
00230 
00231       // select render path to go 
00232       void selectRenderPath();
00233       // function pointer via to invoke quad rendering routines below
00234       void (GLTraverser::*m_pfnProcessQuads)( const nvsg::Quads *p );
00235       // function pointer via to invoke QuadStrips rendering routines below
00236       void (GLTraverser::*m_pfnProcessQuadStrips)( const nvsg::QuadStrips *p );
00237       // function pointer via to invoke triangle rendering routines below
00238       void (GLTraverser::*m_pfnProcessTriangles)( const nvsg::Triangles *p );
00239       // function pointer via to invoke TriStrips rendering routines below
00240       void (GLTraverser::*m_pfnProcessTriStrips)( const nvsg::TriStrips *p );
00241 
00242       // prepare effected GeoSet
00243       void  GLTraverser::prepareEffect( void );
00244 
00245       // process quads using a default render path 
00246       void processQuads( const nvsg::Quads *p );
00247       // process QuadStrips using a default render path
00248       void processQuadStrips( const nvsg::QuadStrips *p );
00249       // process triangles using a default render path 
00250       void processTriangles( const nvsg::Triangles *p );
00251       // process TriStrips using a default render path
00252       void processTriStrips( const nvsg::TriStrips *p );
00253       // common processing on GeoSet
00254       void processGeoSet( const nvsg::GeoSet *p );
00255 
00256       // draw a highlight box (ignoring current tree states)
00257       void drawHighlightBox( const nvsg::HighlightBox *box );
00258       
00259       // check if the viewport size has changed, and if yes resize
00260       // our texture or pbuffer objects for OIT
00261       void resizeOITBuffers();
00262 
00263       struct VBOInfo;
00264       // process quads using vertex buffer objects (VBO) extension 
00265       void processQuadsVBO( const nvsg::Quads * p );
00266       // process QuadStrips using vertex buffer objects (VBO) extension 
00267       void processQuadStripsVBO( const nvsg::QuadStrips * p );
00268       // process triangles using vertex buffer objects (VBO) extension 
00269       void processTrianglesVBO( const nvsg::Triangles * p );
00270       // process TriStrips using vertex buffer objects (VBO) extension 
00271       void processTriStripsVBO( const nvsg::TriStrips * p );
00272       void processGeoSetVBO( const nvsg::GeoSet *p, const VBOInfo* vboInfo );
00273       VBOInfo * getCachedVBOInfo(const nvsg::GeoSet * p) const;
00274 
00275       struct DLInfo;
00276       // process quads using OpenGL display lists 
00277       void processQuadsDL( const nvsg::Quads * p );
00278       // process QuadStrips using OpenGL display lists 
00279       void processQuadStripsDL( const nvsg::QuadStrips * p );
00280       // process triangles using OpenGL display lists 
00281       void processTrianglesDL( const nvsg::Triangles * p );
00282       // process TriStrips using OpenGL display lists 
00283       void processTriStripsDL( const nvsg::TriStrips * p );
00284       DLInfo * processGeoSetDL( const nvsg::GeoSet *p );
00285       DLInfo * getCachedDLInfo(const nvsg::GeoSet * p) const;
00286 
00287       const nvsg::Scene     * m_scene;                  // a reference to the currently traversed scene
00288       bool                    m_isCgFxAware;            // assugns whether the current system is CgFX aware
00289 //      const nvsg::CgFx      * m_effect;                 // current effect attrib
00290 
00291       const nvsg::StateSet  * m_stateSet;
00292 //      bool                    m_resetStateAttribute;    // used with internal state caching
00293                                                         // need to check this inside the StateAttribute handler routines
00294       bool                    m_textured;               // the current geometry is textured
00295 
00296       std::list<const nvsg::StateAttribute *>   m_attrList[2];
00297       std::list<const nvsg::StateAttribute *> * m_currentList;
00298       std::list<const nvsg::StateAttribute *> * m_nextList;
00299 
00300       int                   m_viewportWidth;
00301       int                   m_viewportHeight;
00302       JitterTechnique       m_jitterTechnique;
00303       float                 m_fraction;                     // independent of special jitter technique
00304       GLuint                m_dataSize;                     // for read-pixels-based jittering
00305       GLubyte             * m_imageData;
00306       GLushort            * m_accumData;
00307       bool                  m_accumulationInitialized;      // for pbuffer-based jittering
00308       GLuint                m_fpAccumulate;
00309       GLuint                m_fpLoad;
00310       GLuint                m_fpReturn;
00311       nvgl::PBuffer       * m_fBuffer[2];
00312       nvgl::PBuffer       * m_jitterPBuffer;
00313 
00314       RenderTechnique       m_renderTechnique;
00315 
00316       // A little helper class for screen door transparency.
00317       class StipplePatterns
00318       {
00319       public:
00320         StipplePatterns();
00321         unsigned char  * getBits(int index);
00322 
00323       private:
00324         unsigned char  patBytes[ 64 ][ 8 ];
00325         unsigned long stippleBits[64][32];
00326 
00327         StipplePatterns( const StipplePatterns &stipplePatterns );
00328         StipplePatterns& operator=( const StipplePatterns &stipplePatterns );
00329       };
00330 
00331       TransparencyTechnique m_transparencyTechnique;
00332       StipplePatterns       m_stipple;                      // for screen door transparency
00333       bool                  m_transparentPass;              // for blended transparency
00334       bool                  m_isTransparentMaterial;
00335       bool                  m_isTransparentTexture;
00336       OITTechnique          m_oitTechnique;
00337       size_t                m_oitMaxPasses;
00338       bool                  m_oitInitialized;
00339       bool                  m_oitSetupPending;
00340       int                   m_oitWidth;
00341       int                   m_oitHeight;
00342       std::vector<GLuint>   m_oitRGBATextureQueries;
00343       std::vector<GLuint>   m_oitPBufferQueries;
00344       std::vector<GLuint> * m_oitOcclusionQueries;
00345       GLuint                m_fpPeel[2];
00346       float                 m_oitThreshold;
00347       size_t                m_oitThresholdPixels;
00348       GLuint                m_oitDepthTexture;
00349       GLint                 m_oitDepthFormat;
00350       std::vector<GLuint>        m_oitRGBATextures;
00351       std::vector<nvgl::PBuffer*>      m_oitPBuffers;
00352       GeometryCacheTechnique  m_geometryCacheTechnique;
00353 
00354       // identifier for a certain cached geometry 
00355       enum
00356       {
00357         GL_VBO = RT_GL | 0x01
00358       , GL_DL  = RT_GL | 0x02
00359       };
00360       // cached geometry storage 
00361       std::vector<nvsg::HDAL> m_cachedGeometry;
00362       
00363       enum {
00364         VERTEX_BUFFER = 0  // index of buffer holding vertex data
00365       , INDEX_BUFFER       // index of buffer holding indices
00366       };
00367 
00368       struct VBOInfo : public nvsg::DALData
00369       {
00370         VBOInfo(nvsg::DALDataCreator * creator, const nvsg::DALHost * host); 
00371         ~VBOInfo(); 
00372 
00373         const nvgl::GLContext * context;
00374         DEVICE_CONTEXT    hdc;            
00375         RENDER_CONTEXT    hglrc;
00376 
00377         GLuint vBufferID[2];   // buffer ids for vertex and index buffer
00378         // index buffer related
00379         GLenum indexType;      // type used with indices (either GL_UNSIGNED_INT or GL_UNSIGNED_SHORT)
00380         GLsizei numIndices;     // total number of indices
00381         // multi draw elements related
00382         GLvoid ** primitiveOffsets;    // per primitive offset into index buffer 
00383         GLsizei * primitiveIndexCount; // per primitive index count
00384         // vertex buffer related
00385         GLsizei numBytes;       // size in bytes of the vertex buffer
00386         GLsizei stride;         // byte offset between consecutive vertex attribute data of same type
00387         size_t attribOffsets[nvsg::GeoSet::NUMBER_OF_VERTEX_ATTRIBUTES]; // per vertex attribute offset into vertex buffer
00388       };
00389 
00390       // predicate that takes a pointer to a VBOInfo objects and decides 
00391       // whether that VBOInfo is valid for the current render context
00392       static bool isVBOInfoValid(const GLTraverser::VBOInfo * info);
00393 
00394       struct DLInfo : public nvsg::DALData
00395       {
00396         DLInfo(nvsg::DALDataCreator * creator, const nvsg::DALHost * host); 
00397         ~DLInfo();
00398 
00399         const nvgl::GLContext * context;
00400         GLuint listID;
00401       };
00402 
00403       // predicate that takes a pointer to a DLInfo objects and decides 
00404       // whether that DLInfo is valid for the current render context
00405       static bool isDLInfoValid(const GLTraverser::DLInfo* info);
00406       void processDL( DLInfo *dlInfo );
00407 
00408       // identifier for TexInfo data
00409       enum
00410       {
00411         GL_TEX = RT_GL | 0x04
00412       };
00413       // DAL data carrying device dependent texture IDs  
00414       struct TexInfo : public nvsg::DALData
00415       {
00416         TexInfo(nvsg::DALDataCreator * creator, const nvsg::DALHost * host, const nvsg::Texture * tex);
00417         ~TexInfo();
00418         const nvsg::Texture * texture;
00419         GLuint textureHandle;
00420       };
00421 
00422       GLint               m_maxTextureSize;
00423       size_t              m_textureLimitWidth;
00424       size_t              m_textureLimitHeight;
00425       std::vector<nvsg::HDAL> m_textures; // DAL handles to TexInfos will be collected here
00426 
00427       nvmath::Vec3f m_accScaling;   //  accumulated scaling factors for enabling/disabling normalization of normals
00428   };
00429 
00430   inline  unsigned char *  GLTraverser::StipplePatterns::getBits( int index )
00431   {
00432     return( (unsigned char *) &stippleBits[ index ][ 0 ] );
00433   }
00434 }

Generated on Tue Mar 1 13:19:20 2005 for NVSGSDK by NVIDIA