Refactored the subsurface PolygonOffset code so that it uses sensible values and also moves management of the PolygonOffset and Depth attribute objects into the Document object rather than using static vars.

This commit is contained in:
Robert Osfield 2009-04-23 10:24:27 +00:00
parent 194b7a32e7
commit 9a072a4dc6
3 changed files with 35 additions and 39 deletions

View File

@ -45,6 +45,7 @@ Document::Document() :
_lightPointAnimationPoolParent(false), _lightPointAnimationPoolParent(false),
_shaderPoolParent(false) _shaderPoolParent(false)
{ {
_subsurfaceDepth = new osg::Depth(osg::Depth::LESS, 0.0, 1.0,false);
} }
Document::~Document() Document::~Document()
@ -110,6 +111,22 @@ osg::Node* Document::getInstanceDefinition(int no)
return NULL; return NULL;
} }
void Document::setSubSurfacePolygonOffset(int level, osg::PolygonOffset* po)
{
_subsurfacePolygonOffsets[level] = po;
}
osg::PolygonOffset* Document::getSubSurfacePolygonOffset(int level)
{
osg::notify(osg::DEBUG_INFO)<<"Document::getSubSurfacePolygonOffset("<<level<<")"<<std::endl;
osg::ref_ptr<osg::PolygonOffset>& po = _subsurfacePolygonOffsets[level];
if (!po)
{
po = new osg::PolygonOffset(-1.0f*float(level), -1.0f);
}
return po.get();
}
double flt::unitsToMeters(CoordUnits unit) double flt::unitsToMeters(CoordUnits unit)
{ {
switch (unit) switch (unit)

View File

@ -24,6 +24,8 @@
#include <osg/Notify> #include <osg/Notify>
#include <osg/Transform> #include <osg/Transform>
#include <osg/Geometry> #include <osg/Geometry>
#include <osg/PolygonOffset>
#include <osg/Depth>
#include <osgDB/ReaderWriter> #include <osgDB/ReaderWriter>
#include "Types.h" #include "Types.h"
@ -166,6 +168,12 @@ class Document
ShaderPool* getOrCreateShaderPool(); ShaderPool* getOrCreateShaderPool();
bool getShaderPoolParent() const { return _shaderPoolParent; } bool getShaderPoolParent() const { return _shaderPoolParent; }
void setSubSurfacePolygonOffset(int level, osg::PolygonOffset* po);
osg::PolygonOffset* getSubSurfacePolygonOffset(int level);
void setSubSurfaceDepth(osg::Depth* depth) { _subsurfaceDepth = depth; }
osg::Depth* getSubSurfaceDepth() { return _subsurfaceDepth.get(); }
// Options // Options
void setReplaceClampWithClampToEdge(bool flag) { _replaceClampWithClampToEdge = flag; } void setReplaceClampWithClampToEdge(bool flag) { _replaceClampWithClampToEdge = flag; }
@ -225,6 +233,11 @@ class Document
osg::ref_ptr<LightPointAppearancePool> _lightPointAppearancePool; osg::ref_ptr<LightPointAppearancePool> _lightPointAppearancePool;
osg::ref_ptr<LightPointAnimationPool> _lightPointAnimationPool; osg::ref_ptr<LightPointAnimationPool> _lightPointAnimationPool;
osg::ref_ptr<ShaderPool> _shaderPool; osg::ref_ptr<ShaderPool> _shaderPool;
typedef std::map<int, osg::ref_ptr<osg::PolygonOffset> > SubSurfacePolygonOffsets;
SubSurfacePolygonOffsets _subsurfacePolygonOffsets;
osg::ref_ptr<osg::Depth> _subsurfaceDepth;
bool _colorPoolParent; bool _colorPoolParent;
bool _texturePoolParent; bool _texturePoolParent;
bool _materialPoolParent; bool _materialPoolParent;

View File

@ -23,8 +23,6 @@
#include <osg/Geometry> #include <osg/Geometry>
#include <osg/Texture2D> #include <osg/Texture2D>
#include <osg/CullFace> #include <osg/CullFace>
#include <osg/PolygonOffset>
#include <osg/Depth>
#include <osg/BlendFunc> #include <osg/BlendFunc>
#include <osgUtil/TransformAttributeFunctor> #include <osgUtil/TransformAttributeFunctor>
#include "Registry.h" #include "Registry.h"
@ -383,28 +381,12 @@ protected:
// Subface // Subface
if (document.subfaceLevel() > 0) if (document.subfaceLevel() > 0)
{ {
static osg::ref_ptr<osg::PolygonOffset> polygonOffset = new osg::PolygonOffset(-10.0f, -40.0f); stateset->setAttributeAndModes(document.getSubSurfacePolygonOffset(document.subfaceLevel()), osg::StateAttribute::ON);
stateset->setAttributeAndModes(polygonOffset.get(), osg::StateAttribute::ON); stateset->setAttribute(document.getSubSurfaceDepth());
static osg::ref_ptr<osg::Depth> depth = new osg::Depth(osg::Depth::LESS, 0.0, 1.0,false);
stateset->setAttribute(depth.get());
stateset->setRenderBinDetails(document.subfaceLevel(),"RenderBin"); stateset->setRenderBinDetails(document.subfaceLevel(),"RenderBin");
} }
#if 0
// note from Robert Osfield, this "optimization" breaks multi-textured datasets that mix single texture
// and mulit-texture geometries as the Multitexture parsing can come after the below code, and accidentally
// polute the non multi-texture geometries StateSet.
// A simple share stateset optimization.
static osg::ref_ptr<osg::StateSet> lastStateset;
if (lastStateset.valid() && (stateset->compare(*lastStateset,false)==0))
stateset = lastStateset;
else
lastStateset = stateset;
#endif
_geode->setStateSet(stateset.get()); _geode->setStateSet(stateset.get());
// Add to parent. // Add to parent.
@ -952,28 +934,12 @@ protected:
// Subface // Subface
if (document.subfaceLevel() > 0) if (document.subfaceLevel() > 0)
{ {
static osg::ref_ptr<osg::PolygonOffset> polygonOffset = new osg::PolygonOffset(-10.0f, -40.0f); stateset->setAttributeAndModes(document.getSubSurfacePolygonOffset(document.subfaceLevel()), osg::StateAttribute::ON);
stateset->setAttributeAndModes(polygonOffset.get(), osg::StateAttribute::ON); stateset->setAttribute(document.getSubSurfaceDepth());
static osg::ref_ptr<osg::Depth> depth = new osg::Depth(osg::Depth::LESS, 0.0, 1.0,false);
stateset->setAttribute(depth.get());
stateset->setRenderBinDetails(document.subfaceLevel(),"RenderBin"); stateset->setRenderBinDetails(document.subfaceLevel(),"RenderBin");
} }
#if 0
// note from Robert Osfield, this "optimization" breaks multi-textured datasets that mix single texture
// and mulit-texture geometries as the Multitexture parsing can come after the below code, and accidentally
// polute the non multi-texture geometries StateSet.
// A simple share stateset optimization.
static osg::ref_ptr<osg::StateSet> lastStateset;
if (lastStateset.valid() && (stateset->compare(*lastStateset,false)==0))
stateset = lastStateset;
else
lastStateset = stateset;
#endif
_geode->setStateSet(stateset.get()); _geode->setStateSet(stateset.get());
// Add to parent. // Add to parent.