| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGUTIL_GLOBJECTSVISITOR |
|---|
| 15 | #define OSGUTIL_GLOBJECTSVISITOR 1 |
|---|
| 16 | |
|---|
| 17 | #include <OpenThreads/Mutex> |
|---|
| 18 | #include <osg/NodeVisitor> |
|---|
| 19 | #include <osg/Geode> |
|---|
| 20 | #include <osg/State> |
|---|
| 21 | |
|---|
| 22 | #include <osgUtil/Export> |
|---|
| 23 | |
|---|
| 24 | namespace osgUtil { |
|---|
| 25 | |
|---|
| 26 | /** Visitor for traversing scene graph and setting each osg::Drawable's _useDisplayList flag, |
|---|
| 27 | * with option to immediately compile osg::Drawable OpenGL Display lists and |
|---|
| 28 | * osg::StateAttribute's. |
|---|
| 29 | */ |
|---|
| 30 | class OSGUTIL_EXPORT GLObjectsVisitor : public osg::NodeVisitor |
|---|
| 31 | { |
|---|
| 32 | public: |
|---|
| 33 | |
|---|
| 34 | /** Operation modes of the.*/ |
|---|
| 35 | enum ModeValues |
|---|
| 36 | { |
|---|
| 37 | SWITCH_ON_DISPLAY_LISTS = 0x1, |
|---|
| 38 | SWITCH_OFF_DISPLAY_LISTS = 0x2, |
|---|
| 39 | COMPILE_DISPLAY_LISTS = 0x4, |
|---|
| 40 | COMPILE_STATE_ATTRIBUTES = 0x8, |
|---|
| 41 | RELEASE_DISPLAY_LISTS = 0x10, |
|---|
| 42 | RELEASE_STATE_ATTRIBUTES = 0x20, |
|---|
| 43 | SWITCH_ON_VERTEX_BUFFER_OBJECTS = 0x40, |
|---|
| 44 | SWITCH_OFF_VERTEX_BUFFER_OBJECTS = 0x80, |
|---|
| 45 | CHECK_BLACK_LISTED_MODES = 0x100 |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | typedef unsigned int Mode; |
|---|
| 49 | |
|---|
| 50 | /** Construct a GLObjectsVisitor to traverse all children, operating on |
|---|
| 51 | * node according to specified mode, such as to compile or release |
|---|
| 52 | * display list/texture objects etc. Default mode is to compile |
|---|
| 53 | * GL objects. |
|---|
| 54 | */ |
|---|
| 55 | GLObjectsVisitor(Mode mode=COMPILE_DISPLAY_LISTS|COMPILE_STATE_ATTRIBUTES|CHECK_BLACK_LISTED_MODES); |
|---|
| 56 | |
|---|
| 57 | META_NodeVisitor("osg","GLObjectsVisitor") |
|---|
| 58 | |
|---|
| 59 | virtual void reset() |
|---|
| 60 | { |
|---|
| 61 | _drawablesAppliedSet.clear(); |
|---|
| 62 | _stateSetAppliedSet.clear(); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | /** Set the operational mode of what operations to do on the scene graph.*/ |
|---|
| 67 | void setMode(Mode mode) { _mode = mode; } |
|---|
| 68 | |
|---|
| 69 | /** Get the operational mode.*/ |
|---|
| 70 | Mode getMode() const { return _mode; } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | /** Set the State to use during traversal. */ |
|---|
| 74 | void setState(osg::State* state) |
|---|
| 75 | { |
|---|
| 76 | _renderInfo.setState(state); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | osg::State* getState() |
|---|
| 80 | { |
|---|
| 81 | return _renderInfo.getState(); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | void setRenderInfo(osg::RenderInfo& renderInfo) |
|---|
| 85 | { |
|---|
| 86 | _renderInfo = renderInfo; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | osg::RenderInfo& getRenderInfo() |
|---|
| 90 | { |
|---|
| 91 | return _renderInfo; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** Simply traverse using standard NodeVisitor traverse method.*/ |
|---|
| 95 | virtual void apply(osg::Node& node); |
|---|
| 96 | |
|---|
| 97 | /** For each Geode visited set the display list usage according to the |
|---|
| 98 | * _displayListMode. |
|---|
| 99 | */ |
|---|
| 100 | virtual void apply(osg::Geode& node); |
|---|
| 101 | |
|---|
| 102 | void apply(osg::Drawable& drawable); |
|---|
| 103 | void apply(osg::StateSet& stateset); |
|---|
| 104 | |
|---|
| 105 | protected: |
|---|
| 106 | |
|---|
| 107 | typedef std::set<osg::Drawable*> DrawableAppliedSet; |
|---|
| 108 | typedef std::set<osg::StateSet*> StatesSetAppliedSet; |
|---|
| 109 | |
|---|
| 110 | Mode _mode; |
|---|
| 111 | osg::RenderInfo _renderInfo; |
|---|
| 112 | DrawableAppliedSet _drawablesAppliedSet; |
|---|
| 113 | StatesSetAppliedSet _stateSetAppliedSet; |
|---|
| 114 | osg::ref_ptr<osg::Program> _lastCompiledProgram; |
|---|
| 115 | |
|---|
| 116 | }; |
|---|
| 117 | |
|---|
| 118 | class OSGUTIL_EXPORT GLObjectsOperation : public osg::GraphicsOperation |
|---|
| 119 | { |
|---|
| 120 | public: |
|---|
| 121 | |
|---|
| 122 | GLObjectsOperation(GLObjectsVisitor::Mode mode = GLObjectsVisitor::COMPILE_DISPLAY_LISTS|GLObjectsVisitor::COMPILE_STATE_ATTRIBUTES|GLObjectsVisitor::CHECK_BLACK_LISTED_MODES); |
|---|
| 123 | |
|---|
| 124 | GLObjectsOperation(osg::Node* subgraph, GLObjectsVisitor::Mode mode = GLObjectsVisitor::COMPILE_DISPLAY_LISTS|GLObjectsVisitor::COMPILE_STATE_ATTRIBUTES|GLObjectsVisitor::CHECK_BLACK_LISTED_MODES); |
|---|
| 125 | |
|---|
| 126 | virtual void operator () (osg::GraphicsContext* context); |
|---|
| 127 | |
|---|
| 128 | protected: |
|---|
| 129 | |
|---|
| 130 | osg::ref_ptr<osg::Node> _subgraph; |
|---|
| 131 | GLObjectsVisitor::Mode _mode; |
|---|
| 132 | }; |
|---|
| 133 | |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | #endif |
|---|
| 137 | |
|---|