00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
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
00058
00059
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
00095 NVSG_API virtual void onReleaseDAL(nvsg::HDAL hDAL);
00096
00097 protected:
00099 NVSG_API virtual ~GLTraverser(void);
00100
00101
00102 NVSG_API virtual void doApply( const nvsg::ViewState *pViewState, const nvsg::Scene *pScene );
00103
00104
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
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;
00175 bool m_resetStateAttribute;
00176
00177 private:
00178 #if defined(_DEBUG) && defined(_WIN32)
00179 HGLRC m_hglrc;
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
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
00232 void selectRenderPath();
00233
00234 void (GLTraverser::*m_pfnProcessQuads)( const nvsg::Quads *p );
00235
00236 void (GLTraverser::*m_pfnProcessQuadStrips)( const nvsg::QuadStrips *p );
00237
00238 void (GLTraverser::*m_pfnProcessTriangles)( const nvsg::Triangles *p );
00239
00240 void (GLTraverser::*m_pfnProcessTriStrips)( const nvsg::TriStrips *p );
00241
00242
00243 void GLTraverser::prepareEffect( void );
00244
00245
00246 void processQuads( const nvsg::Quads *p );
00247
00248 void processQuadStrips( const nvsg::QuadStrips *p );
00249
00250 void processTriangles( const nvsg::Triangles *p );
00251
00252 void processTriStrips( const nvsg::TriStrips *p );
00253
00254 void processGeoSet( const nvsg::GeoSet *p );
00255
00256
00257 void drawHighlightBox( const nvsg::HighlightBox *box );
00258
00259
00260
00261 void resizeOITBuffers();
00262
00263 struct VBOInfo;
00264
00265 void processQuadsVBO( const nvsg::Quads * p );
00266
00267 void processQuadStripsVBO( const nvsg::QuadStrips * p );
00268
00269 void processTrianglesVBO( const nvsg::Triangles * p );
00270
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
00277 void processQuadsDL( const nvsg::Quads * p );
00278
00279 void processQuadStripsDL( const nvsg::QuadStrips * p );
00280
00281 void processTrianglesDL( const nvsg::Triangles * p );
00282
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;
00288 bool m_isCgFxAware;
00289
00290
00291 const nvsg::StateSet * m_stateSet;
00292
00293
00294 bool m_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;
00304 GLuint m_dataSize;
00305 GLubyte * m_imageData;
00306 GLushort * m_accumData;
00307 bool m_accumulationInitialized;
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
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;
00333 bool m_transparentPass;
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
00355 enum
00356 {
00357 GL_VBO = RT_GL | 0x01
00358 , GL_DL = RT_GL | 0x02
00359 };
00360
00361 std::vector<nvsg::HDAL> m_cachedGeometry;
00362
00363 enum {
00364 VERTEX_BUFFER = 0
00365 , INDEX_BUFFER
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];
00378
00379 GLenum indexType;
00380 GLsizei numIndices;
00381
00382 GLvoid ** primitiveOffsets;
00383 GLsizei * primitiveIndexCount;
00384
00385 GLsizei numBytes;
00386 GLsizei stride;
00387 size_t attribOffsets[nvsg::GeoSet::NUMBER_OF_VERTEX_ATTRIBUTES];
00388 };
00389
00390
00391
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
00404
00405 static bool isDLInfoValid(const GLTraverser::DLInfo* info);
00406 void processDL( DLInfo *dlInfo );
00407
00408
00409 enum
00410 {
00411 GL_TEX = RT_GL | 0x04
00412 };
00413
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;
00426
00427 nvmath::Vec3f m_accScaling;
00428 };
00429
00430 inline unsigned char * GLTraverser::StipplePatterns::getBits( int index )
00431 {
00432 return( (unsigned char *) &stippleBits[ index ][ 0 ] );
00433 }
00434 }