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