View 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 OSG_VIEW
  14. #define OSG_VIEW 1
  15. #include <osg/Camera>
  16. #include <osg/Light>
  17. #include <osg/Stats>
  18. #include <OpenThreads/Mutex>
  19. namespace osg {
  20. /** View - maintains a master camera view and a list of slave cameras that are relative to this master camera.
  21. * Note, if no slave cameras are attached to the view then the master camera does both the control and implementation of the rendering of the scene,
  22. * but if slave cameras are present then the master controls the view onto the scene, while the slaves implement the rendering of the scene.
  23. */
  24. class OSG_EXPORT View : public virtual osg::Object
  25. {
  26. public :
  27. View();
  28. View(const osg::View& view, const osg::CopyOp& copyop=CopyOp::SHALLOW_COPY);
  29. META_Object(osg,View);
  30. /** Take all the settings, Camera and Slaves from the passed in view, leaving it empty. */
  31. virtual void take(View& rhs);
  32. /** Set the Stats object used to collect various frame related timing and scene graph stats.*/
  33. void setStats(osg::Stats* stats) { _stats = stats; }
  34. /** Get the Viewers Stats object.*/
  35. osg::Stats* getStats() { return _stats.get(); }
  36. /** Get the Viewers Stats object.*/
  37. const osg::Stats* getStats() const { return _stats.get(); }
  38. /** Options for controlling the global lighting used for the view.*/
  39. enum LightingMode
  40. {
  41. NO_LIGHT,
  42. HEADLIGHT,
  43. SKY_LIGHT
  44. };
  45. /** Set the global lighting to use for this view.
  46. * Defaults to headlight. */
  47. void setLightingMode(LightingMode lightingMode);
  48. /** Get the global lighting used for this view.*/
  49. LightingMode getLightingMode() const { return _lightingMode; }
  50. /** Get the global light.*/
  51. void setLight(osg::Light* light) { _light = light; }
  52. /** Get the global lighting if assigned.*/
  53. osg::Light* getLight() { return _light.get(); }
  54. /** Get the const global lighting if assigned.*/
  55. const osg::Light* getLight() const { return _light.get(); }
  56. /** Set the master camera of the view. */
  57. void setCamera(osg::Camera* camera);
  58. /** Get the master camera of the view. */
  59. osg::Camera* getCamera() { return _camera.get(); }
  60. /** Get the const master camera of the view. */
  61. const osg::Camera* getCamera() const { return _camera.get(); }
  62. /** Set the frame stamp of the view. */
  63. void setFrameStamp(osg::FrameStamp* fs) { _frameStamp = fs; }
  64. /** Get the frame stamp of the view. */
  65. osg::FrameStamp* getFrameStamp() { return _frameStamp.get(); }
  66. /** Get the frame stamp of the view. */
  67. const osg::FrameStamp* getFrameStamp() const { return _frameStamp.get(); }
  68. /** Slave allows one to up a camera that follows the master with a local offset to the project and view matrices.*/
  69. struct OSG_EXPORT Slave
  70. {
  71. Slave(bool useMastersSceneData=true):
  72. _useMastersSceneData(useMastersSceneData) {}
  73. Slave(osg::Camera* camera, const osg::Matrixd& projectionOffset, const osg::Matrixd& viewOffset, bool useMastersSceneData=true):
  74. _camera(camera),
  75. _projectionOffset(projectionOffset),
  76. _viewOffset(viewOffset),
  77. _useMastersSceneData(useMastersSceneData) {}
  78. Slave(const Slave& rhs) :
  79. _camera(rhs._camera),
  80. _projectionOffset(rhs._projectionOffset),
  81. _viewOffset(rhs._viewOffset),
  82. _useMastersSceneData(rhs._useMastersSceneData),
  83. _updateSlaveCallback(rhs._updateSlaveCallback) {}
  84. virtual ~Slave() {}
  85. Slave& operator = (const Slave& rhs)
  86. {
  87. _camera = rhs._camera;
  88. _projectionOffset = rhs._projectionOffset;
  89. _viewOffset = rhs._viewOffset;
  90. _useMastersSceneData = rhs._useMastersSceneData;
  91. _updateSlaveCallback = rhs._updateSlaveCallback;
  92. return *this;
  93. }
  94. struct UpdateSlaveCallback : public virtual Referenced
  95. {
  96. virtual void updateSlave(osg::View& view, osg::View::Slave& slave) = 0;
  97. };
  98. void updateSlave(View& view)
  99. {
  100. if (_updateSlaveCallback.valid()) _updateSlaveCallback->updateSlave(view, *this);
  101. else updateSlaveImplementation(view);
  102. }
  103. virtual void updateSlaveImplementation(View& view);
  104. osg::ref_ptr<osg::Camera> _camera;
  105. osg::Matrixd _projectionOffset;
  106. osg::Matrixd _viewOffset;
  107. bool _useMastersSceneData;
  108. osg::ref_ptr<UpdateSlaveCallback> _updateSlaveCallback;
  109. };
  110. bool addSlave(osg::Camera* camera, bool useMastersSceneData=true) { return addSlave(camera, osg::Matrix::identity(), osg::Matrix::identity(), useMastersSceneData); }
  111. bool addSlave(osg::Camera* camera, const osg::Matrix& projectionOffset, const osg::Matrix& viewOffset, bool useMastersSceneData=true);
  112. bool removeSlave(unsigned int pos);
  113. unsigned int getNumSlaves() const { return static_cast<unsigned int>(_slaves.size()); }
  114. Slave& getSlave(unsigned int pos) { return _slaves[pos]; }
  115. const Slave& getSlave(unsigned int pos) const { return _slaves[pos]; }
  116. unsigned int findSlaveIndexForCamera(osg::Camera* camera) const;
  117. Slave * findSlaveForCamera(osg::Camera* camera);
  118. void updateSlaves();
  119. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  120. virtual void releaseGLObjects(osg::State* = 0) const;
  121. protected :
  122. virtual ~View();
  123. virtual osg::GraphicsOperation* createRenderer(osg::Camera*) { return 0; }
  124. osg::ref_ptr<osg::Stats> _stats;
  125. LightingMode _lightingMode;
  126. osg::ref_ptr<osg::Light> _light;
  127. osg::ref_ptr<osg::Camera> _camera;
  128. typedef std::vector<Slave> Slaves;
  129. Slaves _slaves;
  130. osg::ref_ptr<osg::FrameStamp> _frameStamp;
  131. };
  132. }
  133. #endif