2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2005-08-26 22:00:02 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2005-08-26 22:00:02 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2005-08-26 22:00:02 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2005-08-26 22:00:02 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSGSIM_OVERLAYNODE
|
|
|
|
#define OSGSIM_OVERLAYNODE 1
|
|
|
|
|
|
|
|
#include <osg/buffered_value>
|
2006-11-27 22:52:07 +08:00
|
|
|
#include <osg/Camera>
|
2005-09-08 00:18:43 +08:00
|
|
|
#include <osg/Texture2D>
|
2005-08-26 22:00:02 +08:00
|
|
|
#include <osg/TexGenNode>
|
2007-05-16 22:22:10 +08:00
|
|
|
#include <osg/Geode>
|
2005-08-26 22:00:02 +08:00
|
|
|
|
2007-05-15 19:22:25 +08:00
|
|
|
#include <osgUtil/CullVisitor>
|
|
|
|
|
2005-08-26 22:00:02 +08:00
|
|
|
#include <osgSim/Export>
|
|
|
|
|
|
|
|
namespace osgSim {
|
|
|
|
|
|
|
|
/** OverlayNode is for creating texture overlays on scenes, with the overlay texture being generated
|
|
|
|
* by pre rendering an Overlay Subgraph to a texture, then projecting this resulting texture on the scene.*/
|
|
|
|
class OSGSIM_EXPORT OverlayNode : public osg::Group
|
|
|
|
{
|
|
|
|
public :
|
2007-05-15 04:23:10 +08:00
|
|
|
|
|
|
|
enum OverlayTechnique
|
|
|
|
{
|
|
|
|
OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY,
|
|
|
|
VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY,
|
2007-09-07 23:03:56 +08:00
|
|
|
VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY
|
2007-05-15 04:23:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
OverlayNode(OverlayTechnique technique=OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY);
|
2005-08-26 22:00:02 +08:00
|
|
|
|
|
|
|
OverlayNode(const OverlayNode& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-08-26 22:00:02 +08:00
|
|
|
META_Node(osgSim, OverlayNode);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-08-26 22:00:02 +08:00
|
|
|
virtual void traverse(osg::NodeVisitor& nv);
|
|
|
|
|
2007-05-15 04:23:10 +08:00
|
|
|
|
|
|
|
void setOverlayTechnique(OverlayTechnique technique);
|
2007-05-21 19:49:37 +08:00
|
|
|
OverlayTechnique getOverlayTechnique() const { return _overlayTechnique; }
|
2007-05-15 04:23:10 +08:00
|
|
|
|
2007-08-13 18:17:18 +08:00
|
|
|
/** Set the implementation to be used when creating the overlay texture. */
|
|
|
|
void setRenderTargetImplementation(osg::Camera::RenderTargetImplementation impl);
|
2007-05-15 04:23:10 +08:00
|
|
|
|
2005-08-31 23:04:27 +08:00
|
|
|
/** Set the overlay subgraph which will be rendered to texture.*/
|
2005-08-26 22:00:02 +08:00
|
|
|
void setOverlaySubgraph(osg::Node* node);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
template<class T> void setOverlaySubgraph(const osg::ref_ptr<T>& node) { setOverlaySubgraph(node.get()); }
|
|
|
|
|
2005-08-31 23:04:27 +08:00
|
|
|
/** Get the overlay subgraph which will be rendered to texture.*/
|
2005-08-26 22:00:02 +08:00
|
|
|
osg::Node* getOverlaySubgraph() { return _overlaySubgraph.get(); }
|
|
|
|
|
|
|
|
/** Get the const overlay subgraph which will be render to texture.*/
|
|
|
|
const osg::Node* getOverlaySubgraph() const { return _overlaySubgraph.get(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-09-29 18:22:06 +08:00
|
|
|
|
2005-09-07 03:54:29 +08:00
|
|
|
/** Inform the OverlayNode that the overlay texture needs to be updated.*/
|
2005-08-26 22:00:02 +08:00
|
|
|
void dirtyOverlayTexture();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-09-07 03:54:29 +08:00
|
|
|
/** Set whether the OverlayNode should update the overlay texture on every frame.*/
|
2006-02-21 05:05:23 +08:00
|
|
|
void setContinuousUpdate(bool update) { _continuousUpdate = update; }
|
2005-09-07 03:54:29 +08:00
|
|
|
|
|
|
|
/** Get whether the OverlayNode should update the overlay texture on every frame.*/
|
2006-02-21 05:05:23 +08:00
|
|
|
bool getContinuousUpdate() const { return _continuousUpdate; }
|
2005-09-07 03:54:29 +08:00
|
|
|
|
2007-05-26 00:00:38 +08:00
|
|
|
/** Set the base height that the overlay subgraph will be projected down to.
|
|
|
|
* Normally you'll set this to just below ground level, if you set it too high
|
|
|
|
* then the overlay texture can end up being clipped in certain viewing directions,
|
|
|
|
* while if its too low then there will be a limit to how close you can get to the
|
|
|
|
* terrain before pixaltion becomes an issue.*/
|
|
|
|
void setOverlayBaseHeight(double baseHeight) { _overlayBaseHeight = baseHeight; }
|
|
|
|
|
|
|
|
/** Get the base height that the overlay subgraph will be projected down to.*/
|
|
|
|
double getOverlayBaseHeight() const { return _overlayBaseHeight; }
|
2005-09-29 18:22:06 +08:00
|
|
|
|
|
|
|
/** Set the clear color to use when rendering the overlay subgraph.*/
|
2009-01-07 18:32:59 +08:00
|
|
|
void setOverlayClearColor(const osg::Vec4& color) { _overlayClearColor = color; }
|
2005-09-29 18:22:06 +08:00
|
|
|
|
|
|
|
/** Get the clear color to use when rendering the overlay subgraph.*/
|
2007-05-16 22:22:10 +08:00
|
|
|
const osg::Vec4& getOverlayClearColor() const { return _overlayClearColor; }
|
2005-09-29 18:22:06 +08:00
|
|
|
|
|
|
|
/** Set the TexEnv mode used to combine the overlay texture with the base color/texture of the OverlayNode's decorate subgraph.*/
|
|
|
|
void setTexEnvMode(GLenum mode);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-09-29 18:22:06 +08:00
|
|
|
/** Get the TexEnv mode used to combine the overlay texture with the base color/texture of the OverlayNode's decorate subgraph.*/
|
2006-02-21 05:05:23 +08:00
|
|
|
GLenum getTexEnvMode() const { return _texEnvMode; }
|
2005-09-29 18:22:06 +08:00
|
|
|
|
2005-08-26 22:00:02 +08:00
|
|
|
/** Set the texture unit that the texture should be assigned to.*/
|
|
|
|
void setOverlayTextureUnit(unsigned int unit);
|
|
|
|
|
|
|
|
/** Get the texture unit that the texture should be assigned to.*/
|
|
|
|
unsigned int getOverlayTextureUnit() const { return _textureUnit; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-09-06 23:48:18 +08:00
|
|
|
/** Set the texture size hint. The size hint is used to request a texture of specified size.*/
|
|
|
|
void setOverlayTextureSizeHint(unsigned int size);
|
|
|
|
|
|
|
|
/** Get the texture size hint.*/
|
|
|
|
unsigned int getOverlayTextureSizeHint() const { return _textureSizeHint; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
|
2007-01-11 18:21:11 +08:00
|
|
|
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
|
|
|
|
virtual void setThreadSafeRefUnref(bool threadSafe);
|
|
|
|
|
|
|
|
/** Resize any per context GLObject buffers to specified size. */
|
|
|
|
virtual void resizeGLObjectBuffers(unsigned int /*maxSize*/);
|
|
|
|
|
|
|
|
/** If State is non-zero, this function releases any associated OpenGL objects for
|
|
|
|
* the specified graphics context. Otherwise, releases OpenGL objexts
|
|
|
|
* for all graphics contexts. */
|
|
|
|
virtual void releaseGLObjects(osg::State* = 0) const;
|
|
|
|
|
2005-08-26 22:00:02 +08:00
|
|
|
protected :
|
|
|
|
|
|
|
|
virtual ~OverlayNode() {}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-08-27 04:01:21 +08:00
|
|
|
void init();
|
2007-05-15 04:23:10 +08:00
|
|
|
void init_OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY();
|
|
|
|
void init_VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY();
|
|
|
|
void init_VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-05-15 04:23:10 +08:00
|
|
|
void traverse_OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY(osg::NodeVisitor& nv);
|
|
|
|
void traverse_VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY(osg::NodeVisitor& nv);
|
|
|
|
void traverse_VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY(osg::NodeVisitor& nv);
|
|
|
|
|
|
|
|
|
2005-09-29 18:22:06 +08:00
|
|
|
void updateMainSubgraphStateSet();
|
2005-08-27 04:01:21 +08:00
|
|
|
|
|
|
|
typedef osg::buffered_value< int > TextureObjectValidList;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-08-27 04:01:21 +08:00
|
|
|
mutable TextureObjectValidList _textureObjectValidList;
|
2005-08-26 22:00:02 +08:00
|
|
|
|
2007-05-15 04:23:10 +08:00
|
|
|
OverlayTechnique _overlayTechnique;
|
|
|
|
|
2007-06-02 03:44:09 +08:00
|
|
|
|
2006-02-21 05:05:23 +08:00
|
|
|
// overlay subgraph is render to a texture
|
2005-08-26 22:00:02 +08:00
|
|
|
osg::ref_ptr<osg::Node> _overlaySubgraph;
|
2007-06-02 03:44:09 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::StateSet> _overlayStateSet;
|
|
|
|
osg::ref_ptr<osg::StateSet> _mainStateSet;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-08-26 22:00:02 +08:00
|
|
|
// texture to render to, and to read from.
|
2005-09-29 18:22:06 +08:00
|
|
|
GLenum _texEnvMode;
|
2005-08-26 22:00:02 +08:00
|
|
|
unsigned int _textureUnit;
|
2005-09-06 23:48:18 +08:00
|
|
|
unsigned int _textureSizeHint;
|
2007-05-16 22:22:10 +08:00
|
|
|
osg::Vec4 _overlayClearColor;
|
2005-08-26 22:00:02 +08:00
|
|
|
|
2006-02-21 05:05:23 +08:00
|
|
|
bool _continuousUpdate;
|
2007-05-26 00:00:38 +08:00
|
|
|
double _overlayBaseHeight;
|
2006-10-30 20:19:41 +08:00
|
|
|
bool _updateCamera;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-08-13 18:17:18 +08:00
|
|
|
osg::Camera::RenderTargetImplementation _renderTargetImpl;
|
2007-05-15 19:22:25 +08:00
|
|
|
|
2008-07-24 06:19:29 +08:00
|
|
|
struct OverlayData : public osg::Referenced
|
2007-05-15 19:22:25 +08:00
|
|
|
{
|
2007-05-16 22:22:10 +08:00
|
|
|
|
|
|
|
void setThreadSafeRefUnref(bool threadSafe);
|
|
|
|
void resizeGLObjectBuffers(unsigned int maxSize);
|
|
|
|
void releaseGLObjects(osg::State* state= 0) const;
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Camera> _camera;
|
|
|
|
osg::ref_ptr<osg::StateSet> _overlayStateSet;
|
|
|
|
osg::ref_ptr<osg::StateSet> _mainSubgraphStateSet;
|
|
|
|
osg::ref_ptr<osg::TexGenNode> _texgenNode;
|
|
|
|
osg::ref_ptr<osg::Texture2D> _texture;
|
|
|
|
osg::Polytope _textureFrustum;
|
|
|
|
osg::ref_ptr<osg::Geode> _geode;
|
2007-06-02 03:44:09 +08:00
|
|
|
|
2007-06-15 05:03:38 +08:00
|
|
|
osg::ref_ptr<osg::Program> _mainSubgraphProgram;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-06-15 05:03:38 +08:00
|
|
|
osg::ref_ptr<osg::Uniform> _y0;
|
|
|
|
osg::ref_ptr<osg::Uniform> _lightingEnabled;
|
2007-05-15 19:22:25 +08:00
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-07-24 06:19:29 +08:00
|
|
|
typedef std::map<osgUtil::CullVisitor*, osg::ref_ptr<OverlayData> > OverlayDataMap;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-05-15 19:22:25 +08:00
|
|
|
OpenThreads::Mutex _overlayDataMapMutex;
|
|
|
|
OverlayDataMap _overlayDataMap;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-07-24 06:19:29 +08:00
|
|
|
OverlayNode::OverlayData* getOverlayData(osgUtil::CullVisitor* cv);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-08-26 22:00:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|