GLObjectsVisitor 4.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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_GLOBJECTSVISITOR
#define OSGUTIL_GLOBJECTSVISITOR 1

#include <OpenThreads/Mutex>
#include <osg/NodeVisitor>
#include <osg/Geode>
#include <osg/State>

#include <osgUtil/Export>

namespace osgUtil {

/** Visitor for traversing scene graph and setting each osg::Drawable's _useDisplayList flag, 
  * with option to immediately compile osg::Drawable OpenGL Display lists and
  * osg::StateAttribute's.
  */
class OSGUTIL_EXPORT GLObjectsVisitor : public osg::NodeVisitor
{
    public:

        /** Operation modes of the.*/
        enum ModeValues
        {
            SWITCH_ON_DISPLAY_LISTS             = 0x1,
            SWITCH_OFF_DISPLAY_LISTS            = 0x2,
            COMPILE_DISPLAY_LISTS               = 0x4,
            COMPILE_STATE_ATTRIBUTES            = 0x8,
            RELEASE_DISPLAY_LISTS               = 0x10,
            RELEASE_STATE_ATTRIBUTES            = 0x20,
            SWITCH_ON_VERTEX_BUFFER_OBJECTS     = 0x40,
            SWITCH_OFF_VERTEX_BUFFER_OBJECTS    = 0x80,
            CHECK_BLACK_LISTED_MODES            = 0x100
        };
        
        typedef unsigned int Mode;

        /** Construct a GLObjectsVisitor to traverse all children, operating on
          * node according to specified mode, such as to compile or release 
          * display list/texture objects etc. Default mode is to compile 
          * GL objects.
          */
        GLObjectsVisitor(Mode mode=COMPILE_DISPLAY_LISTS|COMPILE_STATE_ATTRIBUTES|CHECK_BLACK_LISTED_MODES);      
 
        META_NodeVisitor("osg","GLObjectsVisitor")
       
        virtual void reset()
        {
            _drawablesAppliedSet.clear();
            _stateSetAppliedSet.clear();
        }

        
        /** Set the operational mode of what operations to do on the scene graph.*/
        void setMode(Mode mode) { _mode = mode; }

        /** Get the operational mode.*/
        Mode getMode() const { return _mode; }
        

        /** Set the State to use during traversal. */
        void setState(osg::State* state)
        {
            _renderInfo.setState(state);
        }
        
        osg::State* getState()
        {
            return _renderInfo.getState();
        }

        void setRenderInfo(osg::RenderInfo& renderInfo)
        {
            _renderInfo = renderInfo;
        }
        
        osg::RenderInfo& getRenderInfo()
        {
            return _renderInfo;
        }

        /** Simply traverse using standard NodeVisitor traverse method.*/
        virtual void apply(osg::Node& node);
        
        /** For each Geode visited set the display list usage according to the 
          * _displayListMode.
          */
        virtual void apply(osg::Geode& node);

        void apply(osg::Drawable& drawable);
        void apply(osg::StateSet& stateset);

    protected:

        typedef std::set<osg::Drawable*> DrawableAppliedSet;
        typedef std::set<osg::StateSet*> StatesSetAppliedSet;

        Mode                        _mode;
        osg::RenderInfo             _renderInfo;
        DrawableAppliedSet          _drawablesAppliedSet;
        StatesSetAppliedSet         _stateSetAppliedSet;
        osg::ref_ptr<osg::Program>  _lastCompiledProgram;

};

class OSGUTIL_EXPORT GLObjectsOperation : public osg::GraphicsOperation
{
    public:

        GLObjectsOperation(GLObjectsVisitor::Mode mode = GLObjectsVisitor::COMPILE_DISPLAY_LISTS|GLObjectsVisitor::COMPILE_STATE_ATTRIBUTES|GLObjectsVisitor::CHECK_BLACK_LISTED_MODES);

        GLObjectsOperation(osg::Node* subgraph, GLObjectsVisitor::Mode mode = GLObjectsVisitor::COMPILE_DISPLAY_LISTS|GLObjectsVisitor::COMPILE_STATE_ATTRIBUTES|GLObjectsVisitor::CHECK_BLACK_LISTED_MODES);

        virtual void operator () (osg::GraphicsContext* context);
        
    protected:
    
        osg::ref_ptr<osg::Node> _subgraph;
        GLObjectsVisitor::Mode  _mode;
};

}

#endif