Renderer 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 OSGVIEWER_RENDERER
  14. #define OSGVIEWER_RENDERER 1
  15. #include <OpenThreads/Condition>
  16. #include <osg/Timer>
  17. #include <osgDB/DatabasePager>
  18. #include <osgUtil/SceneView>
  19. #include <osgViewer/Export>
  20. namespace osgViewer {
  21. class OSGVIEWER_EXPORT OpenGLQuerySupport : public osg::Referenced
  22. {
  23. public:
  24. OpenGLQuerySupport();
  25. virtual void checkQuery(osg::Stats* stats, osg::State* state,
  26. osg::Timer_t startTick) = 0;
  27. virtual void beginQuery(unsigned int frameNumber, osg::State* state) = 0;
  28. virtual void endQuery(osg::State* state) = 0;
  29. virtual void initialize(osg::State* state, osg::Timer_t startTick);
  30. protected:
  31. const osg::GLExtensions* _extensions;
  32. };
  33. class OSGVIEWER_EXPORT Renderer : public osg::GraphicsOperation
  34. {
  35. public:
  36. Renderer(osg::Camera* camera);
  37. osgUtil::SceneView* getSceneView(unsigned int i) { return _sceneView[i].get(); }
  38. const osgUtil::SceneView* getSceneView(unsigned int i) const { return _sceneView[i].get(); }
  39. void setDone(bool done) { _done = done; }
  40. bool getDone() { return _done; }
  41. void setGraphicsThreadDoesCull(bool flag);
  42. bool getGraphicsThreadDoesCull() const { return _graphicsThreadDoesCull; }
  43. virtual void cull();
  44. virtual void draw();
  45. virtual void cull_draw();
  46. virtual void compile();
  47. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  48. virtual void releaseGLObjects(osg::State* = 0) const;
  49. void setCompileOnNextDraw(bool flag) { _compileOnNextDraw = flag; }
  50. bool getCompileOnNextDraw() const { return _compileOnNextDraw; }
  51. virtual void operator () (osg::Object* object);
  52. virtual void operator () (osg::GraphicsContext* context);
  53. virtual void release();
  54. virtual void reset();
  55. /** Force update of state associated with cameras. */
  56. void setCameraRequiresSetUp(bool flag);
  57. bool getCameraRequiresSetUp() const;
  58. protected:
  59. void initialize(osg::State* state);
  60. virtual ~Renderer();
  61. virtual void updateSceneView(osgUtil::SceneView* sceneView);
  62. osg::observer_ptr<osg::Camera> _camera;
  63. bool _done;
  64. bool _graphicsThreadDoesCull;
  65. bool _compileOnNextDraw;
  66. bool _serializeDraw;
  67. osg::ref_ptr<osgUtil::SceneView> _sceneView[2];
  68. struct OSGVIEWER_EXPORT ThreadSafeQueue
  69. {
  70. OpenThreads::Mutex _mutex;
  71. OpenThreads::Condition _cond;
  72. typedef std::list<osgUtil::SceneView*> SceneViewList;
  73. SceneViewList _queue;
  74. bool _isReleased;
  75. ThreadSafeQueue();
  76. ~ThreadSafeQueue();
  77. /** Release any thread waiting on the queue, even if the queue is empty. */
  78. void release();
  79. /** Reset to fefault state (_isReleased = false)*/
  80. void reset();
  81. /** Take a SceneView from the queue. Can return 0 if release() is called when the queue is empty. */
  82. osgUtil::SceneView* takeFront();
  83. /** Add a SceneView object to the back of the queue. */
  84. void add(osgUtil::SceneView* sv);
  85. };
  86. ThreadSafeQueue _availableQueue;
  87. ThreadSafeQueue _drawQueue;
  88. bool _initialized;
  89. osg::ref_ptr<OpenGLQuerySupport> _querySupport;
  90. osg::Timer_t _startTick;
  91. };
  92. }
  93. #endif