2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +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
|
2003-01-22 00:45:36 +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
|
|
|
*
|
2003-01-22 00:45:36 +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
|
2003-01-22 00:45:36 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2001-10-19 22:22:02 +08:00
|
|
|
|
|
|
|
#ifndef OSGUTIL_OPTIMIZER
|
|
|
|
#define OSGUTIL_OPTIMIZER
|
|
|
|
|
|
|
|
#include <osg/NodeVisitor>
|
|
|
|
#include <osg/Matrix>
|
2002-07-18 08:53:03 +08:00
|
|
|
#include <osg/Geometry>
|
2002-08-30 23:07:10 +08:00
|
|
|
#include <osg/Transform>
|
2006-08-25 23:53:16 +08:00
|
|
|
#include <osg/Texture2D>
|
2001-10-19 22:22:02 +08:00
|
|
|
|
|
|
|
#include <osgUtil/Export>
|
|
|
|
|
2002-02-07 09:15:15 +08:00
|
|
|
#include <set>
|
|
|
|
|
2001-10-19 22:22:02 +08:00
|
|
|
namespace osgUtil {
|
|
|
|
|
2005-05-13 21:29:45 +08:00
|
|
|
// forward declare
|
|
|
|
class Optimizer;
|
|
|
|
|
|
|
|
/** Helper base class for implementing Optimizer techniques.*/
|
|
|
|
class OSGUTIL_EXPORT BaseOptimizerVisitor : public osg::NodeVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
BaseOptimizerVisitor(Optimizer* optimizer, unsigned int operation):
|
|
|
|
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
|
|
|
|
_optimizer(optimizer),
|
2006-08-10 14:43:09 +08:00
|
|
|
_operationType(operation)
|
|
|
|
{
|
|
|
|
setNodeMaskOverride(0xffffffff);
|
|
|
|
}
|
2005-05-13 21:29:45 +08:00
|
|
|
|
|
|
|
inline bool isOperationPermissibleForObject(const osg::StateSet* object) const;
|
|
|
|
inline bool isOperationPermissibleForObject(const osg::StateAttribute* object) const;
|
2012-03-22 01:36:20 +08:00
|
|
|
inline bool isOperationPermissibleForObject(const osg::Drawable* object) const;
|
2005-05-13 21:29:45 +08:00
|
|
|
inline bool isOperationPermissibleForObject(const osg::Node* object) const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
Optimizer* _optimizer;
|
|
|
|
unsigned int _operationType;
|
|
|
|
};
|
|
|
|
|
2004-09-27 02:39:34 +08:00
|
|
|
/** Traverses scene graph to improve efficiency. See OptimizationOptions.
|
|
|
|
* For example of usage see examples/osgimpostor or osgviewer.
|
2001-10-19 22:22:02 +08:00
|
|
|
*/
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2001-10-19 22:22:02 +08:00
|
|
|
class OSGUTIL_EXPORT Optimizer
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Optimizer() {}
|
2003-12-12 07:26:22 +08:00
|
|
|
virtual ~Optimizer() {}
|
2001-10-19 22:22:02 +08:00
|
|
|
|
|
|
|
enum OptimizationOptions
|
|
|
|
{
|
2008-08-04 00:57:09 +08:00
|
|
|
FLATTEN_STATIC_TRANSFORMS = (1 << 0),
|
|
|
|
REMOVE_REDUNDANT_NODES = (1 << 1),
|
|
|
|
REMOVE_LOADED_PROXY_NODES = (1 << 2),
|
|
|
|
COMBINE_ADJACENT_LODS = (1 << 3),
|
|
|
|
SHARE_DUPLICATE_STATE = (1 << 4),
|
|
|
|
MERGE_GEOMETRY = (1 << 5),
|
|
|
|
CHECK_GEOMETRY = (1 << 6),
|
2009-03-10 22:15:59 +08:00
|
|
|
MAKE_FAST_GEOMETRY = (1 << 7),
|
|
|
|
SPATIALIZE_GROUPS = (1 << 8),
|
|
|
|
COPY_SHARED_NODES = (1 << 9),
|
|
|
|
TRISTRIP_GEOMETRY = (1 << 10),
|
|
|
|
TESSELLATE_GEOMETRY = (1 << 11),
|
|
|
|
OPTIMIZE_TEXTURE_SETTINGS = (1 << 12),
|
|
|
|
MERGE_GEODES = (1 << 13),
|
|
|
|
FLATTEN_BILLBOARDS = (1 << 14),
|
|
|
|
TEXTURE_ATLAS_BUILDER = (1 << 15),
|
|
|
|
STATIC_OBJECT_DETECTION = (1 << 16),
|
|
|
|
FLATTEN_STATIC_TRANSFORMS_DUPLICATING_SHARED_SUBGRAPHS = (1 << 17),
|
2010-03-12 02:15:20 +08:00
|
|
|
INDEX_MESH = (1 << 18),
|
|
|
|
VERTEX_POSTTRANSFORM = (1 << 19),
|
|
|
|
VERTEX_PRETRANSFORM = (1 << 20),
|
2016-07-11 16:52:07 +08:00
|
|
|
BUFFER_OBJECT_SETTINGS = (1 << 21),
|
2003-12-04 05:45:32 +08:00
|
|
|
DEFAULT_OPTIMIZATIONS = FLATTEN_STATIC_TRANSFORMS |
|
|
|
|
REMOVE_REDUNDANT_NODES |
|
2005-04-30 23:16:05 +08:00
|
|
|
REMOVE_LOADED_PROXY_NODES |
|
2003-12-04 05:45:32 +08:00
|
|
|
COMBINE_ADJACENT_LODS |
|
|
|
|
SHARE_DUPLICATE_STATE |
|
|
|
|
MERGE_GEOMETRY |
|
2009-03-10 22:15:59 +08:00
|
|
|
MAKE_FAST_GEOMETRY |
|
2004-07-12 21:20:18 +08:00
|
|
|
CHECK_GEOMETRY |
|
2007-01-30 06:44:29 +08:00
|
|
|
OPTIMIZE_TEXTURE_SETTINGS |
|
|
|
|
STATIC_OBJECT_DETECTION,
|
2008-06-20 21:16:35 +08:00
|
|
|
ALL_OPTIMIZATIONS = FLATTEN_STATIC_TRANSFORMS_DUPLICATING_SHARED_SUBGRAPHS |
|
2002-12-07 14:59:08 +08:00
|
|
|
REMOVE_REDUNDANT_NODES |
|
2005-04-30 23:16:05 +08:00
|
|
|
REMOVE_LOADED_PROXY_NODES |
|
2001-10-19 22:22:02 +08:00
|
|
|
COMBINE_ADJACENT_LODS |
|
2003-05-22 23:29:20 +08:00
|
|
|
SHARE_DUPLICATE_STATE |
|
2006-04-06 22:06:22 +08:00
|
|
|
MERGE_GEODES |
|
2003-09-24 23:54:22 +08:00
|
|
|
MERGE_GEOMETRY |
|
2009-03-10 22:15:59 +08:00
|
|
|
MAKE_FAST_GEOMETRY |
|
2003-12-01 18:28:23 +08:00
|
|
|
CHECK_GEOMETRY |
|
2003-12-04 05:45:32 +08:00
|
|
|
SPATIALIZE_GROUPS |
|
|
|
|
COPY_SHARED_NODES |
|
2004-07-12 21:20:18 +08:00
|
|
|
TRISTRIP_GEOMETRY |
|
2006-08-28 18:46:39 +08:00
|
|
|
OPTIMIZE_TEXTURE_SETTINGS |
|
2007-01-30 06:44:29 +08:00
|
|
|
TEXTURE_ATLAS_BUILDER |
|
2016-07-11 16:52:07 +08:00
|
|
|
STATIC_OBJECT_DETECTION |
|
|
|
|
BUFFER_OBJECT_SETTINGS
|
2001-10-19 22:22:02 +08:00
|
|
|
};
|
|
|
|
|
2004-09-27 02:39:34 +08:00
|
|
|
/** Reset internal data to initial state - the getPermissibleOptionsMap is cleared.*/
|
2003-12-12 07:26:22 +08:00
|
|
|
void reset();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-09-27 02:39:34 +08:00
|
|
|
/** Traverse the node and its subgraph with a series of optimization
|
|
|
|
* visitors, specified by the OptimizationOptions.*/
|
2003-12-04 05:45:32 +08:00
|
|
|
void optimize(osg::Node* node);
|
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
template<class T> void optimize(const osg::ref_ptr<T>& node) { optimize(node.get()); }
|
|
|
|
|
2004-09-27 02:39:34 +08:00
|
|
|
/** Traverse the node and its subgraph with a series of optimization
|
|
|
|
* visitors, specified by the OptimizationOptions.*/
|
2003-12-04 05:45:32 +08:00
|
|
|
virtual void optimize(osg::Node* node, unsigned int options);
|
2001-10-19 22:22:02 +08:00
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
template<class T> void optimize(const osg::ref_ptr<T>& node, unsigned int options) { optimize(node.get(), options); }
|
|
|
|
|
2001-10-19 22:22:02 +08:00
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** Callback for customizing what operations are permitted on objects in the scene graph.*/
|
2005-03-18 10:44:08 +08:00
|
|
|
struct IsOperationPermissibleForObjectCallback : public osg::Referenced
|
|
|
|
{
|
|
|
|
virtual bool isOperationPermissibleForObjectImplementation(const Optimizer* optimizer, const osg::StateSet* stateset,unsigned int option) const
|
|
|
|
{
|
|
|
|
return optimizer->isOperationPermissibleForObjectImplementation(stateset,option);
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-03-18 10:44:08 +08:00
|
|
|
virtual bool isOperationPermissibleForObjectImplementation(const Optimizer* optimizer, const osg::StateAttribute* attribute,unsigned int option) const
|
|
|
|
{
|
|
|
|
return optimizer->isOperationPermissibleForObjectImplementation(attribute,option);
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-03-18 10:44:08 +08:00
|
|
|
virtual bool isOperationPermissibleForObjectImplementation(const Optimizer* optimizer, const osg::Drawable* drawable,unsigned int option) const
|
|
|
|
{
|
|
|
|
return optimizer->isOperationPermissibleForObjectImplementation(drawable,option);
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-03-18 10:44:08 +08:00
|
|
|
virtual bool isOperationPermissibleForObjectImplementation(const Optimizer* optimizer, const osg::Node* node,unsigned int option) const
|
|
|
|
{
|
|
|
|
return optimizer->isOperationPermissibleForObjectImplementation(node,option);
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-03-18 10:44:08 +08:00
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
/** Set the callback for customizing what operations are permitted on objects in the scene graph.*/
|
2005-03-18 10:44:08 +08:00
|
|
|
void setIsOperationPermissibleForObjectCallback(IsOperationPermissibleForObjectCallback* callback) { _isOperationPermissibleForObjectCallback=callback; }
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** Get the callback for customizing what operations are permitted on objects in the scene graph.*/
|
2005-03-18 10:44:08 +08:00
|
|
|
IsOperationPermissibleForObjectCallback* getIsOperationPermissibleForObjectCallback() { return _isOperationPermissibleForObjectCallback.get(); }
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** Get the callback for customizing what operations are permitted on objects in the scene graph.*/
|
2005-03-18 10:44:08 +08:00
|
|
|
const IsOperationPermissibleForObjectCallback* getIsOperationPermissibleForObjectCallback() const { return _isOperationPermissibleForObjectCallback.get(); }
|
|
|
|
|
|
|
|
|
2004-09-27 02:39:34 +08:00
|
|
|
inline void setPermissibleOptimizationsForObject(const osg::Object* object, unsigned int options)
|
2003-12-12 07:26:22 +08:00
|
|
|
{
|
2004-09-27 02:39:34 +08:00
|
|
|
_permissibleOptimizationsMap[object] = options;
|
2003-12-12 07:26:22 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-09-27 02:39:34 +08:00
|
|
|
inline unsigned int getPermissibleOptimizationsForObject(const osg::Object* object) const
|
2003-12-12 07:26:22 +08:00
|
|
|
{
|
2004-09-27 02:39:34 +08:00
|
|
|
PermissibleOptimizationsMap::const_iterator itr = _permissibleOptimizationsMap.find(object);
|
|
|
|
if (itr!=_permissibleOptimizationsMap.end()) return itr->second;
|
2003-12-12 07:26:22 +08:00
|
|
|
else return 0xffffffff;
|
|
|
|
}
|
2005-03-18 10:44:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
inline bool isOperationPermissibleForObject(const osg::StateSet* object, unsigned int option) const
|
2003-12-12 07:26:22 +08:00
|
|
|
{
|
2005-03-18 10:44:08 +08:00
|
|
|
if (_isOperationPermissibleForObjectCallback.valid())
|
2005-03-24 04:34:02 +08:00
|
|
|
return _isOperationPermissibleForObjectCallback->isOperationPermissibleForObjectImplementation(this,object,option);
|
2012-03-22 01:36:20 +08:00
|
|
|
else
|
|
|
|
return isOperationPermissibleForObjectImplementation(object,option);
|
2003-12-12 07:26:22 +08:00
|
|
|
}
|
|
|
|
|
2005-03-18 10:44:08 +08:00
|
|
|
inline bool isOperationPermissibleForObject(const osg::StateAttribute* object, unsigned int option) const
|
|
|
|
{
|
|
|
|
if (_isOperationPermissibleForObjectCallback.valid())
|
2005-03-24 04:34:02 +08:00
|
|
|
return _isOperationPermissibleForObjectCallback->isOperationPermissibleForObjectImplementation(this,object,option);
|
2012-03-22 01:36:20 +08:00
|
|
|
else
|
|
|
|
return isOperationPermissibleForObjectImplementation(object,option);
|
2005-03-18 10:44:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isOperationPermissibleForObject(const osg::Drawable* object, unsigned int option) const
|
|
|
|
{
|
|
|
|
if (_isOperationPermissibleForObjectCallback.valid())
|
2005-03-24 04:34:02 +08:00
|
|
|
return _isOperationPermissibleForObjectCallback->isOperationPermissibleForObjectImplementation(this,object,option);
|
2012-03-22 01:36:20 +08:00
|
|
|
else
|
|
|
|
return isOperationPermissibleForObjectImplementation(object,option);
|
2005-03-18 10:44:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isOperationPermissibleForObject(const osg::Node* object, unsigned int option) const
|
|
|
|
{
|
|
|
|
if (_isOperationPermissibleForObjectCallback.valid())
|
2005-03-24 04:34:02 +08:00
|
|
|
return _isOperationPermissibleForObjectCallback->isOperationPermissibleForObjectImplementation(this,object,option);
|
2012-03-22 01:36:20 +08:00
|
|
|
else
|
|
|
|
return isOperationPermissibleForObjectImplementation(object,option);
|
2005-03-18 10:44:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isOperationPermissibleForObjectImplementation(const osg::StateSet* stateset, unsigned int option) const
|
|
|
|
{
|
|
|
|
return (option & getPermissibleOptimizationsForObject(stateset))!=0;
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-03-18 10:44:08 +08:00
|
|
|
bool isOperationPermissibleForObjectImplementation(const osg::StateAttribute* attribute, unsigned int option) const
|
|
|
|
{
|
|
|
|
return (option & getPermissibleOptimizationsForObject(attribute))!=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isOperationPermissibleForObjectImplementation(const osg::Drawable* drawable, unsigned int option) const
|
|
|
|
{
|
|
|
|
if (option & (REMOVE_REDUNDANT_NODES|MERGE_GEOMETRY))
|
|
|
|
{
|
|
|
|
if (drawable->getUserData()) return false;
|
|
|
|
if (drawable->getUpdateCallback()) return false;
|
|
|
|
if (drawable->getEventCallback()) return false;
|
|
|
|
if (drawable->getCullCallback()) return false;
|
|
|
|
}
|
|
|
|
return (option & getPermissibleOptimizationsForObject(drawable))!=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isOperationPermissibleForObjectImplementation(const osg::Node* node, unsigned int option) const
|
|
|
|
{
|
|
|
|
if (option & (REMOVE_REDUNDANT_NODES|COMBINE_ADJACENT_LODS|FLATTEN_STATIC_TRANSFORMS))
|
|
|
|
{
|
|
|
|
if (node->getUserData()) return false;
|
|
|
|
if (node->getUpdateCallback()) return false;
|
|
|
|
if (node->getEventCallback()) return false;
|
|
|
|
if (node->getCullCallback()) return false;
|
|
|
|
if (node->getNumDescriptions()>0) return false;
|
|
|
|
if (node->getStateSet()) return false;
|
|
|
|
if (node->getNodeMask()!=0xffffffff) return false;
|
2005-03-23 16:30:15 +08:00
|
|
|
// if (!node->getName().empty()) return false;
|
2005-03-18 10:44:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (option & getPermissibleOptimizationsForObject(node))!=0;
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-12-12 07:26:22 +08:00
|
|
|
protected:
|
|
|
|
|
2005-03-18 10:44:08 +08:00
|
|
|
osg::ref_ptr<IsOperationPermissibleForObjectCallback> _isOperationPermissibleForObjectCallback;
|
2003-12-12 07:26:22 +08:00
|
|
|
|
2005-03-18 10:44:08 +08:00
|
|
|
typedef std::map<const osg::Object*,unsigned int> PermissibleOptimizationsMap;
|
2012-03-22 01:36:20 +08:00
|
|
|
PermissibleOptimizationsMap _permissibleOptimizationsMap;
|
|
|
|
|
2003-12-12 07:26:22 +08:00
|
|
|
public:
|
2002-06-24 05:43:46 +08:00
|
|
|
|
2004-09-27 02:39:34 +08:00
|
|
|
/** Flatten Static Transform nodes by applying their transform to the
|
2012-03-22 01:36:20 +08:00
|
|
|
* geometry on the leaves of the scene graph, then removing the
|
2013-06-19 20:32:40 +08:00
|
|
|
* now redundant transforms. Static transformed subgraphs that have multiple
|
2008-06-20 21:16:35 +08:00
|
|
|
* parental paths above them are not flattened, if you require this then
|
2012-03-22 01:36:20 +08:00
|
|
|
* the subgraphs have to be duplicated - for this use the
|
2008-06-20 21:16:35 +08:00
|
|
|
* FlattenStaticTransformsDuplicatingSharedSubgraphsVisitor. */
|
2005-03-18 10:44:08 +08:00
|
|
|
class OSGUTIL_EXPORT FlattenStaticTransformsVisitor : public BaseOptimizerVisitor
|
2001-10-19 22:22:02 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2003-12-12 07:26:22 +08:00
|
|
|
FlattenStaticTransformsVisitor(Optimizer* optimizer=0):
|
2005-03-18 10:44:08 +08:00
|
|
|
BaseOptimizerVisitor(optimizer, FLATTEN_STATIC_TRANSFORMS) {}
|
2001-10-19 22:22:02 +08:00
|
|
|
|
2004-07-29 18:13:02 +08:00
|
|
|
virtual void apply(osg::Node& geode);
|
2017-01-21 05:43:58 +08:00
|
|
|
virtual void apply(osg::Drawable& drawable);
|
2002-08-30 23:07:10 +08:00
|
|
|
virtual void apply(osg::Billboard& geode);
|
2005-05-09 23:29:18 +08:00
|
|
|
virtual void apply(osg::ProxyNode& node);
|
2007-02-08 23:47:05 +08:00
|
|
|
virtual void apply(osg::PagedLOD& node);
|
2002-06-24 05:43:46 +08:00
|
|
|
virtual void apply(osg::Transform& transform);
|
|
|
|
|
2003-01-22 20:06:22 +08:00
|
|
|
bool removeTransforms(osg::Node* nodeWeCannotRemove);
|
2002-06-24 05:43:46 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2002-08-30 23:07:10 +08:00
|
|
|
typedef std::vector<osg::Transform*> TransformStack;
|
|
|
|
typedef std::set<osg::Drawable*> DrawableSet;
|
|
|
|
typedef std::set<osg::Billboard*> BillboardSet;
|
2004-07-29 18:13:02 +08:00
|
|
|
typedef std::set<osg::Node* > NodeSet;
|
2002-08-30 23:07:10 +08:00
|
|
|
typedef std::set<osg::Transform*> TransformSet;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-24 05:43:46 +08:00
|
|
|
TransformStack _transformStack;
|
2004-07-29 18:13:02 +08:00
|
|
|
NodeSet _excludedNodeSet;
|
2002-08-30 23:07:10 +08:00
|
|
|
DrawableSet _drawableSet;
|
|
|
|
BillboardSet _billboardSet;
|
|
|
|
TransformSet _transformSet;
|
2002-06-24 05:43:46 +08:00
|
|
|
};
|
|
|
|
|
2013-06-19 20:32:40 +08:00
|
|
|
/** FlattenStaticTransformsDuplicatingSharedSubgraphsVisitor is similar
|
|
|
|
* to FlattenStaticTransformsVisitor in that it is designed to remove static transforms
|
2008-06-20 21:16:35 +08:00
|
|
|
* from the scene graph, pushing down the transforms to the geometry leaves of the scene graph,
|
|
|
|
* but with the difference that any subgraphs that are shared between different transforms
|
2013-06-19 20:32:40 +08:00
|
|
|
* are duplicated and flattened individually. This results in more static transforms
|
2008-06-20 21:16:35 +08:00
|
|
|
* being removed, but also means that more data is generated, and as a result may
|
2012-03-22 01:36:20 +08:00
|
|
|
* not always be the most appropriate flatten visitor to use.*/
|
2008-06-27 18:17:38 +08:00
|
|
|
class OSGUTIL_EXPORT FlattenStaticTransformsDuplicatingSharedSubgraphsVisitor : public BaseOptimizerVisitor
|
2008-06-20 21:16:35 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
FlattenStaticTransformsDuplicatingSharedSubgraphsVisitor(Optimizer* optimizer=0):
|
|
|
|
BaseOptimizerVisitor(optimizer, FLATTEN_STATIC_TRANSFORMS_DUPLICATING_SHARED_SUBGRAPHS) {}
|
|
|
|
|
|
|
|
virtual void reset();
|
|
|
|
|
|
|
|
virtual void apply(osg::Group& group);
|
|
|
|
virtual void apply(osg::Transform& transform);
|
|
|
|
virtual void apply(osg::LOD& lod);
|
|
|
|
virtual void apply(osg::Geode& geode);
|
|
|
|
virtual void apply(osg::Billboard& billboard);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2008-11-25 22:31:19 +08:00
|
|
|
void transformGeode(osg::Geode& geode);
|
|
|
|
void transformDrawable(osg::Drawable& drawable);
|
2008-06-20 21:16:35 +08:00
|
|
|
void transformBillboard(osg::Billboard& billboard);
|
|
|
|
|
|
|
|
std::vector<osg::Matrix> _matrixStack;
|
|
|
|
|
|
|
|
};
|
2002-08-30 23:07:10 +08:00
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** Combine Static Transform nodes that sit above one another.*/
|
2005-03-18 10:44:08 +08:00
|
|
|
class OSGUTIL_EXPORT CombineStaticTransformsVisitor : public BaseOptimizerVisitor
|
2003-12-05 22:39:32 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2003-12-12 07:26:22 +08:00
|
|
|
CombineStaticTransformsVisitor(Optimizer* optimizer=0):
|
2005-03-18 10:44:08 +08:00
|
|
|
BaseOptimizerVisitor(optimizer, FLATTEN_STATIC_TRANSFORMS) {}
|
2003-12-05 22:39:32 +08:00
|
|
|
|
|
|
|
virtual void apply(osg::MatrixTransform& transform);
|
|
|
|
|
|
|
|
bool removeTransforms(osg::Node* nodeWeCannotRemove);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
typedef std::set<osg::MatrixTransform*> TransformSet;
|
|
|
|
TransformSet _transformSet;
|
|
|
|
};
|
|
|
|
|
2002-12-07 14:59:08 +08:00
|
|
|
/** Remove rendundant nodes, such as groups with one single child.*/
|
2005-03-18 10:44:08 +08:00
|
|
|
class OSGUTIL_EXPORT RemoveEmptyNodesVisitor : public BaseOptimizerVisitor
|
2002-06-24 05:43:46 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
|
|
typedef std::set<osg::Node*> NodeList;
|
2002-12-07 14:59:08 +08:00
|
|
|
NodeList _redundantNodeList;
|
2002-06-24 05:43:46 +08:00
|
|
|
|
2003-12-12 07:26:22 +08:00
|
|
|
RemoveEmptyNodesVisitor(Optimizer* optimizer=0):
|
2005-03-18 10:44:08 +08:00
|
|
|
BaseOptimizerVisitor(optimizer, REMOVE_REDUNDANT_NODES) {}
|
2003-12-12 07:26:22 +08:00
|
|
|
|
2002-06-24 05:43:46 +08:00
|
|
|
virtual void apply(osg::Group& group);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-06-24 05:43:46 +08:00
|
|
|
void removeEmptyNodes();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2005-04-30 23:16:05 +08:00
|
|
|
/** Remove redundant nodes, such as groups with one single child.*/
|
2005-03-18 10:44:08 +08:00
|
|
|
class OSGUTIL_EXPORT RemoveRedundantNodesVisitor : public BaseOptimizerVisitor
|
2001-10-19 22:22:02 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef std::set<osg::Node*> NodeList;
|
2002-12-07 14:59:08 +08:00
|
|
|
NodeList _redundantNodeList;
|
2001-10-19 22:22:02 +08:00
|
|
|
|
2003-12-12 07:26:22 +08:00
|
|
|
RemoveRedundantNodesVisitor(Optimizer* optimizer=0):
|
2005-03-18 10:44:08 +08:00
|
|
|
BaseOptimizerVisitor(optimizer, REMOVE_REDUNDANT_NODES) {}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2001-10-19 22:22:02 +08:00
|
|
|
virtual void apply(osg::Group& group);
|
2002-06-24 05:43:46 +08:00
|
|
|
virtual void apply(osg::Transform& transform);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-11-26 22:17:22 +08:00
|
|
|
bool isOperationPermissible(osg::Node& node);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-12-07 14:59:08 +08:00
|
|
|
void removeRedundantNodes();
|
2001-10-19 22:22:02 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2005-04-30 23:16:05 +08:00
|
|
|
/** Remove loaded proxy nodes.*/
|
|
|
|
class OSGUTIL_EXPORT RemoveLoadedProxyNodesVisitor : public BaseOptimizerVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef std::set<osg::Node*> NodeList;
|
|
|
|
NodeList _redundantNodeList;
|
|
|
|
|
|
|
|
RemoveLoadedProxyNodesVisitor(Optimizer* optimizer=0):
|
|
|
|
BaseOptimizerVisitor(optimizer, REMOVE_LOADED_PROXY_NODES) {}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-04-30 23:16:05 +08:00
|
|
|
virtual void apply(osg::ProxyNode& group);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-04-30 23:16:05 +08:00
|
|
|
void removeRedundantNodes();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-01-21 05:47:43 +08:00
|
|
|
/** Tessellate all Geometries, to remove POLYGONS.*/
|
2007-01-09 05:29:49 +08:00
|
|
|
class OSGUTIL_EXPORT TessellateVisitor : public BaseOptimizerVisitor
|
2003-12-28 06:17:25 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef std::set<osg::Group*> GroupList;
|
|
|
|
GroupList _groupList;
|
|
|
|
|
2007-01-09 05:29:49 +08:00
|
|
|
TessellateVisitor(Optimizer* optimizer=0):
|
|
|
|
BaseOptimizerVisitor(optimizer, TESSELLATE_GEOMETRY) {}
|
2003-12-28 06:17:25 +08:00
|
|
|
|
2017-01-21 05:47:43 +08:00
|
|
|
virtual void apply(osg::Geometry& geom);
|
2003-12-28 06:17:25 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2001-10-19 22:22:02 +08:00
|
|
|
/** Optimize the LOD groups, by combining adjacent LOD's which have
|
|
|
|
* complementary ranges.*/
|
2005-03-18 10:44:08 +08:00
|
|
|
class OSGUTIL_EXPORT CombineLODsVisitor : public BaseOptimizerVisitor
|
2001-10-19 22:22:02 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef std::set<osg::Group*> GroupList;
|
|
|
|
GroupList _groupList;
|
|
|
|
|
2003-12-12 07:26:22 +08:00
|
|
|
CombineLODsVisitor(Optimizer* optimizer=0):
|
2005-03-18 10:44:08 +08:00
|
|
|
BaseOptimizerVisitor(optimizer, COMBINE_ADJACENT_LODS) {}
|
2001-10-19 22:22:02 +08:00
|
|
|
|
|
|
|
virtual void apply(osg::LOD& lod);
|
|
|
|
|
|
|
|
void combineLODs();
|
|
|
|
|
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2001-10-19 22:22:02 +08:00
|
|
|
/** Optimize State in the scene graph by removing duplicate state,
|
|
|
|
* replacing it with shared instances, both for StateAttributes,
|
|
|
|
* and whole StateSets.*/
|
2005-03-18 10:44:08 +08:00
|
|
|
class OSGUTIL_EXPORT StateVisitor : public BaseOptimizerVisitor
|
2001-10-19 22:22:02 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// default to traversing all children.
|
2008-06-29 20:22:50 +08:00
|
|
|
StateVisitor(bool combineDynamicState,
|
|
|
|
bool combineStaticState,
|
|
|
|
bool combineUnspecifiedState,
|
|
|
|
Optimizer* optimizer=0):
|
|
|
|
BaseOptimizerVisitor(optimizer, SHARE_DUPLICATE_STATE)
|
|
|
|
{
|
|
|
|
_optimize[osg::Object::DYNAMIC] = combineDynamicState;
|
|
|
|
_optimize[osg::Object::STATIC] = combineStaticState;
|
|
|
|
_optimize[osg::Object::UNSPECIFIED] = combineUnspecifiedState;
|
|
|
|
}
|
2001-10-19 22:22:02 +08:00
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** empty visitor, make it ready for next traversal.*/
|
2001-10-19 22:22:02 +08:00
|
|
|
virtual void reset();
|
|
|
|
|
|
|
|
virtual void apply(osg::Node& node);
|
|
|
|
|
|
|
|
virtual void apply(osg::Geode& geode);
|
|
|
|
|
|
|
|
void optimize();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2015-04-07 23:49:06 +08:00
|
|
|
void addStateSet(osg::StateSet* stateset, osg::Node* node);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-06-29 20:22:50 +08:00
|
|
|
inline bool optimize(osg::Object::DataVariance variance)
|
|
|
|
{
|
|
|
|
return _optimize[variance];
|
|
|
|
}
|
2001-10-19 22:22:02 +08:00
|
|
|
|
2015-04-07 23:49:06 +08:00
|
|
|
typedef std::set<osg::Node*> NodeSet;
|
|
|
|
typedef std::map<osg::StateSet*, NodeSet> StateSetMap;
|
2001-10-19 22:22:02 +08:00
|
|
|
|
2008-06-29 20:22:50 +08:00
|
|
|
// note, one element for DYNAMIC, STATIC and UNSPECIFIED
|
|
|
|
bool _optimize[3];
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2001-10-19 22:22:02 +08:00
|
|
|
StateSetMap _statesets;
|
|
|
|
|
|
|
|
};
|
2006-04-06 22:06:22 +08:00
|
|
|
|
|
|
|
/** Combine geodes
|
|
|
|
*/
|
|
|
|
class OSGUTIL_EXPORT MergeGeodesVisitor : public BaseOptimizerVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// default to traversing all children.
|
|
|
|
MergeGeodesVisitor(Optimizer* optimizer=0):
|
|
|
|
BaseOptimizerVisitor(optimizer, MERGE_GEODES) {}
|
|
|
|
|
|
|
|
virtual void apply(osg::Group& group);
|
|
|
|
|
|
|
|
bool mergeGeodes(osg::Group& group);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
bool mergeGeode(osg::Geode& lhs, osg::Geode& rhs);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2005-03-18 10:44:08 +08:00
|
|
|
class OSGUTIL_EXPORT CheckGeometryVisitor : public BaseOptimizerVisitor
|
2003-09-24 23:54:22 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// default to traversing all children.
|
2003-12-12 07:26:22 +08:00
|
|
|
CheckGeometryVisitor(Optimizer* optimizer=0):
|
2005-03-18 10:44:08 +08:00
|
|
|
BaseOptimizerVisitor(optimizer, CHECK_GEOMETRY) {}
|
2003-09-24 23:54:22 +08:00
|
|
|
|
|
|
|
virtual void apply(osg::Geode& geode) { checkGeode(geode); }
|
|
|
|
|
2003-12-12 07:26:22 +08:00
|
|
|
void checkGeode(osg::Geode& geode);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-09-24 23:54:22 +08:00
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-03-10 22:15:59 +08:00
|
|
|
class OSGUTIL_EXPORT MakeFastGeometryVisitor : public BaseOptimizerVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// default to traversing all children.
|
|
|
|
MakeFastGeometryVisitor(Optimizer* optimizer=0):
|
|
|
|
BaseOptimizerVisitor(optimizer, MAKE_FAST_GEOMETRY) {}
|
|
|
|
|
|
|
|
virtual void apply(osg::Geode& geode) { checkGeode(geode); }
|
|
|
|
|
|
|
|
void checkGeode(osg::Geode& geode);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2005-03-18 10:44:08 +08:00
|
|
|
class OSGUTIL_EXPORT MergeGeometryVisitor : public BaseOptimizerVisitor
|
2002-07-18 08:53:03 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// default to traversing all children.
|
2003-12-12 07:26:22 +08:00
|
|
|
MergeGeometryVisitor(Optimizer* optimizer=0) :
|
2005-04-24 00:09:20 +08:00
|
|
|
BaseOptimizerVisitor(optimizer, MERGE_GEOMETRY),
|
|
|
|
_targetMaximumNumberOfVertices(10000) {}
|
|
|
|
|
|
|
|
void setTargetMaximumNumberOfVertices(unsigned int num)
|
|
|
|
{
|
|
|
|
_targetMaximumNumberOfVertices = num;
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-04-24 00:09:20 +08:00
|
|
|
unsigned int getTargetMaximumNumberOfVertices() const
|
|
|
|
{
|
|
|
|
return _targetMaximumNumberOfVertices;
|
|
|
|
}
|
2001-10-19 22:22:02 +08:00
|
|
|
|
2002-07-18 08:53:03 +08:00
|
|
|
virtual void apply(osg::Geode& geode) { mergeGeode(geode); }
|
2002-07-18 17:17:50 +08:00
|
|
|
virtual void apply(osg::Billboard&) { /* don't do anything*/ }
|
2002-07-18 08:53:03 +08:00
|
|
|
|
2003-12-12 07:26:22 +08:00
|
|
|
bool mergeGeode(osg::Geode& geode);
|
2002-07-19 22:19:49 +08:00
|
|
|
|
2003-01-08 00:29:07 +08:00
|
|
|
static bool geometryContainsSharedArrays(osg::Geometry& geom);
|
|
|
|
|
2002-07-18 08:53:03 +08:00
|
|
|
static bool mergeGeometry(osg::Geometry& lhs,osg::Geometry& rhs);
|
|
|
|
|
2002-07-19 22:19:49 +08:00
|
|
|
static bool mergePrimitive(osg::DrawArrays& lhs,osg::DrawArrays& rhs);
|
|
|
|
static bool mergePrimitive(osg::DrawArrayLengths& lhs,osg::DrawArrayLengths& rhs);
|
|
|
|
static bool mergePrimitive(osg::DrawElementsUByte& lhs,osg::DrawElementsUByte& rhs);
|
|
|
|
static bool mergePrimitive(osg::DrawElementsUShort& lhs,osg::DrawElementsUShort& rhs);
|
|
|
|
static bool mergePrimitive(osg::DrawElementsUInt& lhs,osg::DrawElementsUInt& rhs);
|
|
|
|
|
2005-04-24 00:09:20 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
unsigned int _targetMaximumNumberOfVertices;
|
|
|
|
|
2002-07-18 08:53:03 +08:00
|
|
|
};
|
2003-12-01 18:28:23 +08:00
|
|
|
|
|
|
|
/** Spatialize scene into a balanced quad/oct tree.*/
|
2005-03-18 10:44:08 +08:00
|
|
|
class OSGUTIL_EXPORT SpatializeGroupsVisitor : public BaseOptimizerVisitor
|
2003-12-01 18:28:23 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2003-12-12 07:26:22 +08:00
|
|
|
SpatializeGroupsVisitor(Optimizer* optimizer=0):
|
2005-03-18 10:44:08 +08:00
|
|
|
BaseOptimizerVisitor(optimizer, SPATIALIZE_GROUPS) {}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-12-01 18:28:23 +08:00
|
|
|
virtual void apply(osg::Group& group);
|
2008-04-14 03:31:09 +08:00
|
|
|
virtual void apply(osg::Geode& geode);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-12-01 18:28:23 +08:00
|
|
|
bool divide(unsigned int maxNumTreesPerCell=8);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-12-01 18:28:23 +08:00
|
|
|
bool divide(osg::Group* group, unsigned int maxNumTreesPerCell);
|
2008-04-14 03:31:09 +08:00
|
|
|
bool divide(osg::Geode* geode, unsigned int maxNumTreesPerCell);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-12-01 18:28:23 +08:00
|
|
|
typedef std::set<osg::Group*> GroupsToDivideList;
|
|
|
|
GroupsToDivideList _groupsToDivideList;
|
|
|
|
|
2008-04-14 03:31:09 +08:00
|
|
|
typedef std::set<osg::Geode*> GeodesToDivideList;
|
|
|
|
GeodesToDivideList _geodesToDivideList;
|
2003-12-01 18:28:23 +08:00
|
|
|
};
|
2003-12-04 05:45:32 +08:00
|
|
|
|
|
|
|
/** Copy any shared subgraphs, enabling flattening of static transforms.*/
|
2005-03-18 10:44:08 +08:00
|
|
|
class OSGUTIL_EXPORT CopySharedSubgraphsVisitor : public BaseOptimizerVisitor
|
2003-12-04 05:45:32 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2003-12-12 07:26:22 +08:00
|
|
|
CopySharedSubgraphsVisitor(Optimizer* optimizer=0):
|
2005-03-18 10:44:08 +08:00
|
|
|
BaseOptimizerVisitor(optimizer, COPY_SHARED_NODES) {}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-12-04 05:45:32 +08:00
|
|
|
virtual void apply(osg::Node& node);
|
|
|
|
|
|
|
|
void copySharedNodes();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-12-04 05:45:32 +08:00
|
|
|
typedef std::set<osg::Node*> SharedNodeList;
|
|
|
|
SharedNodeList _sharedNodeList;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-12-04 05:45:32 +08:00
|
|
|
};
|
2004-07-12 21:20:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
/** For all textures apply settings.*/
|
2005-03-18 10:44:08 +08:00
|
|
|
class OSGUTIL_EXPORT TextureVisitor : public BaseOptimizerVisitor
|
2004-07-12 21:20:18 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
TextureVisitor(bool changeAutoUnRef, bool valueAutoUnRef,
|
|
|
|
bool changeClientImageStorage, bool valueClientImageStorage,
|
|
|
|
bool changeAnisotropy, float valueAnisotropy,
|
|
|
|
Optimizer* optimizer=0):
|
2005-03-18 10:44:08 +08:00
|
|
|
BaseOptimizerVisitor(optimizer, OPTIMIZE_TEXTURE_SETTINGS),
|
2004-07-12 21:20:18 +08:00
|
|
|
_changeAutoUnRef(changeAutoUnRef), _valueAutoUnRef(valueAutoUnRef),
|
|
|
|
_changeClientImageStorage(changeClientImageStorage), _valueClientImageStorage(valueClientImageStorage),
|
|
|
|
_changeAnisotropy(changeAnisotropy), _valueAnisotropy(valueAnisotropy) {}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-07-12 21:20:18 +08:00
|
|
|
virtual void apply(osg::Node& node);
|
|
|
|
|
|
|
|
void apply(osg::StateSet& stateset);
|
|
|
|
void apply(osg::Texture& texture);
|
|
|
|
|
|
|
|
bool _changeAutoUnRef, _valueAutoUnRef;
|
|
|
|
bool _changeClientImageStorage, _valueClientImageStorage;
|
2004-07-22 15:41:17 +08:00
|
|
|
bool _changeAnisotropy;
|
|
|
|
float _valueAnisotropy;
|
2004-07-12 21:20:18 +08:00
|
|
|
|
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-07-18 19:03:46 +08:00
|
|
|
/** Flatten MatrixTransform/Billboard pairs.*/
|
|
|
|
class OSGUTIL_EXPORT FlattenBillboardVisitor : public BaseOptimizerVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FlattenBillboardVisitor(Optimizer* optimizer=0):
|
|
|
|
BaseOptimizerVisitor(optimizer, FLATTEN_BILLBOARDS) {}
|
|
|
|
|
|
|
|
typedef std::vector<osg::NodePath> NodePathList;
|
|
|
|
typedef std::map<osg::Billboard*, NodePathList > BillboardNodePathMap;
|
|
|
|
|
|
|
|
virtual void reset();
|
|
|
|
|
|
|
|
virtual void apply(osg::Billboard& billboard);
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
void process();
|
2006-07-18 19:03:46 +08:00
|
|
|
|
|
|
|
BillboardNodePathMap _billboards;
|
|
|
|
|
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-08-25 23:53:16 +08:00
|
|
|
/** Texture Atlas Builder creates a set of textures/images which each contain multiple images.
|
|
|
|
* Texture Atlas' are used to make it possible to use much wider batching of data. */
|
|
|
|
class OSGUTIL_EXPORT TextureAtlasBuilder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TextureAtlasBuilder();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-08-28 18:46:39 +08:00
|
|
|
void reset();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-11-06 01:04:08 +08:00
|
|
|
void setMaximumAtlasSize(int width, int height);
|
2006-08-25 23:53:16 +08:00
|
|
|
|
2010-11-06 01:04:08 +08:00
|
|
|
int getMaximumAtlasWidth() const { return _maximumAtlasWidth; }
|
|
|
|
int getMaximumAtlasHeight() const { return _maximumAtlasHeight; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-11-06 01:04:08 +08:00
|
|
|
void setMargin(int margin);
|
|
|
|
int getMargin() const { return _margin; }
|
2006-08-25 23:53:16 +08:00
|
|
|
|
|
|
|
void addSource(const osg::Image* image);
|
|
|
|
void addSource(const osg::Texture2D* texture);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-08-25 23:53:16 +08:00
|
|
|
unsigned int getNumSources() const { return _sourceList.size(); }
|
|
|
|
const osg::Image* getSourceImage(unsigned int i) { return _sourceList[i]->_image.get(); }
|
|
|
|
const osg::Texture2D* getSourceTexture(unsigned int i) { return _sourceList[i]->_texture.get(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-08-25 23:53:16 +08:00
|
|
|
void buildAtlas();
|
|
|
|
osg::Image* getImageAtlas(unsigned int i);
|
|
|
|
osg::Texture2D* getTextureAtlas(unsigned int i);
|
|
|
|
osg::Matrix getTextureMatrix(unsigned int i);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-08-25 23:53:16 +08:00
|
|
|
osg::Image* getImageAtlas(const osg::Image* image);
|
|
|
|
osg::Texture2D* getTextureAtlas(const osg::Image* image);
|
|
|
|
osg::Matrix getTextureMatrix(const osg::Image* image);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-08-28 18:46:39 +08:00
|
|
|
osg::Image* getImageAtlas(const osg::Texture2D* textue);
|
2006-08-25 23:53:16 +08:00
|
|
|
osg::Texture2D* getTextureAtlas(const osg::Texture2D* texture);
|
|
|
|
osg::Matrix getTextureMatrix(const osg::Texture2D* texture);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-08-25 23:53:16 +08:00
|
|
|
protected:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-11-06 01:04:08 +08:00
|
|
|
int _maximumAtlasWidth;
|
|
|
|
int _maximumAtlasHeight;
|
|
|
|
int _margin;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-08-25 23:53:16 +08:00
|
|
|
|
|
|
|
// forward declare
|
|
|
|
class Atlas;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-08-25 23:53:16 +08:00
|
|
|
class Source : public osg::Referenced
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Source():
|
|
|
|
_x(0),_y(0),_atlas(0) {}
|
|
|
|
|
|
|
|
Source(const osg::Image* image):
|
|
|
|
_x(0),_y(0),_atlas(0),_image(image) {}
|
|
|
|
|
|
|
|
Source(const osg::Texture2D* texture):
|
|
|
|
_x(0),_y(0),_atlas(0),_texture(texture) { if (texture) _image = texture->getImage(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-11-06 01:04:08 +08:00
|
|
|
int _x;
|
|
|
|
int _y;
|
2006-08-25 23:53:16 +08:00
|
|
|
Atlas* _atlas;
|
|
|
|
|
|
|
|
osg::ref_ptr<const osg::Image> _image;
|
|
|
|
osg::ref_ptr<const osg::Texture2D> _texture;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-11-06 01:04:08 +08:00
|
|
|
bool suitableForAtlas(int maximumAtlasWidth, int maximumAtlasHeight, int margin);
|
2006-08-25 23:53:16 +08:00
|
|
|
osg::Matrix computeTextureMatrix() const;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
|
2006-08-25 23:53:16 +08:00
|
|
|
protected:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-08-25 23:53:16 +08:00
|
|
|
virtual ~Source() {}
|
|
|
|
};
|
2010-11-06 01:04:08 +08:00
|
|
|
|
2006-08-25 23:53:16 +08:00
|
|
|
typedef std::vector< osg::ref_ptr<Source> > SourceList;
|
|
|
|
|
|
|
|
class Atlas : public osg::Referenced
|
|
|
|
{
|
|
|
|
public:
|
2010-11-06 01:04:08 +08:00
|
|
|
Atlas(int width, int height, int margin):
|
2006-08-25 23:53:16 +08:00
|
|
|
_maximumAtlasWidth(width),
|
|
|
|
_maximumAtlasHeight(height),
|
|
|
|
_margin(margin),
|
|
|
|
_x(0),
|
2006-08-28 18:46:39 +08:00
|
|
|
_y(0),
|
2006-08-25 23:53:16 +08:00
|
|
|
_width(0),
|
2010-11-06 01:04:08 +08:00
|
|
|
_height(0),
|
|
|
|
_indexFirstOfRow(0){}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-11-06 01:04:08 +08:00
|
|
|
int _maximumAtlasWidth;
|
|
|
|
int _maximumAtlasHeight;
|
|
|
|
int _margin;
|
2006-08-25 23:53:16 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::Texture2D> _texture;
|
|
|
|
osg::ref_ptr<osg::Image> _image;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-08-25 23:53:16 +08:00
|
|
|
SourceList _sourceList;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-11-06 01:04:08 +08:00
|
|
|
int _x;
|
|
|
|
int _y;
|
|
|
|
int _width;
|
|
|
|
int _height;
|
|
|
|
unsigned int _indexFirstOfRow; ///< Contain the index of the first element of the last row.
|
|
|
|
enum FitsIn
|
|
|
|
{
|
2010-12-06 18:35:02 +08:00
|
|
|
DOES_NOT_FIT_IN_ANY_ROW,
|
|
|
|
FITS_IN_CURRENT_ROW,
|
2010-11-06 01:04:08 +08:00
|
|
|
IN_NEXT_ROW
|
|
|
|
};
|
2010-12-06 18:35:02 +08:00
|
|
|
FitsIn doesSourceFit(Source* source);
|
2006-08-25 23:53:16 +08:00
|
|
|
bool addSource(Source* source);
|
|
|
|
void clampToNearestPowerOfTwoSize();
|
|
|
|
void copySources();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-08-25 23:53:16 +08:00
|
|
|
protected:
|
|
|
|
virtual ~Atlas() {}
|
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-08-25 23:53:16 +08:00
|
|
|
typedef std::vector< osg::ref_ptr<Atlas> > AtlasList;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-08-25 23:53:16 +08:00
|
|
|
Source* getSource(const osg::Image* image);
|
|
|
|
Source* getSource(const osg::Texture2D* texture);
|
|
|
|
|
|
|
|
SourceList _sourceList;
|
2010-11-06 01:04:08 +08:00
|
|
|
AtlasList _atlasList;
|
|
|
|
private:
|
|
|
|
struct CompareSrc
|
|
|
|
{
|
|
|
|
bool operator()(osg::ref_ptr<Source> src1, osg::ref_ptr<Source> src2) const
|
|
|
|
{
|
|
|
|
return src1->_image->t() > src2->_image->t();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
void completeRow(unsigned int indexAtlas);
|
2006-08-25 23:53:16 +08:00
|
|
|
};
|
2006-07-18 19:03:46 +08:00
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
/** Optimize texture usage in the scene graph by combining textures into texture atlas
|
2015-04-13 18:43:56 +08:00
|
|
|
* Use of texture atlas cuts down on the number of separate states in the scene, reducing
|
2013-06-19 20:32:40 +08:00
|
|
|
* state changes and improving the chances of using larger batches of geometry.*/
|
2006-08-28 18:46:39 +08:00
|
|
|
class OSGUTIL_EXPORT TextureAtlasVisitor : public BaseOptimizerVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// default to traversing all children.
|
|
|
|
TextureAtlasVisitor(Optimizer* optimizer=0):
|
|
|
|
BaseOptimizerVisitor(optimizer, TEXTURE_ATLAS_BUILDER) {}
|
|
|
|
|
|
|
|
|
|
|
|
TextureAtlasBuilder& getTextureAtlasBuilder() { return _builder; }
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** empty visitor, make it ready for next traversal.*/
|
2006-08-28 18:46:39 +08:00
|
|
|
virtual void reset();
|
|
|
|
|
|
|
|
virtual void apply(osg::Node& node);
|
2017-01-21 04:25:32 +08:00
|
|
|
virtual void apply(osg::Drawable& node);
|
2006-08-28 18:46:39 +08:00
|
|
|
|
|
|
|
void optimize();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2006-08-28 23:46:02 +08:00
|
|
|
bool pushStateSet(osg::StateSet* stateset);
|
|
|
|
void popStateSet();
|
2006-08-28 18:46:39 +08:00
|
|
|
|
2006-08-28 23:46:02 +08:00
|
|
|
typedef std::set<osg::Drawable*> Drawables;
|
|
|
|
typedef std::map<osg::StateSet*, Drawables> StateSetMap;
|
2006-08-28 18:46:39 +08:00
|
|
|
typedef std::set<osg::Texture2D*> Textures;
|
2006-08-28 23:46:02 +08:00
|
|
|
typedef std::vector<osg::StateSet*> StateSetStack;
|
2006-08-28 18:46:39 +08:00
|
|
|
|
|
|
|
TextureAtlasBuilder _builder;
|
|
|
|
|
2006-08-28 23:46:02 +08:00
|
|
|
StateSetMap _statesetMap;
|
|
|
|
StateSetStack _statesetStack;
|
|
|
|
Textures _textures;
|
2006-08-28 18:46:39 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2007-01-30 06:44:29 +08:00
|
|
|
/** Optimize the setting of StateSet and Geometry objects in scene so that they have a STATIC DataVariance
|
|
|
|
* when they don't have any callbacks associated with them. */
|
|
|
|
class OSGUTIL_EXPORT StaticObjectDetectionVisitor : public BaseOptimizerVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// default to traversing all children.
|
|
|
|
StaticObjectDetectionVisitor(Optimizer* optimizer=0):
|
|
|
|
BaseOptimizerVisitor(optimizer, STATIC_OBJECT_DETECTION) {}
|
|
|
|
|
|
|
|
virtual void apply(osg::Node& node);
|
2017-01-21 04:48:45 +08:00
|
|
|
virtual void apply(osg::Drawable& drawable);
|
2007-01-30 06:44:29 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
void applyStateSet(osg::StateSet& stateset);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-01-30 06:44:29 +08:00
|
|
|
};
|
2016-07-11 16:52:07 +08:00
|
|
|
|
|
|
|
/** For all geometry apply settings.*/
|
|
|
|
class OSGUTIL_EXPORT BufferObjectVisitor : public BaseOptimizerVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
BufferObjectVisitor(bool changeVBO, bool valueVBO,
|
|
|
|
bool changeVertexArrayObject, bool valueVertexArrayObject,
|
|
|
|
bool changeDisplayList, bool valueDisplayList,
|
|
|
|
Optimizer* optimizer=0):
|
|
|
|
BaseOptimizerVisitor(optimizer, BUFFER_OBJECT_SETTINGS),
|
|
|
|
_changeVertexBufferObject(changeVBO), _valueVertexBufferObject(valueVBO),
|
|
|
|
_changeVertexArrayObject(changeVertexArrayObject), _valueVertexArrayObject(valueVertexArrayObject),
|
|
|
|
_changeDisplayList(changeDisplayList), _valueDisplayList(valueDisplayList) {}
|
|
|
|
|
|
|
|
virtual void apply(osg::Geometry& geometry);
|
|
|
|
|
|
|
|
bool _changeVertexBufferObject, _valueVertexBufferObject;
|
|
|
|
bool _changeVertexArrayObject, _valueVertexArrayObject;
|
|
|
|
bool _changeDisplayList, _valueDisplayList;
|
|
|
|
|
|
|
|
};
|
2001-10-19 22:22:02 +08:00
|
|
|
};
|
|
|
|
|
2005-05-13 21:29:45 +08:00
|
|
|
inline bool BaseOptimizerVisitor::isOperationPermissibleForObject(const osg::StateSet* object) const
|
|
|
|
{
|
2012-03-22 01:36:20 +08:00
|
|
|
return _optimizer ? _optimizer->isOperationPermissibleForObject(object,_operationType) : true;
|
2005-05-13 21:29:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BaseOptimizerVisitor::isOperationPermissibleForObject(const osg::StateAttribute* object) const
|
|
|
|
{
|
2012-03-22 01:36:20 +08:00
|
|
|
return _optimizer ? _optimizer->isOperationPermissibleForObject(object,_operationType) : true;
|
2005-05-13 21:29:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BaseOptimizerVisitor::isOperationPermissibleForObject(const osg::Drawable* object) const
|
|
|
|
{
|
2012-03-22 01:36:20 +08:00
|
|
|
return _optimizer ? _optimizer->isOperationPermissibleForObject(object,_operationType) : true;
|
2005-05-13 21:29:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool BaseOptimizerVisitor::isOperationPermissibleForObject(const osg::Node* object) const
|
|
|
|
{
|
2012-03-22 01:36:20 +08:00
|
|
|
return _optimizer ? _optimizer->isOperationPermissibleForObject(object,_operationType) : true;
|
2005-05-13 21:29:45 +08:00
|
|
|
}
|
|
|
|
|
2002-02-03 20:33:41 +08:00
|
|
|
}
|
2001-10-19 22:22:02 +08:00
|
|
|
|
|
|
|
#endif
|