diff --git a/VisualStudio/osgUtil/osgUtil.dsp b/VisualStudio/osgUtil/osgUtil.dsp index db2f55c1a..8062e2b34 100755 --- a/VisualStudio/osgUtil/osgUtil.dsp +++ b/VisualStudio/osgUtil/osgUtil.dsp @@ -161,6 +161,10 @@ SOURCE=..\..\src\osgUtil\SceneView.cpp # End Source File # Begin Source File +SOURCE=..\..\src\osgUtil\Simplifier.cpp +# End Source File +# Begin Source File + SOURCE=..\..\src\osgUtil\SmoothingVisitor.cpp # End Source File # Begin Source File @@ -277,6 +281,10 @@ SOURCE=..\..\Include\osgUtil\SceneView # End Source File # Begin Source File +SOURCE=..\..\include\osgUtil\Simplifier +# End Source File +# Begin Source File + SOURCE=..\..\include\osgUtil\SmoothingVisitor # End Source File # Begin Source File diff --git a/include/osgUtil/Simplifier b/include/osgUtil/Simplifier new file mode 100644 index 000000000..63b906564 --- /dev/null +++ b/include/osgUtil/Simplifier @@ -0,0 +1,45 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield + * + * 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 + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSGUTIL_SIMPLIYFIER +#define OSGUTIL_SIMPLIYFIER 1 + +#include +#include +#include + +#include + +namespace osgUtil { + +/** A simplifier for reducing the number of traingles in osg::Geometry. + */ +class OSGUTIL_EXPORT Simplifier +{ + public: + + Simplifier(); + + /** simply the geometry to defined ratio of original size.*/ + void simplify(osg::Geometry& geometry, float sampleRatio); + + void simplify(osg::Geometry& geometry, unsigned int targetNumberOfTriangles); + + + protected: + +}; + +} + +#endif diff --git a/src/osgTerrain/DataSet.cpp b/src/osgTerrain/DataSet.cpp index ed6c9e578..41dc579cf 100644 --- a/src/osgTerrain/DataSet.cpp +++ b/src/osgTerrain/DataSet.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -2177,6 +2178,10 @@ osg::Node* DataSet::DestinationTile::createPolygonal() geometry->setColorBinding(osg::Geometry::BIND_OVERALL); } + osgUtil::Simplifier simplifier; + simplifier.simplify(*geometry,0.5f); // this will replace the normal vector with a new one + + osg::Geode* geode = new osg::Geode; geode->addDrawable(geometry); diff --git a/src/osgUtil/GNUmakefile b/src/osgUtil/GNUmakefile index 7294af3c4..b94bc5fc8 100644 --- a/src/osgUtil/GNUmakefile +++ b/src/osgUtil/GNUmakefile @@ -3,11 +3,13 @@ include $(TOPDIR)/Make/makedefs CXXFILES = \ - UpdateVisitor.cpp\ + CubeMapGenerator.cpp\ CullVisitor.cpp\ + DelaunayTriangulator.cpp\ DisplayListVisitor.cpp\ DisplayRequirementsVisitor.cpp\ - DelaunayTriangulator.cpp\ + HalfWayMapGenerator.cpp\ + HighlightMapGenerator.cpp\ InsertImpostorsVisitor.cpp\ IntersectVisitor.cpp\ Optimizer.cpp\ @@ -18,16 +20,15 @@ CXXFILES = \ RenderStageLighting.cpp\ RenderToTextureStage.cpp\ SceneView.cpp\ + Simplifier.cpp\ SmoothingVisitor.cpp\ TangentSpaceGenerator.cpp\ Tesselator.cpp\ - TransformCallback.cpp\ TransformAttributeFunctor.cpp\ - TriStripVisitor.cpp\ + TransformCallback.cpp\ TriStrip_tri_stripper.cpp\ - CubeMapGenerator.cpp\ - HalfWayMapGenerator.cpp\ - HighlightMapGenerator.cpp\ + TriStripVisitor.cpp\ + UpdateVisitor.cpp\ Version.cpp\ DEF += -DOSGUTIL_LIBRARY diff --git a/src/osgUtil/Simplifier.cpp b/src/osgUtil/Simplifier.cpp new file mode 100644 index 000000000..4692ced62 --- /dev/null +++ b/src/osgUtil/Simplifier.cpp @@ -0,0 +1,161 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield + * + * 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 + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include + +#include + +#include +#include + +using namespace osgUtil; + + +class EdgeCollapse +{ +public: + + EdgeCollapse(): + _targetNumTriangles(0) {} + ~EdgeCollapse() {} + + void setGeometry(osg::Geometry* geometry); + osg::Geometry* getGeometry() { return _geometry; } + + void setTargetNumOfTriangles(unsigned int num) { _targetNumTriangles = num; } + + unsigned int getNumOfTriangles() { return 0; } + + bool collapseMinimumErrorEdge() { return false; } + + void copyBackToGeometry() {} + + class Triangle; + class Edge; + + + struct Point : public osg::Referenced + { + Point() {} + + unsigned int _index; + }; + + struct Edge : public osg::Referenced + { + Edge() {} + + osg::ref_ptr _p1; + osg::ref_ptr _p2; + + osg::ref_ptr _t1; + osg::ref_ptr _t2; + + void setErrorMetric(float errorMetric) { _errorMetric = errorMetric; } + float getErrorMetric() const { return _errorMetric; } + + float _errorMetric; + }; + + struct Triangle : public osg::Referenced + { + Triangle() {} + + osg::ref_ptr _p1; + osg::ref_ptr _p2; + osg::ref_ptr _p3; + + osg::ref_ptr _e1; + osg::ref_ptr _e2; + osg::ref_ptr _e3; + }; + + + struct LessPtr + { + inline bool operator() (const osg::ref_ptr& lhs,const osg::ref_ptr& rhs) const + { + return lhs->getErrorMetric()getErrorMetric(); + } + }; + + typedef std::set,LessPtr> EdgeSet; + + + void addTriangle(unsigned int p1, unsigned int p2, unsigned int p3) + { + std::cout<<"addTriangle("<addTriangle(p1,p2,p3); + } + +}; + +typedef osg::TriangleIndexFunctor MyTriangleIndexFunctor; + +void EdgeCollapse::setGeometry(osg::Geometry* geometry) +{ + _geometry = geometry; + + MyTriangleIndexFunctor collectTriangles; + collectTriangles.setEdgeCollapse(this); +} + +Simplifier::Simplifier() +{ +} + +void Simplifier::simplify(osg::Geometry& geometry, float sampleRatio) +{ + EdgeCollapse ec; + ec.setGeometry(&geometry); + + ec.setTargetNumOfTriangles((unsigned int)(sampleRatio*(float)ec.getNumOfTriangles())); + + while (ec.collapseMinimumErrorEdge()) {} + + ec.copyBackToGeometry(); +} + +void Simplifier::simplify(osg::Geometry& geometry, unsigned int targetNumberOfTriangles) +{ + EdgeCollapse ec; + ec.setGeometry(&geometry); + + ec.setTargetNumOfTriangles(targetNumberOfTriangles); + + while (ec.collapseMinimumErrorEdge()) {} + + ec.copyBackToGeometry(); +}