Made the Drawable::*Callback derive from osg::Object so that they can be

saved to .osg properly.

Added osg::ClusterCullingCallback to Drawable header/source.  Not complete yet,
but will enable drawables to culled is they are facing away from the eye point.
This commit is contained in:
Robert Osfield 2003-10-08 21:29:45 +00:00
parent 83816dac04
commit 1a6dc82d32
3 changed files with 166 additions and 24 deletions

View File

@ -247,23 +247,38 @@ class SG_EXPORT Drawable : public Object
virtual void compile(State& state) const;
struct UpdateCallback : public virtual osg::Referenced
struct UpdateCallback : public virtual osg::Object
{
/** do customized app code.*/
virtual void update(osg::NodeVisitor *visitor, osg::Drawable* drawable) = 0;
UpdateCallback() {}
UpdateCallback(const UpdateCallback&,const CopyOp&) {}
META_Object(osg,UpdateCallback)
/** do customized update code.*/
virtual void update(osg::NodeVisitor*, osg::Drawable*) {}
};
/** Set the UpdateCallback which allows users to attach customize the undating of an object during the app traversal.*/
/** Set the UpdateCallback which allows users to attach customize the undating of an object during the update traversal.*/
void setUpdateCallback(UpdateCallback* ac);
/** Get the non const UpdateCallback.*/
UpdateCallback* getUpdateCallback() { return _updateCallback.get(); }
/** Get the const UpdateCallback.*/
const UpdateCallback* getUpdateCallback() const { return _updateCallback.get(); }
struct CullCallback : public virtual osg::Referenced
struct CullCallback : public virtual osg::Object
{
/** do customized cull code.*/
virtual bool cull(osg::NodeVisitor *visitor, osg::Drawable* drawable, osg::State *state=NULL) const = 0;
CullCallback() {}
CullCallback(const CullCallback&,const CopyOp&) {}
META_Object(osg,CullCallback)
/** do customized cull code, return true if drawable should be culled.*/
virtual bool cull(osg::NodeVisitor*, osg::Drawable*, osg::State*) const { return false; }
};
/** Set the CullCallback which allows users to attach customize the culling of Drawable during the cull traversal.*/
@ -276,15 +291,23 @@ class SG_EXPORT Drawable : public Object
const CullCallback* getCullCallback() const { return _cullCallback.get(); }
/** Callback attached to an Drawable which allows the users to customize the drawing of an exist Drawable object.
* The draw callback is implement as a replacement to the Drawable's own drawImplementation() method, if the
* the user intends to decorate the exist draw code then simple call the drawable->drawImplementation() from
* with the callbacks drawImplementation() method. This allows the users to do both pre and post callbacks
* without fuss and can even diable the inner draw in required.*/
struct DrawCallback : public virtual osg::Referenced
struct DrawCallback : public virtual osg::Object
{
DrawCallback() {}
DrawCallback(const DrawCallback&,const CopyOp&) {}
META_Object(osg,DrawCallback)
/** do customized draw code.*/
virtual void drawImplementation(State& state,const osg::Drawable* drawable) const = 0;
virtual void drawImplementation(State&,const osg::Drawable*) const {}
};
/** Set the DrawCallback which allows users to attach customize the drawing of existing Drawable object.*/
@ -684,6 +707,44 @@ 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 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 _deviation;
};
}

View File

@ -20,6 +20,7 @@
#include <osg/Node>
#include <osg/GLExtensions>
#include <osg/Timer>
#include <osg/TriangleFunctor>
#include <algorithm>
#include <map>
@ -891,3 +892,83 @@ void Drawable::Extensions::glGetOcclusionQueryuiv( GLuint id, GLenum pname, GLui
osg::notify(osg::WARN)<<"Error: glGetOcclusionQueryuiv not supported by OpenGL driver"<<std::endl;
}
}
///////////////////////////////////////////////////////////////////////////////////////////
//
// Cluster culling callback
//
ClusterCullingCallback::ClusterCullingCallback():
_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 CollectNormalsFunctor
{
CollectNormalsFunctor():
_x(0.0),_y(0.0),_z(0) {}
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);
normal.normalize();
_normals.push_back(normal);
_x += v1.x();
_y += v1.y();
_z += v1.z();
_x += v2.x();
_y += v2.y();
_z += v2.z();
_x += v3.x();
_y += v3.y();
_z += v3.z();
}
typedef std::vector<osg::Vec3> NormalList;
NormalList _normals;
double _x,_y,_z;
};
void ClusterCullingCallback::computeFrom(const osg::Drawable* drawable)
{
TriangleFunctor<CollectNormalsFunctor> stf;
drawable->accept(stf);
}
void ClusterCullingCallback::set(const osg::Vec3& controlPoint, const osg::Vec3& normal, float deviation)
{
_controlPoint = controlPoint;
_normal = normal;
_deviation = deviation;
}
bool ClusterCullingCallback::cull(osg::NodeVisitor*, osg::Drawable*, osg::State*) const
{
return false;
}

View File

