ClusterCullingCallback 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_CLUSTERCULLINGCALLBACK
  14. #define OSG_CLUSTERCULLINGCALLBACK 1
  15. #include <osg/Drawable>
  16. #include <osg/Callback>
  17. namespace osg {
  18. /** @class ClusterCullingCallback
  19. @brief Implements cluster culling to cull back facing subgraphs and drawables. Derived from Drawable::CullCallback and osg::NodeCallback.
  20. This culling callback is intended to be attached to a node using the setCullCallback method. If the
  21. node is a drawable cull(osg::NodeVisitor*, osg::Drawable*, osg::State*) otherwise
  22. operator()(Node*, NodeVisitor*) will be called during the cull traversal.
  23. To decide whether the node (in case of a drawable) or its children (in case of any other node type) are
  24. to be culled depends on four parameters:
  25. - a control point,
  26. - a normal specified at the control point,
  27. - a deviation value representing the cosinus of an enclosed angle and
  28. - a radius describing a sphere around the control point.
  29. The node is culled if the following two conditions are fulfilled:
  30. - the distance between the current eye/view point to the control point is larger or equal to radius,
  31. - the cosinus of the enclosed angle between the normal and the vector from the control point to the eye/view point
  32. is smaller(!) than the specified deviation value (normally this value is negative meaning that the enclosed angle
  33. between the control point and the eye/view point is larger than the angle indirectly specified by the
  34. deviation value).
  35. @remark As the deviation is representing the cosine of an enclosed angle its value should be within the
  36. the interval [-1; 1]. A value of one will cull all nodes while a value of -1 will never cull a node.
  37. The deviation will normally have negative values because then the enclosed angle between the normal and the
  38. eye/view point is larger than 90 degrees (and therefore the eye sees the "back" from the control point).
  39. */
  40. class OSG_EXPORT ClusterCullingCallback : public DrawableCullCallback, public NodeCallback
  41. {
  42. public:
  43. ClusterCullingCallback();
  44. ClusterCullingCallback(const ClusterCullingCallback& ccc,const CopyOp& copyop);
  45. ClusterCullingCallback(const osg::Vec3& controlPoint, const osg::Vec3& normal, float deviation, float radius=-1.0f);
  46. ClusterCullingCallback(const osg::Drawable* drawable);
  47. META_Object(osg,ClusterCullingCallback);
  48. virtual NodeCallback* asNodeCallback() { return osg::NodeCallback::asNodeCallback(); }
  49. virtual const NodeCallback* asNodeCallback() const { return osg::NodeCallback::asNodeCallback(); }
  50. virtual DrawableCullCallback* asDrawableCullCallback() { return osg::DrawableCullCallback::asDrawableCullCallback(); }
  51. virtual const DrawableCullCallback* asDrawableCullCallback() const { return osg::DrawableCullCallback::asDrawableCullCallback(); }
  52. // use the NodeCallbacks implementation of run.
  53. virtual bool run(osg::Object* object, osg::Object* data) { return NodeCallback::run(object, data); }
  54. /** Computes the control point, normal, and deviation from the
  55. * given drawable contents. */
  56. void computeFrom(const osg::Drawable* drawable);
  57. /** Transform the ClusterCullingCallback's positional members to a new coordinate frame.*/
  58. void transform(const osg::Matrixd& matrix);
  59. void set(const osg::Vec3& controlPoint, const osg::Vec3& normal, float deviation, float radius);
  60. void setControlPoint(const osg::Vec3& controlPoint) { _controlPoint = controlPoint; }
  61. const osg::Vec3& getControlPoint() const { return _controlPoint; }
  62. void setNormal(const osg::Vec3& normal) { _normal = normal; }
  63. const osg::Vec3& getNormal() const { return _normal; }
  64. void setRadius(float radius) { _radius = radius; }
  65. float getRadius() const { return _radius; }
  66. void setDeviation(float deviation) { _deviation = deviation; }
  67. float getDeviation() const { return _deviation; }
  68. virtual bool cull(osg::NodeVisitor*, osg::Drawable*, osg::State*) const;
  69. /** Callback method called by the NodeVisitor when visiting a node.*/
  70. virtual void operator()(Node* node, NodeVisitor* nv);
  71. protected:
  72. virtual ~ClusterCullingCallback() {}
  73. osg::Vec3 _controlPoint;
  74. osg::Vec3 _normal;
  75. float _radius;
  76. float _deviation;
  77. };
  78. }
  79. #endif