diff --git a/include/osg/TexGenNode b/include/osg/TexGenNode index 7ea5de01e..001e15171 100644 --- a/include/osg/TexGenNode +++ b/include/osg/TexGenNode @@ -32,25 +32,26 @@ class SG_EXPORT TexGenNode : public Group META_Node(osg, TexGenNode); + + void setTextureUnit(unsigned int textureUnit) { _textureUnit = textureUnit; } + + unsigned int getTextureUnit() const { return _textureUnit; } + /** Set the TexGen.*/ void setTexGen(TexGen* texgen); - + /** Get the TexGen.*/ inline TexGen* getTexGen() { return _texgen.get(); } /** Get the const TexGen.*/ inline const TexGen* getTexGen() const { return _texgen.get(); } - /** Set the GLModes on StateSet associated with the TexGen.*/ - void setStateSetModes(StateSet&,StateAttribute::GLModeValue) const; - - /** Set up the local StateSet */ - void setLocalStateSetModes(StateAttribute::GLModeValue=StateAttribute::ON); protected: virtual ~TexGenNode(); + unsigned int _textureUnit; StateAttribute::GLModeValue _value; osg::ref_ptr _texgen; }; diff --git a/include/osgUtil/CullVisitor b/include/osgUtil/CullVisitor index d26c6886b..e4eec873a 100644 --- a/include/osgUtil/CullVisitor +++ b/include/osgUtil/CullVisitor @@ -170,6 +170,9 @@ class OSGUTIL_EXPORT CullVisitor : public osg::NodeVisitor, public osg::CullStac /** Add an attribute which is positioned related to the modelview matrix.*/ inline void addPositionedAttribute(osg::RefMatrix* matrix,const osg::StateAttribute* attr); + /** Add an attribute which is positioned related to the modelview matrix.*/ + inline void addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix* matrix,const osg::StateAttribute* attr); + /** reimplement CullStack's popProjectionMatrix() adding clamping of the projection matrix to the computed near and far.*/ virtual void popProjectionMatrix(); @@ -351,6 +354,12 @@ inline void CullVisitor::addPositionedAttribute(osg::RefMatrix* matrix,const osg _currentRenderBin->_stage->addPositionedAttribute(matrix,attr); } +/** Add an attribute which is positioned related to the modelview matrix.*/ +inline void CullVisitor::addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix* matrix,const osg::StateAttribute* attr) +{ + _currentRenderBin->_stage->addPositionedTextureAttribute(textureUnit,matrix,attr); +} + inline RenderLeaf* CullVisitor::createOrReuseRenderLeaf(osg::Drawable* drawable,osg::RefMatrix* projection,osg::RefMatrix* matrix, float depth) { // skip of any already reused renderleaf. diff --git a/include/osgUtil/RenderStage b/include/osgUtil/RenderStage index d01577856..7412f62b1 100644 --- a/include/osgUtil/RenderStage +++ b/include/osgUtil/RenderStage @@ -117,6 +117,11 @@ class OSGUTIL_EXPORT RenderStage : public RenderBin getRenderStageLighting()->addPositionedAttribute(matrix,attr); } + virtual void addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix* matrix,const osg::StateAttribute* attr) + { + getRenderStageLighting()->addPositionedTextureAttribute(textureUnit, matrix,attr); + } + virtual void drawPreRenderStages(osg::State& state,RenderLeaf*& previous); virtual void draw(osg::State& state,RenderLeaf*& previous); diff --git a/include/osgUtil/RenderStageLighting b/include/osgUtil/RenderStageLighting index 049a8379c..d02351895 100644 --- a/include/osgUtil/RenderStageLighting +++ b/include/osgUtil/RenderStageLighting @@ -41,18 +41,25 @@ class OSGUTIL_EXPORT RenderStageLighting : public osg::Object virtual void reset(); typedef std::pair< const osg::StateAttribute*, osg::ref_ptr > AttrMatrixPair; - typedef std::vector< AttrMatrixPair > AttrMatrixList; + typedef std::vector< AttrMatrixPair > AttrMatrixList; + typedef std::map< unsigned int, AttrMatrixList > TexUnitAttrMatrixListMap; virtual void addPositionedAttribute(osg::RefMatrix* matrix,const osg::StateAttribute* attr) { _attrList.push_back(AttrMatrixPair(attr,matrix)); } + virtual void addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix* matrix,const osg::StateAttribute* attr) + { + _texAttrListMap[textureUnit].push_back(AttrMatrixPair(attr,matrix)); + } + virtual void draw(osg::State& state,RenderLeaf*& previous); public: - AttrMatrixList _attrList; + AttrMatrixList _attrList; + TexUnitAttrMatrixListMap _texAttrListMap; protected: diff --git a/src/osg/TexGenNode.cpp b/src/osg/TexGenNode.cpp index b40a53165..ae281547d 100644 --- a/src/osg/TexGenNode.cpp +++ b/src/osg/TexGenNode.cpp @@ -16,14 +16,15 @@ using namespace osg; TexGenNode::TexGenNode() { + _textureUnit = 0; _value = StateAttribute::ON; _stateset = new StateSet; _texgen = new TexGen; - setLocalStateSetModes(_value); } TexGenNode::TexGenNode(const TexGenNode& cn, const CopyOp& copyop): Group(cn,copyop), + _textureUnit(cn._textureUnit), _value(cn._value), _texgen(static_cast(copyop(cn._texgen.get()))) { @@ -36,19 +37,4 @@ TexGenNode::~TexGenNode() void TexGenNode::setTexGen(TexGen* texgen) { _texgen = texgen; - setLocalStateSetModes(_value); -} - -// Set the GLModes on StateSet associated with the TexGen. -void TexGenNode::setStateSetModes(StateSet& stateset,const StateAttribute::GLModeValue value) const -{ - if (_texgen.valid()) - stateset.setAssociatedModes(_texgen.get(),value); -} - -void TexGenNode::setLocalStateSetModes(const StateAttribute::GLModeValue value) -{ - if (!_stateset) _stateset = new StateSet; - _stateset->setAllToInherit(); - setStateSetModes(*_stateset,value); } diff --git a/src/osgUtil/CullVisitor.cpp b/src/osgUtil/CullVisitor.cpp index 7c70721ea..97c72e7fa 100644 --- a/src/osgUtil/CullVisitor.cpp +++ b/src/osgUtil/CullVisitor.cpp @@ -883,7 +883,7 @@ void CullVisitor::apply(TexGenNode& node) RefMatrix& matrix = getModelViewMatrix(); - addPositionedAttribute(&matrix,node.getTexGen()); + addPositionedTextureAttribute(node.getTextureUnit(), &matrix,node.getTexGen()); handle_cull_callbacks_and_traverse(node); diff --git a/src/osgUtil/RenderStageLighting.cpp b/src/osgUtil/RenderStageLighting.cpp index 96def9296..c9b585759 100644 --- a/src/osgUtil/RenderStageLighting.cpp +++ b/src/osgUtil/RenderStageLighting.cpp @@ -56,4 +56,27 @@ void RenderStageLighting::draw(osg::State& state,RenderLeaf*& previous) } + for(TexUnitAttrMatrixListMap::iterator titr=_texAttrListMap.begin(); + titr!=_texAttrListMap.end(); + ++titr) + { + state.setActiveTextureUnit(titr->first); + + AttrMatrixList attrList = titr->second; + + for(AttrMatrixList::iterator litr=attrList.begin(); + litr!=attrList.end(); + ++litr) + { + state.applyModelViewMatrix((*litr).second.get()); + + // apply the light source. + litr->first->apply(state); + + // tell state about. + state.haveAppliedTextureAttribute(titr->first, litr->first); + + } + + } }