From eec378a885ffaa794318cfca7fb4acda97aced8a Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 7 Jun 2004 15:05:22 +0000 Subject: [PATCH] Moved the osg::ClusterCullingCallback into into own header and source file. --- include/osg/ClusterCullingCallback | 66 ++++++++ include/osg/Drawable | 42 ----- src/osg/ClusterCullingCallback.cpp | 161 ++++++++++++++++++++ src/osg/Drawable.cpp | 158 ------------------- src/osg/GNUmakefile | 1 + src/osgPlugins/ive/ClusterCullingCallback.h | 2 +- 6 files changed, 229 insertions(+), 201 deletions(-) create mode 100644 include/osg/ClusterCullingCallback create mode 100644 src/osg/ClusterCullingCallback.cpp diff --git a/include/osg/ClusterCullingCallback b/include/osg/ClusterCullingCallback new file mode 100644 index 000000000..9efe9ffcf --- /dev/null +++ b/include/osg/ClusterCullingCallback @@ -0,0 +1,66 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSG_CLUSTERCULLINGCALLBACK +#define OSG_CLUSTERCULLINGCALLBACK 1 + +#include + +namespace osg { + +/** Drawable CullCallback for adding cluster culling to cull back facing + * drawables.*/ +class SG_EXPORT ClusterCullingCallback : public Drawable::CullCallback +{ + public: + + ClusterCullingCallback(); + ClusterCullingCallback(const ClusterCullingCallback& ccc,const CopyOp& copyop); + ClusterCullingCallback(const osg::Vec3& controlPoint, const osg::Vec3& normal, float deviation); + ClusterCullingCallback(const osg::Drawable* drawable); + + META_Object(osg,ClusterCullingCallback) + + /** compute the control point, normal and deviation from the contents of the drawable.*/ + void computeFrom(const osg::Drawable* drawable); + + void set(const osg::Vec3& controlPoint, const osg::Vec3& normal, float deviation); + + void setControlPoint(const osg::Vec3& controlPoint) { _controlPoint = controlPoint; } + const osg::Vec3& getControlPoint() const { return _controlPoint; } + + void setNormal(const osg::Vec3& normal) { _normal = normal; } + const osg::Vec3& getNormal() const { return _normal; } + + void setRadius(float radius) { _radius = radius; } + float getRadius() const { return _radius; } + + void setDeviation(float deviation) { _deviation = deviation; } + float getDeviation() const { return _deviation; } + + virtual bool cull(osg::NodeVisitor*, osg::Drawable*, osg::State*) const; + + protected: + + virtual ~ClusterCullingCallback() {} + + osg::Vec3 _controlPoint; + osg::Vec3 _normal; + float _radius; + float _deviation; +}; + + +} + +#endif diff --git a/include/osg/Drawable b/include/osg/Drawable index 1e7cdc1d9..e8d638b44 100644 --- a/include/osg/Drawable +++ b/include/osg/Drawable @@ -778,48 +778,6 @@ inline void Drawable::draw(State& state) const drawImplementation(state); }; -/** Drawable CullCallback for adding cluster culling to cull back facing - * drawables.*/ -class SG_EXPORT ClusterCullingCallback : public Drawable::CullCallback -{ - public: - - ClusterCullingCallback(); - ClusterCullingCallback(const ClusterCullingCallback& ccc,const CopyOp& copyop); - ClusterCullingCallback(const osg::Vec3& controlPoint, const osg::Vec3& normal, float deviation); - ClusterCullingCallback(const osg::Drawable* drawable); - - META_Object(osg,ClusterCullingCallback) - - /** compute the control point, normal and deviation from the contents of the drawable.*/ - void computeFrom(const osg::Drawable* drawable); - - void set(const osg::Vec3& controlPoint, const osg::Vec3& normal, float deviation); - - void setControlPoint(const osg::Vec3& controlPoint) { _controlPoint = controlPoint; } - const osg::Vec3& getControlPoint() const { return _controlPoint; } - - void setNormal(const osg::Vec3& normal) { _normal = normal; } - const osg::Vec3& getNormal() const { return _normal; } - - void setRadius(float radius) { _radius = radius; } - float getRadius() const { return _radius; } - - void setDeviation(float deviation) { _deviation = deviation; } - float getDeviation() const { return _deviation; } - - virtual bool cull(osg::NodeVisitor*, osg::Drawable*, osg::State*) const; - - protected: - - virtual ~ClusterCullingCallback() {} - - osg::Vec3 _controlPoint; - osg::Vec3 _normal; - float _radius; - float _deviation; -}; - } diff --git a/src/osg/ClusterCullingCallback.cpp b/src/osg/ClusterCullingCallback.cpp new file mode 100644 index 000000000..d4e6d1894 --- /dev/null +++ b/src/osg/ClusterCullingCallback.cpp @@ -0,0 +1,161 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ +#include +#include + +using namespace osg; + + +/////////////////////////////////////////////////////////////////////////////////////////// +// +// Cluster culling callback +// + +ClusterCullingCallback::ClusterCullingCallback(): + _radius(-1.0f), + _deviation(-1.0f) +{ +} + +ClusterCullingCallback::ClusterCullingCallback(const ClusterCullingCallback& ccc,const CopyOp& copyop): + Drawable::CullCallback(ccc,copyop), + _controlPoint(ccc._controlPoint),_normal(ccc._normal),_deviation(ccc._deviation) +{ +} + +ClusterCullingCallback::ClusterCullingCallback(const osg::Vec3& controlPoint, const osg::Vec3& normal, float deviation): + _controlPoint(controlPoint),_normal(normal), _deviation(deviation) +{ +} + +ClusterCullingCallback::ClusterCullingCallback(const osg::Drawable* drawable) +{ + computeFrom(drawable); +} + +struct ComputeAveragesFunctor +{ + + ComputeAveragesFunctor(): + _num(0) {} + + inline void operator() ( const osg::Vec3 &v1, const osg::Vec3 &v2, const osg::Vec3 &v3, bool) + { + // calc orientation of triangle. + osg::Vec3d normal = (v2-v1)^(v3-v1); + if (normal.normalize()!=0.0f) + { + _normal += normal; + } + _center += v1; + _center += v2; + _center += v3; + + ++_num; + + } + + osg::Vec3 center() { return _center / (double)(3*_num); } + osg::Vec3 normal() { _normal.normalize(); return _normal; } + + unsigned int _num; + Vec3d _center; + Vec3d _normal; +}; + +struct ComputeDeviationFunctor +{ + + ComputeDeviationFunctor(): + _deviation(1.0), + _radius2(0.0) {} + + void set(const osg::Vec3& center,const osg::Vec3& normal) + { + _center = center; + _normal = normal; + } + + inline void operator() ( const osg::Vec3 &v1, const osg::Vec3 &v2, const osg::Vec3 &v3, bool) + { + // calc orientation of triangle. + osg::Vec3 normal = (v2-v1)^(v3-v1); + if (normal.normalize()!=0.0f) + { + _deviation = osg::minimum(_normal*normal,_deviation); + } + _radius2 = osg::maximum((v1-_center).length2(),_radius2); + _radius2 = osg::maximum((v2-_center).length2(),_radius2); + _radius2 = osg::maximum((v3-_center).length2(),_radius2); + + } + osg::Vec3 _center; + osg::Vec3 _normal; + float _deviation; + float _radius2; +}; + + +void ClusterCullingCallback::computeFrom(const osg::Drawable* drawable) +{ + TriangleFunctor caf; + drawable->accept(caf); + + _controlPoint = caf.center(); + _normal = caf.normal(); + + TriangleFunctor cdf; + cdf.set(_controlPoint,_normal); + drawable->accept(cdf); + +// osg::notify(osg::NOTICE)<<"ClusterCullingCallback::computeFrom() _controlPoint="<<_controlPoint< caf; - drawable->accept(caf); - - _controlPoint = caf.center(); - _normal = caf.normal(); - - TriangleFunctor cdf; - cdf.set(_controlPoint,_normal); - drawable->accept(cdf); - -// osg::notify(osg::NOTICE)<<"ClusterCullingCallback::computeFrom() _controlPoint="<<_controlPoint<