OverlayNode 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGSIM_OVERLAYNODE
  14. #define OSGSIM_OVERLAYNODE 1
  15. #include <osg/buffered_value>
  16. #include <osg/Camera>
  17. #include <osg/Texture2D>
  18. #include <osg/TexGenNode>
  19. #include <osg/Geode>
  20. #include <osgUtil/CullVisitor>
  21. #include <osgSim/Export>
  22. namespace osgSim {
  23. /** OverlayNode is for creating texture overlays on scenes, with the overlay texture being generated
  24. * by pre rendering an Overlay Subgraph to a texture, then projecting this resulting texture on the scene.*/
  25. class OSGSIM_EXPORT OverlayNode : public osg::Group
  26. {
  27. public :
  28. enum OverlayTechnique
  29. {
  30. OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY,
  31. VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY,
  32. VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY
  33. };
  34. OverlayNode(OverlayTechnique technique=OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY);
  35. OverlayNode(const OverlayNode& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  36. META_Node(osgSim, OverlayNode);
  37. virtual void traverse(osg::NodeVisitor& nv);
  38. void setOverlayTechnique(OverlayTechnique technique);
  39. OverlayTechnique getOverlayTechnique() const { return _overlayTechnique; }
  40. /** Set the implementation to be used when creating the overlay texture. */
  41. void setRenderTargetImplementation(osg::Camera::RenderTargetImplementation impl);
  42. /** Set the overlay subgraph which will be rendered to texture.*/
  43. void setOverlaySubgraph(osg::Node* node);
  44. template<class T> void setOverlaySubgraph(const osg::ref_ptr<T>& node) { setOverlaySubgraph(node.get()); }
  45. /** Get the overlay subgraph which will be rendered to texture.*/
  46. osg::Node* getOverlaySubgraph() { return _overlaySubgraph.get(); }
  47. /** Get the const overlay subgraph which will be render to texture.*/
  48. const osg::Node* getOverlaySubgraph() const { return _overlaySubgraph.get(); }
  49. /** Inform the OverlayNode that the overlay texture needs to be updated.*/
  50. void dirtyOverlayTexture();
  51. /** Set whether the OverlayNode should update the overlay texture on every frame.*/
  52. void setContinuousUpdate(bool update) { _continuousUpdate = update; }
  53. /** Get whether the OverlayNode should update the overlay texture on every frame.*/
  54. bool getContinuousUpdate() const { return _continuousUpdate; }
  55. /** Set the base height that the overlay subgraph will be projected down to.
  56. * Normally you'll set this to just below ground level, if you set it too high
  57. * then the overlay texture can end up being clipped in certain viewing directions,
  58. * while if its too low then there will be a limit to how close you can get to the
  59. * terrain before pixaltion becomes an issue.*/
  60. void setOverlayBaseHeight(double baseHeight) { _overlayBaseHeight = baseHeight; }
  61. /** Get the base height that the overlay subgraph will be projected down to.*/
  62. double getOverlayBaseHeight() const { return _overlayBaseHeight; }
  63. /** Set the clear color to use when rendering the overlay subgraph.*/
  64. void setOverlayClearColor(const osg::Vec4& color) { _overlayClearColor = color; }
  65. /** Get the clear color to use when rendering the overlay subgraph.*/
  66. const osg::Vec4& getOverlayClearColor() const { return _overlayClearColor; }
  67. /** Set the TexEnv mode used to combine the overlay texture with the base color/texture of the OverlayNode's decorate subgraph.*/
  68. void setTexEnvMode(GLenum mode);
  69. /** Get the TexEnv mode used to combine the overlay texture with the base color/texture of the OverlayNode's decorate subgraph.*/
  70. GLenum getTexEnvMode() const { return _texEnvMode; }
  71. /** Set the texture unit that the texture should be assigned to.*/
  72. void setOverlayTextureUnit(unsigned int unit);
  73. /** Get the texture unit that the texture should be assigned to.*/
  74. unsigned int getOverlayTextureUnit() const { return _textureUnit; }
  75. /** Set the texture size hint. The size hint is used to request a texture of specified size.*/
  76. void setOverlayTextureSizeHint(unsigned int size);
  77. /** Get the texture size hint.*/
  78. unsigned int getOverlayTextureSizeHint() const { return _textureSizeHint; }
  79. /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
  80. virtual void setThreadSafeRefUnref(bool threadSafe);
  81. /** Resize any per context GLObject buffers to specified size. */
  82. virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/);
  83. /** If State is non-zero, this function releases any associated OpenGL objects for
  84. * the specified graphics context. Otherwise, releases OpenGL objexts
  85. * for all graphics contexts. */
  86. virtual void releaseGLObjects(osg::State* = 0) const;
  87. protected :
  88. virtual ~OverlayNode() {}
  89. void init();
  90. void init_OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY();
  91. void init_VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY();
  92. void init_VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY();
  93. void traverse_OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY(osg::NodeVisitor& nv);
  94. void traverse_VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY(osg::NodeVisitor& nv);
  95. void traverse_VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY(osg::NodeVisitor& nv);
  96. void updateMainSubgraphStateSet();
  97. typedef osg::buffered_value< int > TextureObjectValidList;
  98. mutable TextureObjectValidList _textureObjectValidList;
  99. OverlayTechnique _overlayTechnique;
  100. // overlay subgraph is render to a texture
  101. osg::ref_ptr<osg::Node> _overlaySubgraph;
  102. osg::ref_ptr<osg::StateSet> _overlayStateSet;
  103. osg::ref_ptr<osg::StateSet> _mainStateSet;
  104. // texture to render to, and to read from.
  105. GLenum _texEnvMode;
  106. unsigned int _textureUnit;
  107. unsigned int _textureSizeHint;
  108. osg::Vec4 _overlayClearColor;
  109. bool _continuousUpdate;
  110. double _overlayBaseHeight;
  111. bool _updateCamera;
  112. osg::Camera::RenderTargetImplementation _renderTargetImpl;
  113. struct OverlayData : public osg::Referenced
  114. {
  115. void setThreadSafeRefUnref(bool threadSafe);
  116. void resizeGLObjectBuffers(unsigned int maxSize);
  117. void releaseGLObjects(osg::State* state= 0) const;
  118. osg::ref_ptr<osg::Camera> _camera;
  119. osg::ref_ptr<osg::StateSet> _overlayStateSet;
  120. osg::ref_ptr<osg::StateSet> _mainSubgraphStateSet;
  121. osg::ref_ptr<osg::TexGenNode> _texgenNode;
  122. osg::ref_ptr<osg::Texture2D> _texture;
  123. osg::Polytope _textureFrustum;
  124. osg::ref_ptr<osg::Geode> _geode;
  125. osg::ref_ptr<osg::Program> _mainSubgraphProgram;
  126. osg::ref_ptr<osg::Uniform> _y0;
  127. osg::ref_ptr<osg::Uniform> _lightingEnabled;
  128. };
  129. typedef std::map<osgUtil::CullVisitor*, osg::ref_ptr<OverlayData> > OverlayDataMap;
  130. OpenThreads::Mutex _overlayDataMapMutex;
  131. OverlayDataMap _overlayDataMap;
  132. OverlayNode::OverlayData* getOverlayData(osgUtil::CullVisitor* cv);
  133. };
  134. }
  135. #endif