| 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 | |
|---|
| 14 | #ifndef OSGSIM_OVERLAYNODE |
|---|
| 15 | #define OSGSIM_OVERLAYNODE 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/buffered_value> |
|---|
| 18 | #include <osg/Camera> |
|---|
| 19 | #include <osg/Texture2D> |
|---|
| 20 | #include <osg/TexGenNode> |
|---|
| 21 | #include <osg/Geode> |
|---|
| 22 | |
|---|
| 23 | #include <osgUtil/CullVisitor> |
|---|
| 24 | |
|---|
| 25 | #include <osgSim/Export> |
|---|
| 26 | |
|---|
| 27 | namespace osgSim { |
|---|
| 28 | |
|---|
| 29 | /** OverlayNode is for creating texture overlays on scenes, with the overlay texture being generated |
|---|
| 30 | * by pre rendering an Overlay Subgraph to a texture, then projecting this resulting texture on the scene.*/ |
|---|
| 31 | class OSGSIM_EXPORT OverlayNode : public osg::Group |
|---|
| 32 | { |
|---|
| 33 | public : |
|---|
| 34 | |
|---|
| 35 | enum OverlayTechnique |
|---|
| 36 | { |
|---|
| 37 | OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY, |
|---|
| 38 | VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY, |
|---|
| 39 | VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY |
|---|
| 40 | }; |
|---|
| 41 | |
|---|
| 42 | OverlayNode(OverlayTechnique technique=OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY); |
|---|
| 43 | |
|---|
| 44 | OverlayNode(const OverlayNode& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); |
|---|
| 45 | |
|---|
| 46 | META_Node(osgSim, OverlayNode); |
|---|
| 47 | |
|---|
| 48 | virtual void traverse(osg::NodeVisitor& nv); |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | void setOverlayTechnique(OverlayTechnique technique); |
|---|
| 52 | OverlayTechnique getOverlayTechnique() const { return _overlayTechnique; } |
|---|
| 53 | |
|---|
| 54 | /** Set the implementation to be used when creating the overlay texture. */ |
|---|
| 55 | void setRenderTargetImplementation(osg::Camera::RenderTargetImplementation impl); |
|---|
| 56 | |
|---|
| 57 | /** Set the overlay subgraph which will be rendered to texture.*/ |
|---|
| 58 | void setOverlaySubgraph(osg::Node* node); |
|---|
| 59 | |
|---|
| 60 | /** Get the overlay subgraph which will be rendered to texture.*/ |
|---|
| 61 | osg::Node* getOverlaySubgraph() { return _overlaySubgraph.get(); } |
|---|
| 62 | |
|---|
| 63 | /** Get the const overlay subgraph which will be render to texture.*/ |
|---|
| 64 | const osg::Node* getOverlaySubgraph() const { return _overlaySubgraph.get(); } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | /** Inform the OverlayNode that the overlay texture needs to be updated.*/ |
|---|
| 68 | void dirtyOverlayTexture(); |
|---|
| 69 | |
|---|
| 70 | /** Set whether the OverlayNode should update the overlay texture on every frame.*/ |
|---|
| 71 | void setContinuousUpdate(bool update) { _continuousUpdate = update; } |
|---|
| 72 | |
|---|
| 73 | /** Get whether the OverlayNode should update the overlay texture on every frame.*/ |
|---|
| 74 | bool getContinuousUpdate() const { return _continuousUpdate; } |
|---|
| 75 | |
|---|
| 76 | /** Set the base height that the overlay subgraph will be projected down to. |
|---|
| 77 | * Normally you'll set this to just below ground level, if you set it too high |
|---|
| 78 | * then the overlay texture can end up being clipped in certain viewing directions, |
|---|
| 79 | * while if its too low then there will be a limit to how close you can get to the |
|---|
| 80 | * terrain before pixaltion becomes an issue.*/ |
|---|
| 81 | void setOverlayBaseHeight(double baseHeight) { _overlayBaseHeight = baseHeight; } |
|---|
| 82 | |
|---|
| 83 | /** Get the base height that the overlay subgraph will be projected down to.*/ |
|---|
| 84 | double getOverlayBaseHeight() const { return _overlayBaseHeight; } |
|---|
| 85 | |
|---|
| 86 | /** Set the clear color to use when rendering the overlay subgraph.*/ |
|---|
| 87 | void setOverlayClearColor(const osg::Vec4& color) { _overlayClearColor = color; } |
|---|
| 88 | |
|---|
| 89 | /** Get the clear color to use when rendering the overlay subgraph.*/ |
|---|
| 90 | const osg::Vec4& getOverlayClearColor() const { return _overlayClearColor; } |
|---|
| 91 | |
|---|
| 92 | /** Set the TexEnv mode used to combine the overlay texture with the base color/texture of the OverlayNode's decorate subgraph.*/ |
|---|
| 93 | void setTexEnvMode(GLenum mode); |
|---|
| 94 | |
|---|
| 95 | /** Get the TexEnv mode used to combine the overlay texture with the base color/texture of the OverlayNode's decorate subgraph.*/ |
|---|
| 96 | GLenum getTexEnvMode() const { return _texEnvMode; } |
|---|
| 97 | |
|---|
| 98 | /** Set the texture unit that the texture should be assigned to.*/ |
|---|
| 99 | void setOverlayTextureUnit(unsigned int unit); |
|---|
| 100 | |
|---|
| 101 | /** Get the texture unit that the texture should be assigned to.*/ |
|---|
| 102 | unsigned int getOverlayTextureUnit() const { return _textureUnit; } |
|---|
| 103 | |
|---|
| 104 | /** Set the texture size hint. The size hint is used to request a texture of specified size.*/ |
|---|
| 105 | void setOverlayTextureSizeHint(unsigned int size); |
|---|
| 106 | |
|---|
| 107 | /** Get the texture size hint.*/ |
|---|
| 108 | unsigned int getOverlayTextureSizeHint() const { return _textureSizeHint; } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/ |
|---|
| 112 | virtual void setThreadSafeRefUnref(bool threadSafe); |
|---|
| 113 | |
|---|
| 114 | /** Resize any per context GLObject buffers to specified size. */ |
|---|
| 115 | virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/); |
|---|
| 116 | |
|---|
| 117 | /** If State is non-zero, this function releases any associated OpenGL objects for |
|---|
| 118 | * the specified graphics context. Otherwise, releases OpenGL objexts |
|---|
| 119 | * for all graphics contexts. */ |
|---|
| 120 | virtual void releaseGLObjects(osg::State* = 0) const; |
|---|
| 121 | |
|---|
| 122 | protected : |
|---|
| 123 | |
|---|
| 124 | virtual ~OverlayNode() {} |
|---|
| 125 | |
|---|
| 126 | void init(); |
|---|
| 127 | void init_OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY(); |
|---|
| 128 | void init_VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY(); |
|---|
| 129 | void init_VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY(); |
|---|
| 130 | |
|---|
| 131 | void traverse_OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY(osg::NodeVisitor& nv); |
|---|
| 132 | void traverse_VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY(osg::NodeVisitor& nv); |
|---|
| 133 | void traverse_VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY(osg::NodeVisitor& nv); |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | void updateMainSubgraphStateSet(); |
|---|
| 137 | |
|---|
| 138 | typedef osg::buffered_value< int > TextureObjectValidList; |
|---|
| 139 | |
|---|
| 140 | mutable TextureObjectValidList _textureObjectValidList; |
|---|
| 141 | |
|---|
| 142 | OverlayTechnique _overlayTechnique; |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | // overlay subgraph is render to a texture |
|---|
| 146 | osg::ref_ptr<osg::Node> _overlaySubgraph; |
|---|
| 147 | |
|---|
| 148 | osg::ref_ptr<osg::StateSet> _overlayStateSet; |
|---|
| 149 | osg::ref_ptr<osg::StateSet> _mainStateSet; |
|---|
| 150 | |
|---|
| 151 | // texture to render to, and to read from. |
|---|
| 152 | GLenum _texEnvMode; |
|---|
| 153 | unsigned int _textureUnit; |
|---|
| 154 | unsigned int _textureSizeHint; |
|---|
| 155 | osg::Vec4 _overlayClearColor; |
|---|
| 156 | |
|---|
| 157 | bool _continuousUpdate; |
|---|
| 158 | double _overlayBaseHeight; |
|---|
| 159 | bool _updateCamera; |
|---|
| 160 | |
|---|
| 161 | osg::Camera::RenderTargetImplementation _renderTargetImpl; |
|---|
| 162 | |
|---|
| 163 | struct OverlayData : public osg::Referenced |
|---|
| 164 | { |
|---|
| 165 | |
|---|
| 166 | void setThreadSafeRefUnref(bool threadSafe); |
|---|
| 167 | void resizeGLObjectBuffers(unsigned int maxSize); |
|---|
| 168 | void releaseGLObjects(osg::State* state= 0) const; |
|---|
| 169 | |
|---|
| 170 | osg::ref_ptr<osg::Camera> _camera; |
|---|
| 171 | osg::ref_ptr<osg::StateSet> _overlayStateSet; |
|---|
| 172 | osg::ref_ptr<osg::StateSet> _mainSubgraphStateSet; |
|---|
| 173 | osg::ref_ptr<osg::TexGenNode> _texgenNode; |
|---|
| 174 | osg::ref_ptr<osg::Texture2D> _texture; |
|---|
| 175 | osg::Polytope _textureFrustum; |
|---|
| 176 | osg::ref_ptr<osg::Geode> _geode; |
|---|
| 177 | |
|---|
| 178 | osg::ref_ptr<osg::Program> _mainSubgraphProgram; |
|---|
| 179 | |
|---|
| 180 | osg::ref_ptr<osg::Uniform> _y0; |
|---|
| 181 | osg::ref_ptr<osg::Uniform> _lightingEnabled; |
|---|
| 182 | }; |
|---|
| 183 | |
|---|
| 184 | typedef std::map<osgUtil::CullVisitor*, osg::ref_ptr<OverlayData> > OverlayDataMap; |
|---|
| 185 | |
|---|
| 186 | OpenThreads::Mutex _overlayDataMapMutex; |
|---|
| 187 | OverlayDataMap _overlayDataMap; |
|---|
| 188 | |
|---|
| 189 | OverlayNode::OverlayData* getOverlayData(osgUtil::CullVisitor* cv); |
|---|
| 190 | |
|---|
| 191 | }; |
|---|
| 192 | |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | #endif |
|---|