@ -15,7 +15,7 @@ using namespace osgDB;
DatabasePager::DatabasePager()
{
//std::cout<<"Constructing DatabasePager()"<<std::endl;
//osg::notify(osg::INFO)<<"Constructing DatabasePager()"<<std::endl;
_deleteRemovedSubgraphsInDatabaseThread = true;
@ -178,7 +178,7 @@ public:
if (drawable->getUseDisplayList() || drawable->getUseVertexBufferObjects())
{
//std::cout<<"Found compilable drawable"<<std::endl;
//osg::notify(osg::INFO)<<"Found compilable drawable"<<std::endl;
_dataToCompile.second.push_back(drawable);
}
}
@ -189,7 +189,7 @@ public:
void DatabasePager::run()
{
std::cout<<"DatabasePager::run()"<<std::endl;
osg::notify(osg::NOTICE)<<"DatabasePager::run()"<<std::endl;
// need to set the texture object manager to be able to reuse textures
// by keeping deleted texture objects around for 10 seconds after being deleted.
@ -206,7 +206,7 @@ void DatabasePager::run()
_childrenToDeleteListMutex.lock();
if (!_childrenToDeleteList.empty())
{
std::cout<<"In DatabasePager thread deleting "<<_childrenToDeleteList.size()<<" subgraphs"<<std::endl;
osg::notify(osg::INFO)<<"In DatabasePager thread deleting "<<_childrenToDeleteList.size()<<" subgraphs"<<std::endl;
_childrenToDeleteList.clear();
}
_childrenToDeleteListMutex.unlock();
@ -323,7 +323,7 @@ void DatabasePager::addLoadedDataToSceneGraph(double timeStamp)
plod->setTimeStamp(plod->getNumChildren(),timeStamp);
}
group->addChild(databaseRequest->_loadedModel.get());
std::cout<<"merged subgraph"<<databaseRequest->_fileName<<" after "<<databaseRequest->_numOfRequests<<" requests."<<std::endl;
osg::notify(osg::INFO)<<"merged subgraph"<<databaseRequest->_fileName<<" after "<<databaseRequest->_numOfRequests<<" requests."<<std::endl;
}
@ -387,7 +387,7 @@ void DatabasePager::removeExpiredSubgraphs(double currentFrameTime)
osg::NodeList childrenRemoved;
//std::cout<<"DatabasePager::removeExpiredSubgraphs("<<expiryTime<<") "<<std::endl;
//osg::notify(osg::INFO)<<"DatabasePager::removeExpiredSubgraphs("<<expiryTime<<") "<<std::endl;
for(PagedLODList::iterator itr=_pagedLODList.begin();
itr!=_pagedLODList.end();
++itr)
@ -405,12 +405,12 @@ void DatabasePager::removeExpiredSubgraphs(double currentFrameTime)
osg::PagedLOD* plod = _pagedLODList[i].get();
if (plod->referenceCount()==1)
{
//std::cout<<" PagedLOD "<<plod<<" orphaned"<<std::endl;
//osg::notify(osg::INFO)<<" PagedLOD "<<plod<<" orphaned"<<std::endl;
_pagedLODList.erase(_pagedLODList.begin()+i);
}
else
{
//std::cout<<" PagedLOD "<<plod<<" refcount "<<plod->referenceCount()<<std::endl;
//osg::notify(osg::INFO)<<" PagedLOD "<<plod<<" refcount "<<plod->referenceCount()<<std::endl;
}
}
@ -505,13 +505,13 @@ void DatabasePager::compileRenderingObjects(osg::State& state, double& available
{
// we have StateSet's to compile
StateSetList& sslist = dtc.first;
//std::cout<<"Compiling statesets"<<std::endl;
//osg::notify(osg::INFO)<<"Compiling statesets"<<std::endl;
StateSetList::iterator itr=sslist.begin();
for(;
itr!=sslist.end() && elapsedTime<availableTime;
++itr)
{
//std::cout<<" Compiling stateset "<<(*itr).get()<<std::endl;
//osg::notify(osg::INFO)<<" Compiling stateset "<<(*itr).get()<<std::endl;
(*itr)->compile(state);
elapsedTime = timer.delta_s(start_tick,timer.tick());
}
@ -521,14 +521,14 @@ void DatabasePager::compileRenderingObjects(osg::State& state, double& available
if (!dtc.second.empty() && elapsedTime<availableTime)
{
// we have Drawable's to compile
//std::cout<<"Compiling drawables"<<std::endl;
//osg::notify(osg::INFO)<<"Compiling drawables"<<std::endl;
DrawableList& dwlist = dtc.second;
DrawableList::iterator itr=dwlist.begin();
for(;
itr!=dwlist.end() && elapsedTime<availableTime;
++itr)
{
//std::cout<<" Compiling drawable "<<(*itr).get()<<std::endl;
//osg::notify(osg::INFO)<<" Compiling drawable "<<(*itr).get()<<std::endl;
(*itr)->compile(state);
elapsedTime = timer.delta_s(start_tick,timer.tick());
}
@ -536,7 +536,7 @@ void DatabasePager::compileRenderingObjects(osg::State& state, double& available
dwlist.erase(dwlist.begin(),itr);
}
//std::cout<<"Checking if compiled"<<std::endl;
//osg::notify(osg::INFO)<<"Checking if compiled"<<std::endl;
// now check the to compile entries for all active graphics contexts
// to make sure that all have been compiled.
@ -552,7 +552,7 @@ void DatabasePager::compileRenderingObjects(osg::State& state, double& available
if (allCompiled)
{
std::cout<<"All compiled"<<std::endl;
osg::notify(osg::INFO)<<"All compiled"<<std::endl;
// we've compile all of the current databaseRequest so we can now pop it off the
// to compile list and place it on the merge list.
@ -571,7 +571,7 @@ void DatabasePager::compileRenderingObjects(osg::State& state, double& available
}
else
{
//std::cout<<"Not all compiled"<<std::endl;
//osg::notify(osg::INFO)<<"Not all compiled"<<std::endl;
databaseRequest = 0;
}