GLObjectsVisitor 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #ifndef OSGUTIL_GLOBJECTSVISITOR
  14. #define OSGUTIL_GLOBJECTSVISITOR 1
  15. #include <OpenThreads/Mutex>
  16. #include <osg/NodeVisitor>
  17. #include <osg/Geode>
  18. #include <osg/State>
  19. #include <osgUtil/Export>
  20. namespace osgUtil {
  21. /** Visitor for traversing scene graph and setting each osg::Drawable's _useDisplayList flag,
  22. * with option to immediately compile osg::Drawable OpenGL Display lists and
  23. * osg::StateAttribute's.
  24. */
  25. class OSGUTIL_EXPORT GLObjectsVisitor : public osg::NodeVisitor
  26. {
  27. public:
  28. /** Operation modes of the.*/
  29. enum ModeValues
  30. {
  31. SWITCH_ON_DISPLAY_LISTS = 0x1,
  32. SWITCH_OFF_DISPLAY_LISTS = 0x2,
  33. COMPILE_DISPLAY_LISTS = 0x4,
  34. COMPILE_STATE_ATTRIBUTES = 0x8,
  35. RELEASE_DISPLAY_LISTS = 0x10,
  36. RELEASE_STATE_ATTRIBUTES = 0x20,
  37. SWITCH_ON_VERTEX_BUFFER_OBJECTS = 0x40,
  38. SWITCH_OFF_VERTEX_BUFFER_OBJECTS = 0x80,
  39. CHECK_BLACK_LISTED_MODES = 0x100
  40. };
  41. typedef unsigned int Mode;
  42. /** Construct a GLObjectsVisitor to traverse all children, operating on
  43. * node according to specified mode, such as to compile or release
  44. * display list/texture objects etc. Default mode is to compile
  45. * GL objects.
  46. */
  47. GLObjectsVisitor(Mode mode=COMPILE_DISPLAY_LISTS|COMPILE_STATE_ATTRIBUTES|CHECK_BLACK_LISTED_MODES);
  48. META_NodeVisitor(osgUtil, GLObjectsVisitor)
  49. virtual void reset()
  50. {
  51. _drawablesAppliedSet.clear();
  52. _stateSetAppliedSet.clear();
  53. }
  54. /** Set the operational mode of what operations to do on the scene graph.*/
  55. void setMode(Mode mode) { _mode = mode; }
  56. /** Get the operational mode.*/
  57. Mode getMode() const { return _mode; }
  58. /** Set the State to use during traversal. */
  59. void setState(osg::State* state)
  60. {
  61. _renderInfo.setState(state);
  62. }
  63. osg::State* getState()
  64. {
  65. return _renderInfo.getState();
  66. }
  67. void setRenderInfo(osg::RenderInfo& renderInfo)
  68. {
  69. _renderInfo = renderInfo;
  70. }
  71. osg::RenderInfo& getRenderInfo()
  72. {
  73. return _renderInfo;
  74. }
  75. /** Set whether and how often OpenGL errors should be checked for, defaults to osg::State::ONCE_PER_ATTRIBUTE. */
  76. void setCheckForGLErrors(osg::State::CheckForGLErrors check) { _checkGLErrors = check; }
  77. /** Get whether and how often OpenGL errors should be checked for.*/
  78. osg::State::CheckForGLErrors getCheckForGLErrors() const { return _checkGLErrors; }
  79. /** Simply traverse using standard NodeVisitor traverse method.*/
  80. virtual void apply(osg::Node& node);
  81. void apply(osg::Drawable& drawable);
  82. void apply(osg::StateSet& stateset);
  83. /** Do a compile traversal and then reset any state,*/
  84. void compile(osg::Node& node);
  85. protected:
  86. typedef std::set<osg::Drawable*> DrawableAppliedSet;
  87. typedef std::set<osg::StateSet*> StatesSetAppliedSet;
  88. Mode _mode;
  89. osg::RenderInfo _renderInfo;
  90. osg::State::CheckForGLErrors _checkGLErrors;
  91. DrawableAppliedSet _drawablesAppliedSet;
  92. StatesSetAppliedSet _stateSetAppliedSet;
  93. osg::ref_ptr<osg::Program> _lastCompiledProgram;
  94. };
  95. class OSGUTIL_EXPORT GLObjectsOperation : public osg::GraphicsOperation
  96. {
  97. public:
  98. GLObjectsOperation(GLObjectsVisitor::Mode mode = GLObjectsVisitor::COMPILE_DISPLAY_LISTS|GLObjectsVisitor::COMPILE_STATE_ATTRIBUTES|GLObjectsVisitor::CHECK_BLACK_LISTED_MODES);
  99. GLObjectsOperation(osg::Node* subgraph, GLObjectsVisitor::Mode mode = GLObjectsVisitor::COMPILE_DISPLAY_LISTS|GLObjectsVisitor::COMPILE_STATE_ATTRIBUTES|GLObjectsVisitor::CHECK_BLACK_LISTED_MODES);
  100. virtual void operator () (osg::GraphicsContext* context);
  101. protected:
  102. osg::ref_ptr<osg::Node> _subgraph;
  103. GLObjectsVisitor::Mode _mode;
  104. };
  105. }
  106. #endif