1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
14#ifndef OSGSIM_OVERLAYNODE
15#define OSGSIM_OVERLAYNODE 1
17#include <osg/buffered_value>
19#include <osg/Texture2D>
20#include <osg/TexGenNode>
23#include <osgUtil/CullVisitor>
25#include <osgSim/Export>
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.*/
31class OSGSIM_EXPORT OverlayNode : public osg::Group
37 OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY,
38 VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY,
39 VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY
42 OverlayNode(OverlayTechnique technique=OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY);
44 OverlayNode(const OverlayNode& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
46 META_Node(osgSim, OverlayNode);
48 virtual void traverse(osg::NodeVisitor& nv);
51 void setOverlayTechnique(OverlayTechnique technique);
52 OverlayTechnique getOverlayTechnique() const { return _overlayTechnique; }
54 /** Set the implementation to be used when creating the overlay texture. */
55 void setRenderTargetImplementation(osg::Camera::RenderTargetImplementation impl);
57 /** Set the overlay subgraph which will be rendered to texture.*/
58 void setOverlaySubgraph(osg::Node* node);
60 template<class T> void setOverlaySubgraph(const osg::ref_ptr<T>& node) { setOverlaySubgraph(node.get()); }
62 /** Get the overlay subgraph which will be rendered to texture.*/
63 osg::Node* getOverlaySubgraph() { return _overlaySubgraph.get(); }
65 /** Get the const overlay subgraph which will be render to texture.*/
66 const osg::Node* getOverlaySubgraph() const { return _overlaySubgraph.get(); }
69 /** Inform the OverlayNode that the overlay texture needs to be updated.*/
70 void dirtyOverlayTexture();
72 /** Set whether the OverlayNode should update the overlay texture on every frame.*/
73 void setContinuousUpdate(bool update) { _continuousUpdate = update; }
75 /** Get whether the OverlayNode should update the overlay texture on every frame.*/
76 bool getContinuousUpdate() const { return _continuousUpdate; }
78 /** Set the base height that the overlay subgraph will be projected down to.
79 * Normally you'll set this to just below ground level, if you set it too high
80 * then the overlay texture can end up being clipped in certain viewing directions,
81 * while if its too low then there will be a limit to how close you can get to the
82 * terrain before pixaltion becomes an issue.*/
83 void setOverlayBaseHeight(double baseHeight) { _overlayBaseHeight = baseHeight; }
85 /** Get the base height that the overlay subgraph will be projected down to.*/
86 double getOverlayBaseHeight() const { return _overlayBaseHeight; }
88 /** Set the clear color to use when rendering the overlay subgraph.*/
89 void setOverlayClearColor(const osg::Vec4& color) { _overlayClearColor = color; }
91 /** Get the clear color to use when rendering the overlay subgraph.*/
92 const osg::Vec4& getOverlayClearColor() const { return _overlayClearColor; }
94 /** Set the TexEnv mode used to combine the overlay texture with the base color/texture of the OverlayNode's decorate subgraph.*/
95 void setTexEnvMode(GLenum mode);
97 /** Get the TexEnv mode used to combine the overlay texture with the base color/texture of the OverlayNode's decorate subgraph.*/
98 GLenum getTexEnvMode() const { return _texEnvMode; }
100 /** Set the texture unit that the texture should be assigned to.*/
101 void setOverlayTextureUnit(unsigned int unit);
103 /** Get the texture unit that the texture should be assigned to.*/
104 unsigned int getOverlayTextureUnit() const { return _textureUnit; }
106 /** Set the texture size hint. The size hint is used to request a texture of specified size.*/
107 void setOverlayTextureSizeHint(unsigned int size);
109 /** Get the texture size hint.*/
110 unsigned int getOverlayTextureSizeHint() const { return _textureSizeHint; }
113 /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
114 virtual void setThreadSafeRefUnref(bool threadSafe);
116 /** Resize any per context GLObject buffers to specified size. */
117 virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/);
119 /** If State is non-zero, this function releases any associated OpenGL objects for
120 * the specified graphics context. Otherwise, releases OpenGL objexts
121 * for all graphics contexts. */
122 virtual void releaseGLObjects(osg::State* = 0) const;
126 virtual ~OverlayNode() {}
129 void init_OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY();
130 void init_VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY();
131 void init_VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY();
133 void traverse_OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY(osg::NodeVisitor& nv);
134 void traverse_VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY(osg::NodeVisitor& nv);
135 void traverse_VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY(osg::NodeVisitor& nv);
138 void updateMainSubgraphStateSet();
140 typedef osg::buffered_value< int > TextureObjectValidList;
142 mutable TextureObjectValidList _textureObjectValidList;
144 OverlayTechnique _overlayTechnique;
147 // overlay subgraph is render to a texture
148 osg::ref_ptr<osg::Node> _overlaySubgraph;
150 osg::ref_ptr<osg::StateSet> _overlayStateSet;
151 osg::ref_ptr<osg::StateSet> _mainStateSet;
153 // texture to render to, and to read from.
155 unsigned int _textureUnit;
156 unsigned int _textureSizeHint;
157 osg::Vec4 _overlayClearColor;
159 bool _continuousUpdate;
160 double _overlayBaseHeight;
163 osg::Camera::RenderTargetImplementation _renderTargetImpl;
165 struct OverlayData : public osg::Referenced
168 void setThreadSafeRefUnref(bool threadSafe);
169 void resizeGLObjectBuffers(unsigned int maxSize);
170 void releaseGLObjects(osg::State* state= 0) const;
172 osg::ref_ptr<osg::Camera> _camera;
173 osg::ref_ptr<osg::StateSet> _overlayStateSet;
174 osg::ref_ptr<osg::StateSet> _mainSubgraphStateSet;
175 osg::ref_ptr<osg::TexGenNode> _texgenNode;
176 osg::ref_ptr<osg::Texture2D> _texture;
177 osg::Polytope _textureFrustum;
178 osg::ref_ptr<osg::Geode> _geode;
180 osg::ref_ptr<osg::Program> _mainSubgraphProgram;
182 osg::ref_ptr<osg::Uniform> _y0;
183 osg::ref_ptr<osg::Uniform> _lightingEnabled;
186 typedef std::map<osgUtil::CullVisitor*, osg::ref_ptr<OverlayData> > OverlayDataMap;
188 OpenThreads::Mutex _overlayDataMapMutex;
189 OverlayDataMap _overlayDataMap;
191 OverlayNode::OverlayData* getOverlayData(osgUtil::CullVisitor* cv);