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 OSGUTIL_MESHOPTIMIZERS
15#define OSGUTIL_MESHOPTIMIZERS 1
21#include <osg/Geometry>
22#include <osg/NodeVisitor>
24#include <osgUtil/Optimizer>
28// Helper that collects all the unique Geometry objects in a subgraph.
29class OSGUTIL_EXPORT GeometryCollector : public BaseOptimizerVisitor
32 GeometryCollector(Optimizer* optimizer,
33 Optimizer::OptimizationOptions options)
34 : BaseOptimizerVisitor(optimizer, options) {}
36 void apply(osg::Geometry& geom);
37 typedef std::set<osg::Geometry*> GeometryList;
38 GeometryList& getGeometryList() { return _geometryList; };
40 GeometryList _geometryList;
43// Convert geometry that uses DrawArrays to DrawElements i.e.,
44// construct a real mesh. This removes duplicate vertices.
45class OSGUTIL_EXPORT IndexMeshVisitor : public GeometryCollector
48 IndexMeshVisitor(Optimizer* optimizer = 0)
49 : GeometryCollector(optimizer, Optimizer::INDEX_MESH), _generateNewIndicesOnAllGeometries(false)
52 inline void setGenerateNewIndicesOnAllGeometries(bool b) { _generateNewIndicesOnAllGeometries = b; }
53 inline bool getGenerateNewIndicesOnAllGeometries() const { return _generateNewIndicesOnAllGeometries; }
55 void makeMesh(osg::Geometry& geom);
58 bool _generateNewIndicesOnAllGeometries;
61// Optimize the triangle order in a mesh for best use of the GPU's
62// post-transform cache. This uses Tom Forsyth's algorithm described
63// at http://home.comcast.net/~tom_forsyth/papers/fast_vert_cache_opt.html
64class OSGUTIL_EXPORT VertexCacheVisitor : public GeometryCollector
67 VertexCacheVisitor(Optimizer* optimizer = 0)
68 : GeometryCollector(optimizer, Optimizer::VERTEX_POSTTRANSFORM)
72 void optimizeVertices(osg::Geometry& geom);
73 void optimizeVertices();
75 void doVertexOptimization(osg::Geometry& geom,
76 std::vector<unsigned>& vertDrawList);
79// Gather statistics on post-transform cache misses for geometry
80class OSGUTIL_EXPORT VertexCacheMissVisitor : public osg::NodeVisitor
83 VertexCacheMissVisitor(unsigned cacheSize = 16);
85 virtual void apply(osg::Geometry& geom);
86 void doGeometry(osg::Geometry& geom);
90 const unsigned _cacheSize;
93// Optimize the use of the GPU pre-transform cache by arranging vertex
94// attributes in the order they are used.
95class OSGUTIL_EXPORT VertexAccessOrderVisitor : public GeometryCollector
97 struct OrderByPrimitiveMode
99 inline bool operator() (const osg::ref_ptr<osg::PrimitiveSet>& prim1, const osg::ref_ptr<osg::PrimitiveSet>& prim2)
102 return prim1->getMode() > prim2->getMode();
109 } order_by_primitive_mode;
112 VertexAccessOrderVisitor(Optimizer* optimizer = 0)
113 : GeometryCollector(optimizer, Optimizer::VERTEX_PRETRANSFORM)
116 void optimizeOrder();
117 void optimizeOrder(osg::Geometry& geom);
120class OSGUTIL_EXPORT SharedArrayOptimizer
123 void findDuplicatedUVs(const osg::Geometry& geometry);
124 void deduplicateUVs(osg::Geometry& geometry);
126 std::map<unsigned int, unsigned int> _deduplicateUvs;
127}; // SharedArrayOptimizer
130inline void optimizeMesh(osg::Node* node)
132 IndexMeshVisitor imv;
136 VertexCacheVisitor vcv;
138 vcv.optimizeVertices();
140 VertexAccessOrderVisitor vaov;
142 vaov.optimizeOrder();