diff --git a/include/osg/AlphaFunc b/include/osg/AlphaFunc index 0fbf72e4c..a28be59aa 100644 --- a/include/osg/AlphaFunc +++ b/include/osg/AlphaFunc @@ -6,7 +6,6 @@ #define OSG_ALPHAFUNC 1 #include -#include namespace osg { @@ -41,9 +40,9 @@ class SG_EXPORT AlphaFunc : public StateAttribute return 0; // passed all the above comparison macro's, must be equal. } - virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const + virtual void getAssociatedModes(std::vector& modes) const { - ds.setMode(GL_ALPHA_TEST,value); + modes.push_back(GL_ALPHA_TEST); } enum ComparisonFunction { diff --git a/include/osg/ClipPlane b/include/osg/ClipPlane index 47eaa8547..fe85c9fb3 100644 --- a/include/osg/ClipPlane +++ b/include/osg/ClipPlane @@ -7,7 +7,6 @@ #include #include -#include namespace osg { @@ -51,9 +50,9 @@ class SG_EXPORT ClipPlane : public StateAttribute return 0; // passed all the above comparison macro's, must be equal. } - virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const + virtual void getAssociatedModes(std::vector& modes) const { - ds.setMode((GLMode)(GL_CLIP_PLANE0+_clipPlaneNum),value); + modes.push_back((GLMode)(GL_CLIP_PLANE0+_clipPlaneNum)); } diff --git a/include/osg/ColorMask b/include/osg/ColorMask index 062c1208a..7cb22bc06 100644 --- a/include/osg/ColorMask +++ b/include/osg/ColorMask @@ -7,7 +7,6 @@ #include #include -#include #include namespace osg { diff --git a/include/osg/CullFace b/include/osg/CullFace index ee14bcce7..02215698d 100644 --- a/include/osg/CullFace +++ b/include/osg/CullFace @@ -7,7 +7,6 @@ #include #include -#include namespace osg { @@ -39,9 +38,9 @@ class SG_EXPORT CullFace : public StateAttribute return 0; // passed all the above comparison macro's, must be equal. } - virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const + virtual void getAssociatedModes(std::vector& modes) const { - ds.setMode(GL_CULL_FACE,value); + modes.push_back(GL_CULL_FACE); } enum Mode { diff --git a/include/osg/Depth b/include/osg/Depth index bcd858d94..f9af7ad16 100644 --- a/include/osg/Depth +++ b/include/osg/Depth @@ -6,7 +6,6 @@ #define OSG_DEPTH 1 #include -#include #include namespace osg { @@ -47,9 +46,9 @@ class SG_EXPORT Depth : public StateAttribute return 0; // passed all the above comparison macro's, must be equal. } - virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const + virtual void getAssociatedModes(std::vector& modes) const { - ds.setMode(GL_DEPTH_TEST,value); + modes.push_back(GL_DEPTH_TEST); } enum Function diff --git a/include/osg/Drawable b/include/osg/Drawable index a900ea5d0..6a55e8113 100644 --- a/include/osg/Drawable +++ b/include/osg/Drawable @@ -6,7 +6,6 @@ #define OSG_DRAWABLE 1 #include -#include #include #include #include diff --git a/include/osg/Fog b/include/osg/Fog index a00ffdf98..3fd96aa0e 100644 --- a/include/osg/Fog +++ b/include/osg/Fog @@ -6,7 +6,6 @@ #define OSG_FOG 1 #include -#include #include #include @@ -48,9 +47,9 @@ class SG_EXPORT Fog : public StateAttribute return 0; // passed all the above comparison macro's, must be equal. } - virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const + virtual void getAssociatedModes(std::vector& modes) const { - ds.setMode(GL_FOG,value); + modes.push_back(GL_FOG); } enum Mode { diff --git a/include/osg/Light b/include/osg/Light index 9b8495f68..5b00df74d 100644 --- a/include/osg/Light +++ b/include/osg/Light @@ -6,7 +6,6 @@ #define OSG_LIGHT 1 #include -#include #include #include @@ -59,9 +58,9 @@ class SG_EXPORT Light : public StateAttribute return 0; // passed all the above comparison macro's, must be equal. } - virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const + virtual void getAssociatedModes(std::vector& modes) const { - ds.setMode(GL_LIGHT0+_lightnum,value); + modes.push_back(GL_LIGHT0+_lightnum); } /** Set which OpenGL light to operate on.*/ diff --git a/include/osg/LightModel b/include/osg/LightModel index 8c40ae923..f6a1bd5ea 100644 --- a/include/osg/LightModel +++ b/include/osg/LightModel @@ -6,7 +6,6 @@ #define OSG_LIGHTMODEL 1 #include -#include #include namespace osg { diff --git a/include/osg/LineStipple b/include/osg/LineStipple index 6381161d5..6a3ea3a90 100644 --- a/include/osg/LineStipple +++ b/include/osg/LineStipple @@ -6,7 +6,6 @@ #define OSG_LINESTIPPLE 1 #include -#include namespace osg { @@ -38,12 +37,11 @@ class SG_EXPORT LineStipple : public StateAttribute return 0; // passed all the above comparison macro's, must be equal. } - virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const + virtual void getAssociatedModes(std::vector& modes) const { - ds.setMode(GL_LINE_STIPPLE,value); + modes.push_back(GL_LINE_STIPPLE); } - void setFactor(const int factor); inline const int getFactor() const { return _factor; } diff --git a/include/osg/LineWidth b/include/osg/LineWidth index e49fc2523..f0da5b3ed 100644 --- a/include/osg/LineWidth +++ b/include/osg/LineWidth @@ -6,7 +6,6 @@ #define OSG_LineWidth 1 #include -#include namespace osg { diff --git a/include/osg/Material b/include/osg/Material index 94d00f361..fe159f1d2 100644 --- a/include/osg/Material +++ b/include/osg/Material @@ -7,7 +7,6 @@ #include #include -#include namespace osg { /** Material - encapsulates OpenGL glMaterial state.*/ @@ -69,12 +68,12 @@ class SG_EXPORT Material : public StateAttribute return 0; // passed all the above comparison macro's, must be equal. } - virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const + virtual void getAssociatedModes(std::vector& modes) const { // Have to think about the role of _colorMode // in setting the colormaterial... also need to take the // color material enable/disable out of the the apply()... - ds.setMode(GL_COLOR_MATERIAL,value); + modes.push_back(GL_COLOR_MATERIAL); } virtual void apply(State& state) const; diff --git a/include/osg/Point b/include/osg/Point index 5e59189ef..132d43dc0 100644 --- a/include/osg/Point +++ b/include/osg/Point @@ -7,7 +7,6 @@ #include #include -#include namespace osg { @@ -42,9 +41,9 @@ class SG_EXPORT Point : public StateAttribute return 0; // passed all the above comparison macro's, must be equal. } - virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const + virtual void getAssociatedModes(std::vector& modes) const { - ds.setMode(GL_POINT_SMOOTH,value); + modes.push_back(GL_POINT_SMOOTH); } void setSize(const float size); diff --git a/include/osg/PolygonOffset b/include/osg/PolygonOffset index 441e938fa..fce8b221f 100644 --- a/include/osg/PolygonOffset +++ b/include/osg/PolygonOffset @@ -6,7 +6,6 @@ #define OSG_POLYGONOFFSET 1 #include -#include namespace osg { @@ -39,11 +38,11 @@ class SG_EXPORT PolygonOffset : public StateAttribute return 0; // passed all the above comparison macro's, must be equal. } - virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const + virtual void getAssociatedModes(std::vector& modes) const { - ds.setMode(GL_POLYGON_OFFSET_FILL,value); - ds.setMode(GL_POLYGON_OFFSET_LINE,value); - ds.setMode(GL_POLYGON_OFFSET_POINT,value); + modes.push_back(GL_POLYGON_OFFSET_FILL); + modes.push_back(GL_POLYGON_OFFSET_LINE); + modes.push_back(GL_POLYGON_OFFSET_POINT); } inline void setFactor(const float factor) { _factor = factor; } diff --git a/include/osg/ShadeModel b/include/osg/ShadeModel index 7729200a9..bbbee793b 100644 --- a/include/osg/ShadeModel +++ b/include/osg/ShadeModel @@ -7,7 +7,6 @@ #include #include -#include namespace osg { diff --git a/include/osg/State b/include/osg/State index c501a1681..c006719cb 100644 --- a/include/osg/State +++ b/include/osg/State @@ -120,11 +120,9 @@ class SG_EXPORT State : public Referenced inline const bool applyTextureMode(unsigned int unit, const StateAttribute::GLMode mode,const bool enabled) { - if (unit>=_textureModeMapList.size()) _textureModeMapList.resize(unit); - ModeMap& modeMap = _textureModeMapList[unit]; - + ModeMap& modeMap = getOrCreateTextureModeMap(unit); ModeStack& ms = modeMap[mode]; - ms.changed = true; + ms.changed = true; return applyMode(mode,enabled,ms); } @@ -138,9 +136,7 @@ class SG_EXPORT State : public Referenced inline const bool applyTextureAttribute(unsigned int unit, const StateAttribute* attribute) { - if (unit>=_textureAttributeMapList.size()) _textureAttributeMapList.resize(unit); - AttributeMap& attributeMap = _textureAttributeMapList[unit]; - + AttributeMap& attributeMap = getOrCreateTextureAttributeMap(unit); AttributeStack& as = attributeMap[attribute->getType()]; as.changed = true; return applyAttribute(attribute,as); @@ -505,6 +501,7 @@ class SG_EXPORT State : public Referenced return false; } + typedef std::map ModeMap; typedef std::vector TextureModeMapList; @@ -516,10 +513,10 @@ class SG_EXPORT State : public Referenced ModeMap _modeMap; AttributeMap _attributeMap; - + TextureModeMapList _textureModeMapList; TextureAttributeMapList _textureAttributeMapList; - + StateSetStack _drawStateStack; struct EnabledArrayPair @@ -543,8 +540,419 @@ class SG_EXPORT State : public Referenced unsigned int _currentActiveTextureUnit; unsigned int _currentClientActiveTextureUnit; + inline ModeMap& getOrCreateTextureModeMap(unsigned int unit) + { + if (unit>=_textureModeMapList.size()) _textureModeMapList.resize(unit+1); + return _textureModeMapList[unit]; + } + + + inline AttributeMap& getOrCreateTextureAttributeMap(unsigned int unit) + { + if (unit>=_textureAttributeMapList.size()) _textureAttributeMapList.resize(unit+1); + return _textureAttributeMapList[unit]; + } + + inline void pushModeList(ModeMap& modeMap,const StateSet::ModeList& modeList); + inline void pushAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList); + + inline void popModeList(ModeMap& modeMap,const StateSet::ModeList& modeList); + inline void popAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList); + + inline void applyModeList(ModeMap& modeMap,const StateSet::ModeList& modeList); + inline void applyAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList); + + inline void applyModeMap(ModeMap& modeMap); + inline void applyAttributeMap(AttributeMap& attributeMap); + + void haveAppliedMode(ModeMap& modeMap,const StateAttribute::GLMode mode,const StateAttribute::GLModeValue value); + void haveAppliedMode(ModeMap& modeMap,const StateAttribute::GLMode mode); + void haveAppliedAttribute(AttributeMap& attributeMap,const StateAttribute* attribute); + void haveAppliedAttribute(AttributeMap& attributeMap,const StateAttribute::Type type); + const bool getLastAppliedMode(const ModeMap& modeMap,const StateAttribute::GLMode mode) const; + const StateAttribute* getLastAppliedAttribute(const AttributeMap& attributeMap,const StateAttribute::Type type) const; + }; +inline void State::pushModeList(ModeMap& modeMap,const StateSet::ModeList& modeList) +{ + for(StateSet::ModeList::const_iterator mitr=modeList.begin(); + mitr!=modeList.end(); + ++mitr) + { + // get the mode stack for incomming GLmode {mitr->first}. + ModeStack& ms = modeMap[mitr->first]; + if (ms.valueVec.empty()) + { + // first pair so simply push incomming pair to back. + ms.valueVec.push_back(mitr->second); + } + else if (ms.valueVec.back() & StateAttribute::OVERRIDE) // check the existing override flag + { + // push existing back since override keeps the previoius value. + ms.valueVec.push_back(ms.valueVec.back()); + } + else + { + // no override on so simply push incomming pair to back. + ms.valueVec.push_back(mitr->second); + } + ms.changed = true; + } +} + +inline void State::pushAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList) +{ + for(StateSet::AttributeList::const_iterator aitr=attributeList.begin(); + aitr!=attributeList.end(); + ++aitr) + { + // get the attribute stack for incomming type {aitr->first}. + AttributeStack& as = attributeMap[aitr->first]; + if (as.attributeVec.empty()) + { + // first pair so simply push incomming pair to back. + as.attributeVec.push_back( + AttributePair(aitr->second.first.get(),aitr->second.second)); + } + else if (as.attributeVec.back().second & StateAttribute::OVERRIDE) // check the existing override flag + { + // push existing back since override keeps the previoius value. + as.attributeVec.push_back(as.attributeVec.back()); + } + else + { + // no override on so simply push incomming pair to back. + as.attributeVec.push_back( + AttributePair(aitr->second.first.get(),aitr->second.second)); + } + as.changed = true; + } +} + +inline void State::popModeList(ModeMap& modeMap,const StateSet::ModeList& modeList) +{ + for(StateSet::ModeList::const_iterator mitr=modeList.begin(); + mitr!=modeList.end(); + ++mitr) + { + // get the mode stack for incomming GLmode {mitr->first}. + ModeStack& ms = modeMap[mitr->first]; + if (!ms.valueVec.empty()) + { + ms.valueVec.pop_back(); + } + ms.changed = true; + } +} + +inline void State::popAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList) +{ + for(StateSet::AttributeList::const_iterator aitr=attributeList.begin(); + aitr!=attributeList.end(); + ++aitr) + { + // get the attribute stack for incomming type {aitr->first}. + AttributeStack& as = attributeMap[aitr->first]; + if (!as.attributeVec.empty()) + { + as.attributeVec.pop_back(); + } + as.changed = true; + } +} + +inline void State::applyModeList(ModeMap& modeMap,const StateSet::ModeList& modeList) +{ + StateSet::ModeList::const_iterator ds_mitr = modeList.begin(); + ModeMap::iterator this_mitr=modeMap.begin(); + + while (this_mitr!=modeMap.end() && ds_mitr!=modeList.end()) + { + if (this_mitr->firstfirst) + { + + // note GLMode = this_mitr->first + ModeStack& ms = this_mitr->second; + if (ms.changed) + { + ms.changed = false; + if (!ms.valueVec.empty()) + { + bool new_value = ms.valueVec.back() & StateAttribute::ON; + applyMode(this_mitr->first,new_value,ms); + } + else + { + // assume default of disabled. + applyMode(this_mitr->first,ms.global_default_value,ms); + + } + + } + + ++this_mitr; + + } + else if (ds_mitr->firstfirst) + { + + // ds_mitr->first is a new mode, therefore + // need to insert a new mode entry for ds_mistr->first. + ModeStack& ms = modeMap[ds_mitr->first]; + + bool new_value = ds_mitr->second & StateAttribute::ON; + applyMode(ds_mitr->first,new_value,ms); + + // will need to disable this mode on next apply so set it to changed. + ms.changed = true; + + ++ds_mitr; + + } + else + { + // this_mitr & ds_mitr refer to the same mode, check the overide + // if any otherwise just apply the incomming mode. + + ModeStack& ms = this_mitr->second; + + if (!ms.valueVec.empty() && ms.valueVec.back() & StateAttribute::OVERRIDE) + { + // override is on, there just treat as a normal apply on modes. + + if (ms.changed) + { + ms.changed = false; + bool new_value = ms.valueVec.back() & StateAttribute::ON; + applyMode(this_mitr->first,new_value,ms); + + } + } + else + { + // no override on or no previous entry, therefore consider incomming mode. + bool new_value = ds_mitr->second & StateAttribute::ON; + if (applyMode(ds_mitr->first,new_value,ms)) + { + ms.changed = true; + } + } + + ++this_mitr; + ++ds_mitr; + } + } + + // iterator over the remaining state modes to apply any previous changes. + for(; + this_mitr!=modeMap.end(); + ++this_mitr) + { + // note GLMode = this_mitr->first + ModeStack& ms = this_mitr->second; + if (ms.changed) + { + ms.changed = false; + if (!ms.valueVec.empty()) + { + bool new_value = ms.valueVec.back() & StateAttribute::ON; + applyMode(this_mitr->first,new_value,ms); + } + else + { + // assume default of disabled. + applyMode(this_mitr->first,ms.global_default_value,ms); + + } + + } + } + + // iterator over the remaining incomming modes to apply any new mode. + for(; + ds_mitr!=modeList.end(); + ++ds_mitr) + { + ModeStack& ms = modeMap[ds_mitr->first]; + + bool new_value = ds_mitr->second & StateAttribute::ON; + applyMode(ds_mitr->first,new_value,ms); + + // will need to disable this mode on next apply so set it to changed. + ms.changed = true; + } +} + +inline void State::applyAttributeList(AttributeMap& attributeMap,const StateSet::AttributeList& attributeList) +{ + StateSet::AttributeList::const_iterator ds_aitr=attributeList.begin(); + + AttributeMap::iterator this_aitr=attributeMap.begin(); + + while (this_aitr!=attributeMap.end() && ds_aitr!=attributeList.end()) + { + if (this_aitr->firstfirst) + { + + // note attribute type = this_aitr->first + AttributeStack& as = this_aitr->second; + if (as.changed) + { + as.changed = false; + if (!as.attributeVec.empty()) + { + const StateAttribute* new_attr = as.attributeVec.back().first; + applyAttribute(new_attr,as); + } + else + { + applyGlobalDefaultAttribute(as); + } + } + + ++this_aitr; + + } + else if (ds_aitr->firstfirst) + { + + // ds_mitr->first is a new attribute, therefore + // need to insert a new attribute entry for ds_aistr->first. + AttributeStack& as = attributeMap[ds_aitr->first]; + + const StateAttribute* new_attr = ds_aitr->second.first.get(); + applyAttribute(new_attr,as); + + // will need to disable this mode on next apply so set it to changed. + as.changed = true; + + ++ds_aitr; + + } + else + { + // this_mitr & ds_mitr refer to the same mode, check the overide + // if any otherwise just apply the incomming mode. + + AttributeStack& as = this_aitr->second; + + if (!as.attributeVec.empty() && as.attributeVec.back().second) + { + // override is os, there just treat as a normal apply on modes. + + if (as.changed) + { + as.changed = false; + const StateAttribute* new_attr = as.attributeVec.back().first; + applyAttribute(new_attr,as); + } + } + else + { + // no override on or no previous entry, therefore consider incomming mode. + const StateAttribute* new_attr = ds_aitr->second.first.get(); + if (applyAttribute(new_attr,as)) + { + as.changed = true; + } + } + + ++this_aitr; + ++ds_aitr; + } + } + + // iterator over the remaining state modes to apply any previous changes. + for(; + this_aitr!=attributeMap.end(); + ++this_aitr) + { + // note attribute type = this_aitr->first + AttributeStack& as = this_aitr->second; + if (as.changed) + { + as.changed = false; + if (!as.attributeVec.empty()) + { + const StateAttribute* new_attr = as.attributeVec.back().first; + applyAttribute(new_attr,as); + } + else + { + applyGlobalDefaultAttribute(as); + } + } + } + + // iterator over the remaining incomming modes to apply any new mode. + for(; + ds_aitr!=attributeList.end(); + ++ds_aitr) + { + // ds_mitr->first is a new attribute, therefore + // need to insert a new attribute entry for ds_aistr->first. + AttributeStack& as = attributeMap[ds_aitr->first]; + + const StateAttribute* new_attr = ds_aitr->second.first.get(); + applyAttribute(new_attr,as); + + // will need to update this attribute on next apply so set it to changed. + as.changed = true; + } + +} + +inline void State::applyModeMap(ModeMap& modeMap) +{ + for(ModeMap::iterator mitr=modeMap.begin(); + mitr!=modeMap.end(); + ++mitr) + { + // note GLMode = mitr->first + ModeStack& ms = mitr->second; + if (ms.changed) + { + ms.changed = false; + if (!ms.valueVec.empty()) + { + bool new_value = ms.valueVec.back() & StateAttribute::ON; + applyMode(mitr->first,new_value,ms); + } + else + { + // assume default of disabled. + applyMode(mitr->first,ms.global_default_value,ms); + } + + } + } +} + +inline void State::applyAttributeMap(AttributeMap& attributeMap) +{ + for(AttributeMap::iterator aitr=attributeMap.begin(); + aitr!=attributeMap.end(); + ++aitr) + { + AttributeStack& as = aitr->second; + if (as.changed) + { + as.changed = false; + if (!as.attributeVec.empty()) + { + const StateAttribute* new_attr = as.attributeVec.back().first; + applyAttribute(new_attr,as); + } + else + { + applyGlobalDefaultAttribute(as); + } + + } + } +} + } #endif diff --git a/include/osg/StateAttribute b/include/osg/StateAttribute index c9b37073e..c78708220 100644 --- a/include/osg/StateAttribute +++ b/include/osg/StateAttribute @@ -10,6 +10,7 @@ #include #include +#include namespace osg { @@ -174,6 +175,8 @@ class SG_EXPORT StateAttribute : public Object /** return the Type identifier of the attribute's class type.*/ virtual const Type getType() const = 0; + /** return true if StateAttribute is a type which controls texturing and needs to be issued w.r.t to specific texture unit.*/ + virtual bool isTextureAttribute() const { return false; } /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/ virtual int compare(const StateAttribute& sa) const = 0; @@ -181,9 +184,10 @@ class SG_EXPORT StateAttribute : public Object bool operator < (const StateAttribute& rhs) const { return compare(rhs)<0; } bool operator == (const StateAttribute& rhs) const { return compare(rhs)==0; } bool operator != (const StateAttribute& rhs) const { return compare(rhs)!=0; } - - virtual void setStateSetModes(StateSet&,const GLModeValue) const + + /** return the modes associated with this StateSet*/ + virtual void getAssociatedModes(std::vector& /*modes*/) const { // default to no GLMode's associated with use of the StateAttribute. } diff --git a/include/osg/StateSet b/include/osg/StateSet index 2b7b24310..5227a4e19 100644 --- a/include/osg/StateSet +++ b/include/osg/StateSet @@ -157,7 +157,9 @@ class SG_EXPORT StateSet : public Object inline const TextureAttributeList& getTextureAttributeList() const { return _textureAttributeList; } - + void setAssociatedModes(const StateAttribute* attribute, const StateAttribute::GLModeValue value); + + void setAssociatedTextureModes(unsigned int unit,const StateAttribute* attribute, const StateAttribute::GLModeValue value); enum RenderingHint { @@ -230,6 +232,21 @@ class SG_EXPORT StateSet : public Object if (unit>=_textureAttributeList.size()) _textureAttributeList.resize(unit+1); return _textureAttributeList[unit]; } + + int compareModes(const ModeList& lhs,const ModeList& rhs); + int compareAttributePtrs(const AttributeList& lhs,const AttributeList& rhs); + int compareAttributeContents(const AttributeList& lhs,const AttributeList& rhs); + + void setMode(ModeList& modeList,const StateAttribute::GLMode mode, const StateAttribute::GLModeValue value); + void setModeToInherit(ModeList& modeList,const StateAttribute::GLMode mode); + const StateAttribute::GLModeValue getMode(const ModeList& modeList,const StateAttribute::GLMode mode) const; + void setAssociatedModes(ModeList& modeList,const StateAttribute* attribute, const StateAttribute::GLModeValue value); + + void setAttribute(AttributeList& attributeList,StateAttribute *attribute, const StateAttribute::OverrideValue value=StateAttribute::OFF); + + StateAttribute* getAttribute(AttributeList& attributeList,const StateAttribute::Type type); + const StateAttribute* getAttribute(const AttributeList& attributeList,const StateAttribute::Type type) const; + const RefAttributePair* getAttributePair(const AttributeList& attributeList,const StateAttribute::Type type) const; int _renderingHint; diff --git a/include/osg/Stencil b/include/osg/Stencil index 31ad36630..3605d7d3a 100644 --- a/include/osg/Stencil +++ b/include/osg/Stencil @@ -6,7 +6,6 @@ #define OSG_STENCIL 1 #include -#include #include namespace osg { @@ -53,9 +52,9 @@ class SG_EXPORT Stencil : public StateAttribute return 0; // passed all the above comparison macro's, must be equal. } - virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const + virtual void getAssociatedModes(std::vector& modes) const { - ds.setMode(GL_STENCIL_TEST,value); + modes.push_back(GL_STENCIL_TEST); } enum Function diff --git a/include/osg/TexEnv b/include/osg/TexEnv index b45dcea3a..1e91ae5ff 100644 --- a/include/osg/TexEnv +++ b/include/osg/TexEnv @@ -25,6 +25,8 @@ class SG_EXPORT TexEnv : public StateAttribute META_StateAttribute(osg, TexEnv, TEXENV); + virtual bool isTextureAttribute() const { return true; } + /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/ virtual int compare(const StateAttribute& sa) const { diff --git a/include/osg/TexGen b/include/osg/TexGen index 53dda2db8..bb6e25dcd 100644 --- a/include/osg/TexGen +++ b/include/osg/TexGen @@ -7,7 +7,6 @@ #include #include -#include namespace osg { @@ -37,6 +36,8 @@ class SG_EXPORT TexGen : public StateAttribute META_StateAttribute(osg, TexGen, TEXGEN); + virtual bool isTextureAttribute() const { return true; } + /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/ virtual int compare(const StateAttribute& sa) const { @@ -54,10 +55,10 @@ class SG_EXPORT TexGen : public StateAttribute return 0; // passed all the above comparison macro's, must be equal. } - virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const + virtual void getAssociatedModes(std::vector& modes) const { - ds.setMode(GL_TEXTURE_GEN_S,value); - ds.setMode(GL_TEXTURE_GEN_T,value); + modes.push_back(GL_TEXTURE_GEN_S); + modes.push_back(GL_TEXTURE_GEN_T); // Not happy with turning all tex gen paramters on // as the OSG currently only supports 2D textures and therefore @@ -69,9 +70,8 @@ class SG_EXPORT TexGen : public StateAttribute // The tidy up is now happening, but will have a think before // resolving the below parameters. - // ds.setMode(GL_TEXTURE_GEN_R,value); - // ds.setMode(GL_TEXTURE_GEN_Q,value); - + modes.push_back(GL_TEXTURE_GEN_R); + modes.push_back(GL_TEXTURE_GEN_Q); } virtual void apply(State& state) const; diff --git a/include/osg/TexMat b/include/osg/TexMat index b804692f8..c960cec02 100644 --- a/include/osg/TexMat +++ b/include/osg/TexMat @@ -24,6 +24,8 @@ class SG_EXPORT TexMat : public StateAttribute META_StateAttribute(osg, TexMat, TEXMAT); + virtual bool isTextureAttribute() const { return true; } + /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/ virtual int compare(const StateAttribute& sa) const { diff --git a/include/osg/Texture b/include/osg/Texture index 9d2aab0f7..aa63ccebd 100644 --- a/include/osg/Texture +++ b/include/osg/Texture @@ -11,7 +11,6 @@ #include #include #include -#include #include #include @@ -103,12 +102,14 @@ class SG_EXPORT Texture : public StateAttribute META_StateAttribute(osg, Texture,TEXTURE); + virtual bool isTextureAttribute() const { return true; } + /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/ virtual int compare(const StateAttribute& rhs) const; - virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const + virtual void getAssociatedModes(std::vector& modes) const { - ds.setMode(_target,value); + modes.push_back(_target); } /** Set the texture image. */ diff --git a/include/osg/Transparency b/include/osg/Transparency index 2ccb7bda5..bb8afd601 100644 --- a/include/osg/Transparency +++ b/include/osg/Transparency @@ -6,7 +6,6 @@ #define OSG_TRANSPARENCY 1 #include -#include namespace osg { @@ -39,9 +38,9 @@ class SG_EXPORT Transparency : public StateAttribute return 0; // passed all the above comparison macro's, must be equal. } - virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const + virtual void getAssociatedModes(std::vector& modes) const { - ds.setMode(GL_BLEND,value); + modes.push_back(GL_BLEND); } enum TransparencyMode { diff --git a/include/osg/Viewport b/include/osg/Viewport index e481d9a3b..a7132ced7 100644 --- a/include/osg/Viewport +++ b/include/osg/Viewport @@ -6,7 +6,6 @@ #define OSG_VIEWPORT 1 #include -#include #include #include diff --git a/src/osg/ClipNode.cpp b/src/osg/ClipNode.cpp index c478f078a..59824977d 100644 --- a/src/osg/ClipNode.cpp +++ b/src/osg/ClipNode.cpp @@ -102,7 +102,7 @@ void ClipNode::setStateSetModes(StateSet& stateset,const StateAttribute::GLModeV itr!=_planes.end(); ++itr) { - (*itr)->setStateSetModes(stateset,value); + stateset.setAssociatedModes(itr->get(),value); } } diff --git a/src/osg/GeoSet.cpp b/src/osg/GeoSet.cpp index 98f0c9c67..3aa37bd82 100644 --- a/src/osg/GeoSet.cpp +++ b/src/osg/GeoSet.cpp @@ -419,6 +419,8 @@ void GeoSet::computeNumVerts() const const bool GeoSet::computeBound() const { + _bbox.init(); + if( _iaformat == IA_OFF && _coords == (Vec3 *)0 ) return false; if( _numcoords == 0 ) @@ -430,7 +432,6 @@ const bool GeoSet::computeBound() const return false; Vec3 center(0.0f,0.0f,0.0f); - _bbox.init(); int i; if( _iaformat == IA_OFF ) @@ -1005,7 +1006,7 @@ void GeoSet::applyPrimitiveOperation(PrimitiveFunctor& functor) if (_cindex._is_ushort) functor.drawElements( (GLenum)_oglprimtype, _primLengths[i],&_cindex._ptr._ushort[index] ); else - functor.drawElements( (GLenum)_oglprimtype, _primLengths[i],&_cindex._ptr._ushort[index] ); + functor.drawElements( (GLenum)_oglprimtype, _primLengths[i],&_cindex._ptr._uint[index] ); } else functor.drawArrays( (GLenum)_oglprimtype, index, _primLengths[i] ); diff --git a/src/osg/LightSource.cpp b/src/osg/LightSource.cpp index 762291175..e15618e14 100644 --- a/src/osg/LightSource.cpp +++ b/src/osg/LightSource.cpp @@ -29,7 +29,7 @@ void LightSource::setStateSetModes(StateSet& stateset,const StateAttribute::GLMo { if (_light.valid()) { - _light->setStateSetModes(stateset,value); + stateset.setAssociatedModes(_light.get(),value); } } diff --git a/src/osg/State.cpp b/src/osg/State.cpp index e74e1085a..ef43fef2f 100644 --- a/src/osg/State.cpp +++ b/src/osg/State.cpp @@ -35,7 +35,8 @@ void State::reset() _modeMap[GL_DEPTH_TEST].global_default_value = true; _modeMap[GL_DEPTH_TEST].changed = true; - // go through all active StateAttribute's, applying where appropriate. + // go through all active StateAttribute's, setting to change to force update, + // the idea is to leave only the global defaults left. for(AttributeMap::iterator aitr=_attributeMap.begin(); aitr!=_attributeMap.end(); ++aitr) @@ -44,9 +45,33 @@ void State::reset() as.attributeVec.clear(); as.last_applied_attribute = NULL; as.changed = true; - } + } + + // we can do a straight clear, we arn't intrested in GL_DEPTH_TEST defaults in texture modes. + for(TextureModeMapList::iterator tmmItr=_textureModeMapList.begin(); + tmmItr!=_textureModeMapList.end(); + ++tmmItr) + { + tmmItr->clear(); + } -// _attributeMap.clear(); + // empty all the texture attributes as per normal attributes, leaving only the global defaults left. + for(TextureAttributeMapList::iterator tamItr=_textureAttributeMapList.begin(); + tamItr!=_textureAttributeMapList.end(); + ++tamItr) + { + AttributeMap& attributeMap = *tamItr; + // go through all active StateAttribute's, setting to change to force update. + for(AttributeMap::iterator aitr=attributeMap.begin(); + aitr!=attributeMap.end(); + ++aitr) + { + AttributeStack& as = aitr->second; + as.attributeVec.clear(); + as.last_applied_attribute = NULL; + as.changed = true; + } + } _drawStateStack.clear(); @@ -59,62 +84,24 @@ void State::pushStateSet(const StateSet* dstate) _drawStateStack.push_back(dstate); if (dstate) { - // iterator through all OpenGL modes in incomming StateSet - // for each GLMode entry push it to the back of the appropriate - // mode stack, taking into consideration current override status. - const StateSet::ModeList& ds_modeList = dstate->getModeList(); - for(StateSet::ModeList::const_iterator mitr=ds_modeList.begin(); - mitr!=ds_modeList.end(); - ++mitr) + + pushModeList(_modeMap,dstate->getModeList()); + + // iterator through texture modes. + unsigned int unit; + const StateSet::TextureModeList& ds_textureModeList = dstate->getTextureModeList(); + for(unit=0;unitfirst}. - ModeStack& ms = _modeMap[mitr->first]; - if (ms.valueVec.empty()) - { - // first pair so simply push incomming pair to back. - ms.valueVec.push_back(mitr->second); - } - else if (ms.valueVec.back() & StateAttribute::OVERRIDE) // check the existing override flag - { - // push existing back since override keeps the previoius value. - ms.valueVec.push_back(ms.valueVec.back()); - } - else - { - // no override on so simply push incomming pair to back. - ms.valueVec.push_back(mitr->second); - } - ms.changed = true; + pushModeList(getOrCreateTextureModeMap(unit),ds_textureModeList[unit]); } - - // iterator through all StateAttribute's in incomming StateSet - // for each Type entry push it to the back of the appropriate - // attribute stack, taking into consideration current override status. - const StateSet::AttributeList& ds_attributeList = dstate->getAttributeList(); - for(StateSet::AttributeList::const_iterator aitr=ds_attributeList.begin(); - aitr!=ds_attributeList.end(); - ++aitr) + + pushAttributeList(_attributeMap,dstate->getAttributeList()); + + // iterator through texture attributes. + const StateSet::TextureAttributeList& ds_textureAttributeList = dstate->getTextureAttributeList(); + for(unit=0;unitfirst}. - AttributeStack& as = _attributeMap[aitr->first]; - if (as.attributeVec.empty()) - { - // first pair so simply push incomming pair to back. - as.attributeVec.push_back( - AttributePair(aitr->second.first.get(),aitr->second.second)); - } - else if (as.attributeVec.back().second & StateAttribute::OVERRIDE) // check the existing override flag - { - // push existing back since override keeps the previoius value. - as.attributeVec.push_back(as.attributeVec.back()); - } - else - { - // no override on so simply push incomming pair to back. - as.attributeVec.push_back( - AttributePair(aitr->second.first.get(),aitr->second.second)); - } - as.changed = true; + pushAttributeList(getOrCreateTextureAttributeMap(unit),ds_textureAttributeList[unit]); } } @@ -129,38 +116,23 @@ void State::popStateSet() if (dstate) { - // iterator through all OpenGL modes in incomming StateSet - // for each GLMode entry pop_back of the appropriate - // mode stack. - const StateSet::ModeList& ds_modeList = dstate->getModeList(); - for(StateSet::ModeList::const_iterator mitr=ds_modeList.begin(); - mitr!=ds_modeList.end(); - ++mitr) + popModeList(_modeMap,dstate->getModeList()); + + // iterator through texture modes. + unsigned int unit; + const StateSet::TextureModeList& ds_textureModeList = dstate->getTextureModeList(); + for(unit=0;unitfirst}. - ModeStack& ms = _modeMap[mitr->first]; - if (!ms.valueVec.empty()) - { - ms.valueVec.pop_back(); - } - ms.changed = true; + popModeList(getOrCreateTextureModeMap(unit),ds_textureModeList[unit]); } - - // iterator through all StateAttribute's in incomming StateSet - // for each Type entry pop_back of the appropriate - // attribute stack. - const StateSet::AttributeList& ds_attributeList = dstate->getAttributeList(); - for(StateSet::AttributeList::const_iterator aitr=ds_attributeList.begin(); - aitr!=ds_attributeList.end(); - ++aitr) + + popAttributeList(_attributeMap,dstate->getAttributeList()); + + // iterator through texture attributes. + const StateSet::TextureAttributeList& ds_textureAttributeList = dstate->getTextureAttributeList(); + for(unit=0;unitfirst}. - AttributeStack& as = _attributeMap[aitr->first]; - if (!as.attributeVec.empty()) - { - as.attributeVec.pop_back(); - } - as.changed = true; + popAttributeList(getOrCreateTextureAttributeMap(unit),ds_textureAttributeList[unit]); } } @@ -209,253 +181,28 @@ void State::apply(const StateSet* dstate) if (dstate) { - // then handle mode changes. + applyModeList(_modeMap,dstate->getModeList()); + applyAttributeList(_attributeMap,dstate->getAttributeList()); + + const StateSet::TextureModeList& ds_textureModeList = dstate->getTextureModeList(); + const StateSet::TextureAttributeList& ds_textureAttributeList = dstate->getTextureAttributeList(); + + unsigned int unit; + unsigned int unitMax = std::max(ds_textureModeList.size(),ds_textureAttributeList.size()); + unitMax = std::max(unitMax,_textureModeMapList.size()); + unitMax = std::max(unitMax,_textureAttributeMapList.size()); + for(unit=0;unitgetModeList(); - - StateSet::ModeList::const_iterator ds_mitr = ds_modeList.begin(); - ModeMap::iterator this_mitr=_modeMap.begin(); - - while (this_mitr!=_modeMap.end() && ds_mitr!=ds_modeList.end()) + if (setActiveTextureUnit(unit)) { - if (this_mitr->firstfirst) - { - - // note GLMode = this_mitr->first - ModeStack& ms = this_mitr->second; - if (ms.changed) - { - ms.changed = false; - if (!ms.valueVec.empty()) - { - bool new_value = ms.valueVec.back() & StateAttribute::ON; - applyMode(this_mitr->first,new_value,ms); - } - else - { - // assume default of disabled. - applyMode(this_mitr->first,ms.global_default_value,ms); - - } - - } - - ++this_mitr; - - } - else if (ds_mitr->firstfirst) - { - - // ds_mitr->first is a new mode, therefore - // need to insert a new mode entry for ds_mistr->first. - ModeStack& ms = _modeMap[ds_mitr->first]; - - bool new_value = ds_mitr->second & StateAttribute::ON; - applyMode(ds_mitr->first,new_value,ms); - - // will need to disable this mode on next apply so set it to changed. - ms.changed = true; - - ++ds_mitr; - - } - else - { - // this_mitr & ds_mitr refer to the same mode, check the overide - // if any otherwise just apply the incomming mode. - - ModeStack& ms = this_mitr->second; - - if (!ms.valueVec.empty() && ms.valueVec.back() & StateAttribute::OVERRIDE) - { - // override is on, there just treat as a normal apply on modes. - - if (ms.changed) - { - ms.changed = false; - bool new_value = ms.valueVec.back() & StateAttribute::ON; - applyMode(this_mitr->first,new_value,ms); - - } - } - else - { - // no override on or no previous entry, therefore consider incomming mode. - bool new_value = ds_mitr->second & StateAttribute::ON; - if (applyMode(ds_mitr->first,new_value,ms)) - { - ms.changed = true; - } - } - - ++this_mitr; - ++ds_mitr; - } - } - - // iterator over the remaining state modes to apply any previous changes. - for(; - this_mitr!=_modeMap.end(); - ++this_mitr) - { - // note GLMode = this_mitr->first - ModeStack& ms = this_mitr->second; - if (ms.changed) - { - ms.changed = false; - if (!ms.valueVec.empty()) - { - bool new_value = ms.valueVec.back() & StateAttribute::ON; - applyMode(this_mitr->first,new_value,ms); - } - else - { - // assume default of disabled. - applyMode(this_mitr->first,ms.global_default_value,ms); - - } - - } - } - - // iterator over the remaining incomming modes to apply any new mode. - for(; - ds_mitr!=ds_modeList.end(); - ++ds_mitr) - { - ModeStack& ms = _modeMap[ds_mitr->first]; - - bool new_value = ds_mitr->second & StateAttribute::ON; - applyMode(ds_mitr->first,new_value,ms); - - // will need to disable this mode on next apply so set it to changed. - ms.changed = true; + if (unitgetAttributeList(); - StateSet::AttributeList::const_iterator ds_aitr=ds_attributeList.begin(); - - AttributeMap::iterator this_aitr=_attributeMap.begin(); - - while (this_aitr!=_attributeMap.end() && ds_aitr!=ds_attributeList.end()) - { - if (this_aitr->firstfirst) - { - - // note attribute type = this_aitr->first - AttributeStack& as = this_aitr->second; - if (as.changed) - { - as.changed = false; - if (!as.attributeVec.empty()) - { - const StateAttribute* new_attr = as.attributeVec.back().first; - applyAttribute(new_attr,as); - } - else - { - applyGlobalDefaultAttribute(as); - } - } - - ++this_aitr; - - } - else if (ds_aitr->firstfirst) - { - - // ds_mitr->first is a new attribute, therefore - // need to insert a new attribute entry for ds_aistr->first. - AttributeStack& as = _attributeMap[ds_aitr->first]; - - const StateAttribute* new_attr = ds_aitr->second.first.get(); - applyAttribute(new_attr,as); - - // will need to disable this mode on next apply so set it to changed. - as.changed = true; - - ++ds_aitr; - - } - else - { - // this_mitr & ds_mitr refer to the same mode, check the overide - // if any otherwise just apply the incomming mode. - - AttributeStack& as = this_aitr->second; - - if (!as.attributeVec.empty() && as.attributeVec.back().second) - { - // override is os, there just treat as a normal apply on modes. - - if (as.changed) - { - as.changed = false; - const StateAttribute* new_attr = as.attributeVec.back().first; - applyAttribute(new_attr,as); - } - } - else - { - // no override on or no previous entry, therefore consider incomming mode. - const StateAttribute* new_attr = ds_aitr->second.first.get(); - if (applyAttribute(new_attr,as)) - { - as.changed = true; - } - } - - ++this_aitr; - ++ds_aitr; - } - } - - // iterator over the remaining state modes to apply any previous changes. - for(; - this_aitr!=_attributeMap.end(); - ++this_aitr) - { - // note attribute type = this_aitr->first - AttributeStack& as = this_aitr->second; - if (as.changed) - { - as.changed = false; - if (!as.attributeVec.empty()) - { - const StateAttribute* new_attr = as.attributeVec.back().first; - applyAttribute(new_attr,as); - } - else - { - applyGlobalDefaultAttribute(as); - } - } - } - - // iterator over the remaining incomming modes to apply any new mode. - for(; - ds_aitr!=ds_attributeList.end(); - ++ds_aitr) - { - // ds_mitr->first is a new attribute, therefore - // need to insert a new attribute entry for ds_aistr->first. - AttributeStack& as = _attributeMap[ds_aitr->first]; - - const StateAttribute* new_attr = ds_aitr->second.first.get(); - applyAttribute(new_attr,as); - - // will need to update this attribute on next apply so set it to changed. - as.changed = true; - } - } - - - + } else { @@ -470,58 +217,90 @@ void State::apply() // go through all active OpenGL modes, enabling/disable where // appropriate. - for(ModeMap::iterator mitr=_modeMap.begin(); - mitr!=_modeMap.end(); - ++mitr) - { - // note GLMode = mitr->first - ModeStack& ms = mitr->second; - if (ms.changed) - { - ms.changed = false; - if (!ms.valueVec.empty()) - { - bool new_value = ms.valueVec.back() & StateAttribute::ON; - applyMode(mitr->first,new_value,ms); - } - else - { - // assume default of disabled. - applyMode(mitr->first,ms.global_default_value,ms); - } - - } - } - + applyModeMap(_modeMap); // go through all active StateAttribute's, applying where appropriate. - for(AttributeMap::iterator aitr=_attributeMap.begin(); - aitr!=_attributeMap.end(); - ++aitr) + applyAttributeMap(_attributeMap); + + unsigned int unit; + unsigned int unitMax = std::max(_textureModeMapList.size(),_textureAttributeMapList.size()); + for(unit=0;unitsecond; - if (as.changed) + if (setActiveTextureUnit(unit)) { - as.changed = false; - if (!as.attributeVec.empty()) - { - const StateAttribute* new_attr = as.attributeVec.back().first; - applyAttribute(new_attr,as); - } - else - { - applyGlobalDefaultAttribute(as); - } - + if (unit<_textureModeMapList.size()) applyModeMap(_textureModeMapList[unit]); + if (unit<_textureAttributeMapList.size()) applyAttributeMap(_textureAttributeMapList[unit]); } - } - + } } -/** mode has been set externally, update state to reflect this setting.*/ void State::haveAppliedMode(const StateAttribute::GLMode mode,const StateAttribute::GLModeValue value) { - ModeStack& ms = _modeMap[mode]; + haveAppliedMode(_modeMap,mode,value); +} + +void State::haveAppliedMode(const StateAttribute::GLMode mode) +{ + haveAppliedMode(_modeMap,mode); +} + +void State::haveAppliedAttribute(const StateAttribute* attribute) +{ + haveAppliedAttribute(_attributeMap,attribute); +} + +void State::haveAppliedAttribute(const StateAttribute::Type type) +{ + haveAppliedAttribute(_attributeMap,type); +} + +const bool State::getLastAppliedMode(const StateAttribute::GLMode mode) const +{ + return getLastAppliedMode(_modeMap,mode); +} + +const StateAttribute* State::getLastAppliedAttribute(const StateAttribute::Type type) const +{ + return getLastAppliedAttribute(_attributeMap,type); +} + + +void State::haveAppliedTextureMode(unsigned int unit,const StateAttribute::GLMode mode,const StateAttribute::GLModeValue value) +{ + haveAppliedMode(getOrCreateTextureModeMap(unit),mode,value); +} + +void State::haveAppliedTextureMode(unsigned int unit,const StateAttribute::GLMode mode) +{ + haveAppliedMode(getOrCreateTextureModeMap(unit),mode); +} + +void State::haveAppliedTextureAttribute(unsigned int unit,const StateAttribute* attribute) +{ + haveAppliedAttribute(getOrCreateTextureAttributeMap(unit),attribute); +} + +void State::haveAppliedTextureAttribute(unsigned int unit,const StateAttribute::Type type) +{ + haveAppliedAttribute(getOrCreateTextureAttributeMap(unit),type); +} + +const bool State::getLastAppliedTextureMode(unsigned int unit,const StateAttribute::GLMode mode) const +{ + if (unit>=_textureModeMapList.size()) return false; + return getLastAppliedMode(_textureModeMapList[unit],mode); +} + +const StateAttribute* State::getLastAppliedTextureAttribute(unsigned int unit,const StateAttribute::Type type) const +{ + if (unit>=_textureAttributeMapList.size()) return false; + return getLastAppliedAttribute(_textureAttributeMapList[unit],type); +} + + +void State::haveAppliedMode(ModeMap& modeMap,const StateAttribute::GLMode mode,const StateAttribute::GLModeValue value) +{ + ModeStack& ms = modeMap[mode]; ms.last_applied_value = value & StateAttribute::ON; @@ -530,9 +309,9 @@ void State::haveAppliedMode(const StateAttribute::GLMode mode,const StateAttribu } /** mode has been set externally, update state to reflect this setting.*/ -void State::haveAppliedMode(const StateAttribute::GLMode mode) +void State::haveAppliedMode(ModeMap& modeMap,const StateAttribute::GLMode mode) { - ModeStack& ms = _modeMap[mode]; + ModeStack& ms = modeMap[mode]; // don't know what last applied value is can't apply it. // assume that it has changed by toggle the value of last_applied_value. @@ -543,11 +322,11 @@ void State::haveAppliedMode(const StateAttribute::GLMode mode) } /** attribute has been applied externally, update state to reflect this setting.*/ -void State::haveAppliedAttribute(const StateAttribute* attribute) +void State::haveAppliedAttribute(AttributeMap& attributeMap,const StateAttribute* attribute) { if (attribute) { - AttributeStack& as = _attributeMap[attribute->getType()]; + AttributeStack& as = attributeMap[attribute->getType()]; as.last_applied_attribute = attribute; @@ -556,11 +335,11 @@ void State::haveAppliedAttribute(const StateAttribute* attribute) } } -void State::haveAppliedAttribute(const StateAttribute::Type type) +void State::haveAppliedAttribute(AttributeMap& attributeMap,const StateAttribute::Type type) { - AttributeMap::iterator itr = _attributeMap.find(type); - if (itr!=_attributeMap.end()) + AttributeMap::iterator itr = attributeMap.find(type); + if (itr!=attributeMap.end()) { AttributeStack& as = itr->second; as.last_applied_attribute = 0L; @@ -570,10 +349,10 @@ void State::haveAppliedAttribute(const StateAttribute::Type type) } } -const bool State::getLastAppliedMode(const StateAttribute::GLMode mode) const +const bool State::getLastAppliedMode(const ModeMap& modeMap,const StateAttribute::GLMode mode) const { - ModeMap::const_iterator itr = _modeMap.find(mode); - if (itr!=_modeMap.end()) + ModeMap::const_iterator itr = modeMap.find(mode); + if (itr!=modeMap.end()) { const ModeStack& ms = itr->second; return ms.last_applied_value; @@ -584,10 +363,10 @@ const bool State::getLastAppliedMode(const StateAttribute::GLMode mode) const } } -const StateAttribute* State::getLastAppliedAttribute(const StateAttribute::Type type) const +const StateAttribute* State::getLastAppliedAttribute(const AttributeMap& attributeMap,const StateAttribute::Type type) const { - AttributeMap::const_iterator itr = _attributeMap.find(type); - if (itr!=_attributeMap.end()) + AttributeMap::const_iterator itr = attributeMap.find(type); + if (itr!=attributeMap.end()) { const AttributeStack& as = itr->second; return as.last_applied_attribute; @@ -598,7 +377,6 @@ const StateAttribute* State::getLastAppliedAttribute(const StateAttribute::Type } } - Polytope State::getViewFrustum() const { Polytope cv; diff --git a/src/osg/StateSet.cpp b/src/osg/StateSet.cpp index f88233a10..c50a1a6cd 100644 --- a/src/osg/StateSet.cpp +++ b/src/osg/StateSet.cpp @@ -230,6 +230,73 @@ int StateSet::compare(const StateSet& rhs,bool compareAttributeContents) const return 0; } +int StateSet::compareModes(const ModeList& lhs,const ModeList& rhs) +{ + ModeList::const_iterator lhs_mode_itr = lhs.begin(); + ModeList::const_iterator rhs_mode_itr = rhs.begin(); + while (lhs_mode_itr!=lhs.end() && rhs_mode_itr!=rhs.end()) + { + if (lhs_mode_itr->firstfirst) return -1; + else if (rhs_mode_itr->firstfirst) return 1; + if (lhs_mode_itr->secondsecond) return -1; + else if (rhs_mode_itr->secondsecond) return 1; + ++lhs_mode_itr; + ++rhs_mode_itr; + } + if (lhs_mode_itr==lhs.end()) + { + if (rhs_mode_itr!=rhs.end()) return -1; + } + else if (rhs_mode_itr == rhs.end()) return 1; + return 0; +} + +int StateSet::compareAttributePtrs(const AttributeList& lhs,const AttributeList& rhs) +{ + AttributeList::const_iterator lhs_attr_itr = lhs.begin(); + AttributeList::const_iterator rhs_attr_itr = rhs.begin(); + while (lhs_attr_itr!=lhs.end() && rhs_attr_itr!=rhs.end()) + { + if (lhs_attr_itr->firstfirst) return -1; + else if (rhs_attr_itr->firstfirst) return 1; + if (lhs_attr_itr->second.firstsecond.first) return -1; + else if (rhs_attr_itr->second.firstsecond.first) return 1; + if (lhs_attr_itr->second.secondsecond.second) return -1; + else if (rhs_attr_itr->second.secondsecond.second) return 1; + ++lhs_attr_itr; + ++rhs_attr_itr; + } + if (lhs_attr_itr==lhs.end()) + { + if (rhs_attr_itr!=rhs.end()) return -1; + } + else if (rhs_attr_itr == rhs.end()) return 1; + return 0; +} + +int StateSet::compareAttributeContents(const AttributeList& lhs,const AttributeList& rhs) +{ + AttributeList::const_iterator lhs_attr_itr = lhs.begin(); + AttributeList::const_iterator rhs_attr_itr = rhs.begin(); + while (lhs_attr_itr!=lhs.end() && rhs_attr_itr!=rhs.end()) + { + if (lhs_attr_itr->firstfirst) return -1; + else if (rhs_attr_itr->firstfirst) return 1; + if (*(lhs_attr_itr->second.first)<*(rhs_attr_itr->second.first)) return -1; + else if (*(rhs_attr_itr->second.first)<*(lhs_attr_itr->second.first)) return 1; + if (lhs_attr_itr->second.secondsecond.second) return -1; + else if (rhs_attr_itr->second.secondsecond.second) return 1; + ++lhs_attr_itr; + ++rhs_attr_itr; + } + if (lhs_attr_itr==lhs.end()) + { + if (rhs_attr_itr!=rhs.end()) return -1; + } + else if (rhs_attr_itr == rhs.end()) return 1; + return 0; +} + void StateSet::setGlobalDefaults() { _renderingHint = DEFAULT_BIN; @@ -244,30 +311,6 @@ void StateSet::setGlobalDefaults() Material *material = osgNew Material; material->setColorMode(Material::AMBIENT_AND_DIFFUSE); setAttributeAndModes(material,StateAttribute::ON); -/* - setMode(GL_LIGHTING,StateAttribute::OFF); - setMode(GL_FOG,StateAttribute::OFF); - setMode(GL_POINT_SMOOTH,StateAttribute::OFF); - - setMode(GL_TEXTURE_2D,StateAttribute::OFF); - - setMode(GL_TEXTURE_GEN_S,StateAttribute::OFF); - setMode(GL_TEXTURE_GEN_T,StateAttribute::OFF); - setMode(GL_TEXTURE_GEN_R,StateAttribute::OFF); - setMode(GL_TEXTURE_GEN_Q,StateAttribute::OFF); - - setAttributeAndModes(osgNew AlphaFunc,StateAttribute::OFF); - setAttributeAndModes(osgNew CullFace,StateAttribute::ON); - setAttributeAndModes(osgNew FrontFace,StateAttribute::ON); - - Material *material = osgNew Material; - material->setColorMode(Material::AMBIENT_AND_DIFFUSE); - setAttributeAndModes(material,StateAttribute::ON); - - setAttributeAndModes(osgNew PolygonMode,StateAttribute::OFF); - setAttributeAndModes(osgNew Transparency,StateAttribute::OFF); - setAttributeAndModes(osgNew Depth,StateAttribute::ON); -*/ } @@ -402,45 +445,60 @@ void StateSet::merge(const StateSet& rhs) void StateSet::setMode(const StateAttribute::GLMode mode, const StateAttribute::GLModeValue value) { - if ((value&StateAttribute::INHERIT)) setModeToInherit(mode); - else _modeList[mode] = value; + setMode(_modeList,mode,value); } void StateSet::setModeToInherit(const StateAttribute::GLMode mode) { - ModeList::iterator itr = _modeList.find(mode); - if (itr!=_modeList.end()) - { - _modeList.erase(itr); - } + setModeToInherit(_modeList,mode); } const StateAttribute::GLModeValue StateSet::getMode(const StateAttribute::GLMode mode) const { - ModeList::const_iterator itr = _modeList.find(mode); - if (itr!=_modeList.end()) - { - return itr->second; - } - else - return StateAttribute::INHERIT; + return getMode(_modeList,mode); } void StateSet::setAttribute(StateAttribute *attribute, const StateAttribute::OverrideValue value) { if (attribute) { - if ((value&StateAttribute::INHERIT)) setAttributeToInherit(attribute->getType()); - else _attributeList[attribute->getType()] = RefAttributePair(attribute,value&StateAttribute::OVERRIDE); - } + if (!attribute->isTextureAttribute()) + { + setAttribute(_attributeList,attribute,value); + } + else + { + notify(NOTICE)<<"Warning: texture attribute '"<className()<<"'passed to setAttribute(attr,value), "<getType()] = RefAttributePair(attribute,value&StateAttribute::OVERRIDE); - attribute->setStateSetModes(*this,value); + if (!attribute->isTextureAttribute()) + { + if (value&StateAttribute::INHERIT) + { + setAttributeToInherit(attribute->getType()); + } + else + { + setAttribute(_attributeList,attribute,value); + setAssociatedModes(_modeList,attribute,value); + } + } + else + { + notify(NOTICE)<<"Warning: texture attribute '"<className()<<"' passed to setAttributeAndModes(attr,value), "<second.first->setStateSetModes(*this,StateAttribute::INHERIT); + setAssociatedModes(_modeList,itr->second.first.get(),StateAttribute::INHERIT); _attributeList.erase(itr); } } StateAttribute* StateSet::getAttribute(const StateAttribute::Type type) { - AttributeList::iterator itr = _attributeList.find(type); - if (itr!=_attributeList.end()) - { - return itr->second.first.get(); - } - else - return NULL; + return getAttribute(_attributeList,type); } const StateAttribute* StateSet::getAttribute(const StateAttribute::Type type) const { - AttributeList::const_iterator itr = _attributeList.find(type); - if (itr!=_attributeList.end()) - { - return itr->second.first.get(); - } - else - return NULL; + return getAttribute(_attributeList,type); } const StateSet::RefAttributePair* StateSet::getAttributePair(const StateAttribute::Type type) const { - AttributeList::const_iterator itr = _attributeList.find(type); - if (itr!=_attributeList.end()) - { - return &(itr->second); - } - else - return NULL; + return getAttributePair(_attributeList,type); } +void StateSet::setAssociatedModes(const StateAttribute* attribute, const StateAttribute::GLModeValue value) +{ + setAssociatedModes(_modeList,attribute,value); +} void StateSet::setTextureMode(unsigned int unit,const StateAttribute::GLMode mode, const StateAttribute::GLModeValue value) { - ModeList& modeList = getOrCreateTextureModeList(unit); - if ((value&StateAttribute::INHERIT)) setTextureModeToInherit(unit,mode); - else modeList[mode] = value; + setMode(getOrCreateTextureModeList(unit),mode,value); } void StateSet::setTextureModeToInherit(unsigned int unit,const StateAttribute::GLMode mode) { if (unit>=_textureModeList.size()) return; - ModeList& modeList = _textureModeList[unit]; - ModeList::iterator itr = modeList.find(mode); - if (itr!=modeList.end()) - { - modeList.erase(itr); - } + setModeToInherit(_textureModeList[unit],mode); } const StateAttribute::GLModeValue StateSet::getTextureMode(unsigned int unit,const StateAttribute::GLMode mode) const { if (unit>=_textureModeList.size()) return StateAttribute::INHERIT; - - const ModeList& modeList = _textureModeList[unit]; - ModeList::const_iterator itr = modeList.find(mode); - if (itr!=modeList.end()) - { - return itr->second; - } - else - return StateAttribute::INHERIT; + return getMode(_textureModeList[unit],mode); } void StateSet::setTextureAttribute(unsigned int unit,StateAttribute *attribute, const StateAttribute::OverrideValue value) { if (attribute) { - AttributeList& attributeList = getOrCreateTextureAttributeList(unit); - if ((value&StateAttribute::INHERIT)) setAttributeToInherit(attribute->getType()); - else attributeList[attribute->getType()] = RefAttributePair(attribute,value&StateAttribute::OVERRIDE); + if (attribute->isTextureAttribute()) + { + setAttribute(getOrCreateTextureAttributeList(unit),attribute,value); + } + else + { + notify(NOTICE)<<"Warning: texture attribute '"<className()<<"' passed to setTextureAttribute(unit,attr,value), "<getType()] = RefAttributePair(attribute,value&StateAttribute::OVERRIDE); - attribute->setStateSetModes(*this,value); + + if (attribute->isTextureAttribute()) + { + if (value&StateAttribute::INHERIT) + { + setTextureAttributeToInherit(unit,attribute->getType()); + } + else + { + setAttribute(getOrCreateTextureAttributeList(unit),attribute,value); + setAssociatedModes(getOrCreateTextureModeList(unit),attribute,value); + } + } + else + { + notify(NOTICE)<<"Warning: non texture attribute '"<className()<<"' passed to setTextureAttributeAndModes(unit,attr,value), "<second.first->setStateSetModes(*this,StateAttribute::INHERIT); + if (unit<_textureModeList.size()) + { + setAssociatedModes(_textureModeList[unit],itr->second.first.get(),StateAttribute::INHERIT); + } attributeList.erase(itr); } } @@ -560,44 +617,27 @@ void StateSet::setTextureAttributeToInherit(unsigned int unit,const StateAttribu StateAttribute* StateSet::getTextureAttribute(unsigned int unit,const StateAttribute::Type type) { if (unit>=_textureAttributeList.size()) return 0; - AttributeList& attributeList = _textureAttributeList[unit]; - AttributeList::iterator itr = attributeList.find(type); - if (itr!=attributeList.end()) - { - return itr->second.first.get(); - } - else - return NULL; + return getAttribute(_textureAttributeList[unit],type); } const StateAttribute* StateSet::getTextureAttribute(unsigned int unit,const StateAttribute::Type type) const { if (unit>=_textureAttributeList.size()) return 0; - const AttributeList& attributeList = _textureAttributeList[unit]; - AttributeList::const_iterator itr = attributeList.find(type); - if (itr!=attributeList.end()) - { - return itr->second.first.get(); - } - else - return NULL; + return getAttribute(_textureAttributeList[unit],type); } const StateSet::RefAttributePair* StateSet::getTextureAttributePair(unsigned int unit,const StateAttribute::Type type) const { if (unit>=_textureAttributeList.size()) return 0; - const AttributeList& attributeList = _textureAttributeList[unit]; - AttributeList::const_iterator itr = attributeList.find(type); - if (itr!=attributeList.end()) - { - return &(itr->second); - } - else - return NULL; + return getAttributePair(_textureAttributeList[unit],type); } +void StateSet::setAssociatedTextureModes(unsigned int unit,const StateAttribute* attribute, const StateAttribute::GLModeValue value) +{ + setAssociatedModes(getOrCreateTextureModeList(unit),attribute,value); +} void StateSet::compile(State& state) const @@ -649,7 +689,88 @@ void StateSet::setRendingBinToInherit() _binName = ""; } - +void StateSet::setMode(ModeList& modeList,const StateAttribute::GLMode mode, const StateAttribute::GLModeValue value) +{ + if ((value&StateAttribute::INHERIT)) setModeToInherit(modeList,mode); + else modeList[mode] = value; +} + +void StateSet::setModeToInherit(ModeList& modeList,const StateAttribute::GLMode mode) +{ + ModeList::iterator itr = modeList.find(mode); + if (itr!=modeList.end()) + { + modeList.erase(itr); + } +} + +const StateAttribute::GLModeValue StateSet::getMode(const ModeList& modeList,const StateAttribute::GLMode mode) const +{ + ModeList::const_iterator itr = modeList.find(mode); + if (itr!=modeList.end()) + { + return itr->second; + } + else + return StateAttribute::INHERIT; +} + +void StateSet::setAssociatedModes(ModeList& modeList,const StateAttribute* attribute, const StateAttribute::GLModeValue value) +{ + // get the associated modes. + std::vector modes; + attribute->getAssociatedModes(modes); + + // set the modes on the StateSet. + for(std::vector::iterator itr=modes.begin(); + itr!=modes.end(); + ++itr) + { + setMode(modeList,*itr,value); + } +} + +void StateSet::setAttribute(AttributeList& attributeList,StateAttribute *attribute, const StateAttribute::OverrideValue value) +{ + if (attribute) + { + attributeList[attribute->getType()] = RefAttributePair(attribute,value&StateAttribute::OVERRIDE); + } +} + + +StateAttribute* StateSet::getAttribute(AttributeList& attributeList,const StateAttribute::Type type) +{ + AttributeList::iterator itr = attributeList.find(type); + if (itr!=attributeList.end()) + { + return itr->second.first.get(); + } + else + return NULL; +} + +const StateAttribute* StateSet::getAttribute(const AttributeList& attributeList,const StateAttribute::Type type) const +{ + AttributeList::const_iterator itr = attributeList.find(type); + if (itr!=attributeList.end()) + { + return itr->second.first.get(); + } + else + return NULL; +} + +const StateSet::RefAttributePair* StateSet::getAttributePair(const AttributeList& attributeList,const StateAttribute::Type type) const +{ + AttributeList::const_iterator itr = attributeList.find(type); + if (itr!=attributeList.end()) + { + return &(itr->second); + } + else + return NULL; +} diff --git a/src/osg/Texture.cpp b/src/osg/Texture.cpp index a588e7eac..dd263cf54 100644 --- a/src/osg/Texture.cpp +++ b/src/osg/Texture.cpp @@ -197,12 +197,11 @@ void Texture::apply(State& state) const } else if (_image.valid() && _image->data()) { + glBindTexture( _target, handle ); uint& modifiedTag = getModifiedTag(contextID); - if (_subloadMode == AUTO || (_subloadMode == IF_DIRTY && modifiedTag != _image->getModifiedTag())) { - glBindTexture( _target, handle ); if (_texParamtersDirty) applyTexParameters(_target,state); glTexSubImage2D(_target, 0, _subloadOffsX, _subloadOffsY, diff --git a/src/osgPlugins/flt/ReaderWriterATTR.cpp b/src/osgPlugins/flt/ReaderWriterATTR.cpp index cac05bfd9..efe253296 100644 --- a/src/osgPlugins/flt/ReaderWriterATTR.cpp +++ b/src/osgPlugins/flt/ReaderWriterATTR.cpp @@ -661,9 +661,16 @@ StateSet* Attr::createOsgStateSet() // break; } - - osgStateSet->setAttribute( osgTexEnv ); - osgStateSet->setAttributeAndModes( osgTexture, StateAttribute::ON ); + // I have just ported the two below set*Attribute lines to use the new + // texture attribute methods, however this tieing to the texture unit 0 + // is probably inappropriate. Perhaps it would be better to create a + // StateSet to store the texture an modes, it is probably best + // to use an intermediate data structure for the flt loader to use to + // encapsulate ATTR files. Need to speak to Brede about this issue. + // Robert Osfield, July 9th 2002. + + osgStateSet->setTextureAttribute( 0, osgTexEnv ); + osgStateSet->setTextureAttributeAndModes( 0, osgTexture, StateAttribute::ON ); return osgStateSet; } diff --git a/src/osgPlugins/flt/flt2osg.cpp b/src/osgPlugins/flt/flt2osg.cpp index 2029f2741..32c9de95d 100644 --- a/src/osgPlugins/flt/flt2osg.cpp +++ b/src/osgPlugins/flt/flt2osg.cpp @@ -465,6 +465,9 @@ void ConvertFromFLT::visitTexturePalette(osg::Group& , TexturePaletteRecord* rec pTexturePool->addTexture(nIndex, osgStateSet); return; // Texture already loaded } + + + unsigned int unit = 0; // Read texture and attribute file osg::ref_ptr image = osgDB::readImageFile(pFilename); @@ -490,26 +493,28 @@ void ConvertFromFLT::visitTexturePalette(osg::Group& , TexturePaletteRecord* rec osg::Texture* osgTexture = new osg::Texture; osgTexture->setWrap(osg::Texture::WRAP_S,osg::Texture::REPEAT); osgTexture->setWrap(osg::Texture::WRAP_T,osg::Texture::REPEAT); - osgStateSet->setAttributeAndModes(osgTexture,osg::StateAttribute::ON); + osgStateSet->setTextureAttributeAndModes( unit, osgTexture,osg::StateAttribute::ON); osg::TexEnv* osgTexEnv = new osg::TexEnv; osgTexEnv->setMode(osg::TexEnv::MODULATE); - osgStateSet->setAttribute( osgTexEnv ); + osgStateSet->setTextureAttribute( unit, osgTexEnv ); } - osg::Texture *osgTexture = dynamic_cast(osgStateSet->getAttribute( osg::StateAttribute::TEXTURE)); + osg::Texture *osgTexture = dynamic_cast(osgStateSet->getTextureAttribute( unit, osg::StateAttribute::TEXTURE)); if (osgTexture == NULL) { osgTexture = new osg::Texture; - osgStateSet->setAttributeAndModes(osgTexture,osg::StateAttribute::ON); + osgStateSet->setTextureAttributeAndModes( unit, osgTexture,osg::StateAttribute::ON); } osgTexture->setImage(image.get()); // Add new texture to registry pool + // ( umm... should this have reference to the texture unit? RO. July2002) Registry::instance()->addTexture(pFilename, osgStateSet); // Also add to local pool to be able to get texture by index. + // ( umm... should this have reference to the texture unit? RO. July2002) pTexturePool->addTexture(nIndex, osgStateSet); } } @@ -913,7 +918,7 @@ void ConvertFromFLT::visitFace(GeoSetBuilder* pBuilder, FaceRecord* rec) osgStateSet->merge(*textureStateSet); // Alpha channel in texture? - osg::Texture *osgTexture = dynamic_cast(textureStateSet->getAttribute( osg::StateAttribute::TEXTURE)); + osg::Texture *osgTexture = dynamic_cast(textureStateSet->getTextureAttribute( 0, osg::StateAttribute::TEXTURE)); if (osgTexture) { osg::Image* osgImage = osgTexture->getImage(); @@ -931,7 +936,7 @@ void ConvertFromFLT::visitFace(GeoSetBuilder* pBuilder, FaceRecord* rec) { osg::TexGen* osgTexGen = new osg::TexGen; osgTexGen->setMode(osg::TexGen::SPHERE_MAP); - osgStateSet->setAttributeAndModes(osgTexGen,osg::StateAttribute::ON); + osgStateSet->setTextueAttributeAndModes( 0, osgTexGen,osg::StateAttribute::ON); } #endif diff --git a/src/osgPlugins/osg/StateSet.cpp b/src/osgPlugins/osg/StateSet.cpp index 6fbc653cc..59df82024 100644 --- a/src/osgPlugins/osg/StateSet.cpp +++ b/src/osgPlugins/osg/StateSet.cpp @@ -221,7 +221,7 @@ bool GeoState_readLocalData(Object& obj, Input& fr) statset.setAttribute(attribute); if (attribute->getType()==StateAttribute::TEXGEN) - attribute->setStateSetModes(statset,texgening); + statset.setAssociatedModes(attribute,texgening); iteratorAdvanced = true; } @@ -347,6 +347,74 @@ bool StateSet_readLocalData(Object& obj, Input& fr) stateset.setAttribute(attribute); iteratorAdvanced = true; } + + while(fr.matchSequence("textureUnit %i {")) + { + int entry = fr[0].getNoNestedBrackets(); + + unsigned int unit=0; + fr[1].getUInt(unit); + fr+=3; + + while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) + { + bool localIteratorAdvanced = false; + + bool readingMode = true; + StateAttribute::GLModeValue value; + while (readingMode) + { + readingMode=false; + if (fr[0].isInt()) + { + if (StateSet_matchModeStr(fr[1].getStr(),value)) + { + int mode; + fr[0].getInt(mode); + stateset.setTextureMode(unit,(StateAttribute::GLMode)mode,value); + fr+=2; + localIteratorAdvanced = true; + readingMode=true; + } + } + else + if (fr[0].getStr()) + { + if (StateSet_matchModeStr(fr[1].getStr(),value)) + { + GLNameToGLModeMap::iterator nitr = s_GLNameToGLModeMap.find(fr[0].getStr()); + if (nitr!=s_GLNameToGLModeMap.end()) + { + StateAttribute::GLMode mode = nitr->second; + stateset.setTextureMode(unit,mode,value); + fr+=2; + localIteratorAdvanced = true; + readingMode=true; + } + } + } + } + + StateAttribute* attribute = NULL; + while((attribute=fr.readStateAttribute())!=NULL) + { + stateset.setTextureAttribute(unit,attribute); + localIteratorAdvanced = true; + } + + if (!localIteratorAdvanced) + fr.advanceOverCurrentFieldOrBlock(); + } + + // skip over trailing '}' + ++fr; + + iteratorAdvanced = true; + + } + + + return iteratorAdvanced; } @@ -409,6 +477,49 @@ bool StateSet_writeLocalData(const Object& obj, Output& fw) { fw.writeObject(*(sitr->second.first)); } + + const StateSet::TextureModeList& tml = stateset.getTextureModeList(); + const StateSet::TextureAttributeList& tal = stateset.getTextureAttributeList(); + unsigned int maxUnit = std::max(tml.size(),tal.size()); + for(unsigned int unit=0;unitfirst); + if (nitr!=s_GLModeToGLNameMap.end()) + { + fw.indent() << nitr->second << " " << StateSet_getModeStr(mitr->second) << std::endl; + } + else + { + // no name defined for GLMode so just pass its value to fw. + fw.indent() << "0x" << std::hex << (osg::uint)mitr->first << std::dec <<" " << StateSet_getModeStr(mitr->second) << std::endl; + } + } + } + + if (unitsecond.first)); + } + } + + fw.moveOut(); + fw.indent()<<"}"<< std::endl; + } return true; } diff --git a/src/osgUtil/Optimizer.cpp b/src/osgUtil/Optimizer.cpp index ef9ba2960..a28d7579f 100644 --- a/src/osgUtil/Optimizer.cpp +++ b/src/osgUtil/Optimizer.cpp @@ -72,7 +72,9 @@ void Optimizer::optimize(osg::Node* node, unsigned int options) if (options & SHARE_DUPLICATE_STATE) { #if (defined(_MSC_VER) && _MSC_VER<1300 && !defined(_STLPORT_VERSION)) - osg::notify(osg::INFO)<<"Warning: VisualStudio 6.0 build, unable to run state optimizer"<accept(osv); @@ -216,9 +218,12 @@ void Optimizer::StateVisitor::optimize() { // create map from state attributes to stateset which contain them. - typedef std::set StateSetList; + typedef std::pair StateSetUnitPair; + typedef std::set StateSetList; typedef std::map AttributeToStateSetMap; + const unsigned int NON_TEXTURE_ATTRIBUTE = 0xffffffff; + AttributeToStateSetMap _attributeToStateSetMap; // NOTE will need to track state attribute override value too. @@ -234,9 +239,26 @@ void Optimizer::StateVisitor::optimize() { if (aitr->second.first->getDataVariance()==osg::Object::STATIC) { - _attributeToStateSetMap[aitr->second.first.get()].insert(sitr->first); + _attributeToStateSetMap[aitr->second.first.get()].insert(StateSetUnitPair(sitr->first,NON_TEXTURE_ATTRIBUTE)); } } + + + osg::StateSet::TextureAttributeList& texAttributes = sitr->first->getTextureAttributeList(); + for(unsigned int unit=0;unitsecond.first->getDataVariance()==osg::Object::STATIC) + { + _attributeToStateSetMap[aitr->second.first.get()].insert(StateSetUnitPair(sitr->first,unit)); + } + } + } + } if (_attributeToStateSetMap.size()<2) @@ -284,8 +306,10 @@ void Optimizer::StateVisitor::optimize() ++sitr) { osg::notify(osg::INFO) << " replace duplicate "<<*current<<" with "<<*first_unique<< std::endl; - osg::StateSet* stateset = *sitr; - stateset->setAttribute(*first_unique); + osg::StateSet* stateset = sitr->first; + unsigned int unit = sitr->second; + if (unit==NON_TEXTURE_ATTRIBUTE) stateset->setAttribute(*first_unique); + else stateset->setTextureAttribute(unit,*first_unique); } } else first_unique = current; diff --git a/src/osgUtil/SceneView.cpp b/src/osgUtil/SceneView.cpp index 698511f7f..150fcc55d 100644 --- a/src/osgUtil/SceneView.cpp +++ b/src/osgUtil/SceneView.cpp @@ -88,7 +88,7 @@ void SceneView::setDefaults() // enable lighting by default. _globalState->setMode(GL_LIGHTING, osg::StateAttribute::ON); - _light->setStateSetModes(*_globalState,osg::StateAttribute::ON); + _globalState->setAssociatedModes(_light.get(),osg::StateAttribute::ON); // enable depth testing by default. _globalState->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON); @@ -98,10 +98,10 @@ void SceneView::setDefaults() alphafunc->setFunction(osg::AlphaFunc::GREATER,0.0f); _globalState->setAttributeAndModes(alphafunc, osg::StateAttribute::ON); - // set up an alphafunc by default to speed up blending operations. - osg::TexEnv* texenv = osgNew osg::TexEnv; - texenv->setMode(osg::TexEnv::MODULATE); - _globalState->setAttributeAndModes(texenv, osg::StateAttribute::ON); + // set up an texture environment by default to speed up blending operations. + osg::TexEnv* texenv = osgNew osg::TexEnv; + texenv->setMode(osg::TexEnv::MODULATE); + _globalState->setTextureAttributeAndModes(0,texenv, osg::StateAttribute::ON); osg::LightModel* lightmodel = osgNew osg::LightModel; lightmodel->setAmbientIntensity(osg::Vec4(0.0f,0.0f,0.0f,1.0f));