2003-01-22 00:45:36 +08:00
|
|
|
/* -*-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.
|
|
|
|
*/
|
2001-09-20 05:19:47 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
#include <osg/Drawable>
|
|
|
|
#include <osg/State>
|
|
|
|
#include <osg/Notify>
|
2002-02-09 06:55:21 +08:00
|
|
|
#include <osg/Node>
|
|
|
|
|
|
|
|
#include <algorithm>
|
2002-11-19 18:56:59 +08:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2002-11-19 18:56:59 +08:00
|
|
|
// static cache of deleted display lists which can only
|
|
|
|
// by completely deleted once the appropriate OpenGL context
|
|
|
|
// is set. Used osg::Drawable::deleteDisplayList(..) and flushDeletedDisplayLists(..) below.
|
2003-01-23 07:34:18 +08:00
|
|
|
typedef std::vector<GLuint> DisplayListVector;
|
|
|
|
typedef std::map<GLuint,DisplayListVector> DeletedDisplayListCache;
|
2002-11-19 18:56:59 +08:00
|
|
|
static DeletedDisplayListCache s_deletedDisplayListCache;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2003-02-14 19:41:52 +08:00
|
|
|
void Drawable::deleteDisplayList(unsigned int contextID,GLuint globj)
|
2003-01-23 07:34:18 +08:00
|
|
|
{
|
|
|
|
if (globj!=0)
|
|
|
|
{
|
|
|
|
// insert the globj into the cache for the appropriate context.
|
|
|
|
s_deletedDisplayListCache[contextID].push_back(globj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** flush all the cached display list which need to be deleted
|
|
|
|
* in the OpenGL context related to contextID.*/
|
2003-02-14 19:41:52 +08:00
|
|
|
void Drawable::flushDeletedDisplayLists(unsigned int contextID)
|
2003-01-23 07:34:18 +08:00
|
|
|
{
|
|
|
|
DeletedDisplayListCache::iterator citr = s_deletedDisplayListCache.find(contextID);
|
|
|
|
if (citr!=s_deletedDisplayListCache.end())
|
|
|
|
{
|
|
|
|
DisplayListVector displayListSet;
|
|
|
|
|
|
|
|
// this swap will transfer the content of and empty citr->second
|
|
|
|
// in one quick pointer change.
|
|
|
|
displayListSet.swap(citr->second);
|
|
|
|
|
2003-01-23 16:37:15 +08:00
|
|
|
for(DisplayListVector::iterator gitr=displayListSet.begin();
|
2003-01-23 07:34:18 +08:00
|
|
|
gitr!=displayListSet.end();
|
|
|
|
++gitr)
|
|
|
|
{
|
|
|
|
glDeleteLists(*gitr,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
Drawable::Drawable()
|
|
|
|
{
|
|
|
|
_bbox_computed = false;
|
|
|
|
|
|
|
|
// Note, if your are defining a subclass from drawable which is
|
|
|
|
// dynamically updated then you should set both the following to
|
|
|
|
// to false in your constructor. This will prevent any display
|
|
|
|
// lists from being automatically created and safeguard the
|
|
|
|
// dynamic updating of data.
|
|
|
|
_supportsDisplayList = true;
|
|
|
|
_useDisplayList = true;
|
|
|
|
}
|
|
|
|
|
2002-02-07 09:07:11 +08:00
|
|
|
Drawable::Drawable(const Drawable& drawable,const CopyOp& copyop):
|
|
|
|
Object(drawable,copyop),
|
2002-02-09 06:55:21 +08:00
|
|
|
_parents(), // leave empty as parentList is managed by Geode
|
2002-09-02 20:31:35 +08:00
|
|
|
_stateset(copyop(drawable._stateset.get())),
|
2002-10-30 21:27:15 +08:00
|
|
|
_bbox(drawable._bbox),
|
|
|
|
_bbox_computed(drawable._bbox_computed),
|
|
|
|
_shape(copyop(drawable._shape.get())),
|
2002-02-07 09:07:11 +08:00
|
|
|
_supportsDisplayList(drawable._supportsDisplayList),
|
|
|
|
_useDisplayList(drawable._useDisplayList),
|
2002-03-14 06:44:22 +08:00
|
|
|
_drawCallback(drawable._drawCallback),
|
|
|
|
_cullCallback(drawable._cullCallback)
|
2002-11-19 18:56:59 +08:00
|
|
|
{
|
|
|
|
}
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
Drawable::~Drawable()
|
|
|
|
{
|
|
|
|
dirtyDisplayList();
|
|
|
|
}
|
|
|
|
|
2002-02-09 06:55:21 +08:00
|
|
|
void Drawable::addParent(osg::Node* node)
|
|
|
|
{
|
|
|
|
_parents.push_back(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Drawable::removeParent(osg::Node* node)
|
|
|
|
{
|
|
|
|
ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),node);
|
|
|
|
if (pitr!=_parents.end()) _parents.erase(pitr);
|
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
osg::StateSet* Drawable::getOrCreateStateSet()
|
|
|
|
{
|
2002-12-16 21:40:58 +08:00
|
|
|
if (!_stateset) _stateset = new StateSet;
|
2002-09-02 20:31:35 +08:00
|
|
|
return _stateset.get();
|
|
|
|
}
|
|
|
|
|
2002-02-09 06:55:21 +08:00
|
|
|
void Drawable::dirtyBound()
|
|
|
|
{
|
|
|
|
if (_bbox_computed)
|
|
|
|
{
|
|
|
|
_bbox_computed = false;
|
|
|
|
|
|
|
|
// dirty parent bounding sphere's to ensure that all are valid.
|
|
|
|
for(ParentList::iterator itr=_parents.begin();
|
|
|
|
itr!=_parents.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
(*itr)->dirtyBound();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2003-01-21 04:40:06 +08:00
|
|
|
void Drawable::compile(State& state) const
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
if (!_useDisplayList) return;
|
|
|
|
|
|
|
|
// get the contextID (user defined ID of 0 upwards) for the
|
|
|
|
// current OpenGL context.
|
2003-02-14 19:41:52 +08:00
|
|
|
unsigned int contextID = state.getContextID();
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
// get the globj for the current contextID.
|
2003-02-15 00:52:47 +08:00
|
|
|
GLuint& globj = _globjList[contextID];
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
// call the globj if already set otherwise comple and execute.
|
|
|
|
if( globj != 0 )
|
|
|
|
{
|
|
|
|
glDeleteLists( globj, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
if (_stateset.valid())
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
2002-09-02 20:31:35 +08:00
|
|
|
_stateset->compile(state);
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
globj = glGenLists( 1 );
|
|
|
|
glNewList( globj, GL_COMPILE );
|
2002-05-29 07:43:22 +08:00
|
|
|
|
|
|
|
if (_drawCallback.valid())
|
2002-11-06 23:43:11 +08:00
|
|
|
_drawCallback->drawImplementation(state,this);
|
2002-05-29 07:43:22 +08:00
|
|
|
else
|
2002-11-06 23:43:11 +08:00
|
|
|
drawImplementation(state);
|
2002-05-29 07:43:22 +08:00
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
glEndList();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
void Drawable::setSupportsDisplayList(bool flag)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
// if value unchanged simply return.
|
|
|
|
if (_supportsDisplayList==flag) return;
|
|
|
|
|
|
|
|
// if previously set to true then need to check about display lists.
|
|
|
|
if (_supportsDisplayList)
|
|
|
|
{
|
|
|
|
if (_useDisplayList)
|
|
|
|
{
|
|
|
|
// used to support display lists and display lists switched
|
|
|
|
// on so now delete them and turn useDisplayList off.
|
|
|
|
dirtyDisplayList();
|
|
|
|
_useDisplayList = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set with new value.
|
|
|
|
_supportsDisplayList=flag;
|
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
void Drawable::setUseDisplayList(bool flag)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
// if value unchanged simply return.
|
|
|
|
if (_useDisplayList==flag) return;
|
|
|
|
|
|
|
|
// if was previously set to true, remove display list.
|
|
|
|
if (_useDisplayList)
|
|
|
|
{
|
|
|
|
dirtyDisplayList();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_supportsDisplayList)
|
|
|
|
{
|
|
|
|
|
|
|
|
// set with new value.
|
|
|
|
_useDisplayList = flag;
|
|
|
|
|
|
|
|
}
|
|
|
|
else // does not support display lists.
|
|
|
|
{
|
|
|
|
if (flag)
|
|
|
|
{
|
2001-12-15 07:18:28 +08:00
|
|
|
notify(WARN)<<"Warning: attempt to setUseDisplayList(true) on a drawable with does not support display lists."<<std::endl;
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// set with new value.
|
|
|
|
_useDisplayList = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Drawable::dirtyDisplayList()
|
|
|
|
{
|
2003-02-14 19:41:52 +08:00
|
|
|
for(unsigned int i=0;i<_globjList.size();++i)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
if (_globjList[i] != 0)
|
|
|
|
{
|
|
|
|
Drawable::deleteDisplayList(i,_globjList[i]);
|
|
|
|
_globjList[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-10 23:35:47 +08:00
|
|
|
|
2002-12-19 23:55:40 +08:00
|
|
|
void Drawable::setUpdateCallback(UpdateCallback* ac)
|
2002-07-10 23:35:47 +08:00
|
|
|
{
|
2002-12-19 23:55:40 +08:00
|
|
|
if (_updateCallback==ac) return;
|
2002-07-10 23:35:47 +08:00
|
|
|
|
|
|
|
int delta = 0;
|
2002-12-19 23:55:40 +08:00
|
|
|
if (_updateCallback.valid()) --delta;
|
2002-07-10 23:35:47 +08:00
|
|
|
if (ac) ++delta;
|
2002-07-11 04:30:57 +08:00
|
|
|
|
2002-12-19 23:55:40 +08:00
|
|
|
_updateCallback = ac;
|
2002-07-10 23:35:47 +08:00
|
|
|
|
|
|
|
if (delta!=0)
|
|
|
|
{
|
|
|
|
for(ParentList::iterator itr=_parents.begin();
|
|
|
|
itr!=_parents.end();
|
|
|
|
++itr)
|
|
|
|
{
|
2002-12-19 23:55:40 +08:00
|
|
|
(*itr)->setNumChildrenRequiringUpdateTraversal((*itr)->getNumChildrenRequiringUpdateTraversal()+delta);
|
2002-07-10 23:35:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-07-19 06:35:54 +08:00
|
|
|
|
|
|
|
struct ComputeBound : public Drawable::PrimitiveFunctor
|
|
|
|
{
|
|
|
|
ComputeBound():_vertices(0) {}
|
|
|
|
|
2002-11-06 18:24:33 +08:00
|
|
|
virtual void setVertexArray(unsigned int,const Vec3* vertices) { _vertices = vertices; }
|
2002-07-19 06:35:54 +08:00
|
|
|
|
|
|
|
virtual void drawArrays(GLenum,GLint first,GLsizei count)
|
|
|
|
{
|
|
|
|
if (_vertices)
|
|
|
|
{
|
2002-11-06 18:24:33 +08:00
|
|
|
const osg::Vec3* vert = _vertices+first;
|
2002-07-19 06:35:54 +08:00
|
|
|
for(;count>0;--count,++vert)
|
|
|
|
{
|
|
|
|
_bb.expandBy(*vert);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-06 18:24:33 +08:00
|
|
|
virtual void drawElements(GLenum,GLsizei count,const GLubyte* indices)
|
2002-07-19 06:35:54 +08:00
|
|
|
{
|
|
|
|
if (_vertices)
|
|
|
|
{
|
|
|
|
for(;count>0;--count,++indices)
|
|
|
|
{
|
|
|
|
_bb.expandBy(_vertices[*indices]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-06 18:24:33 +08:00
|
|
|
virtual void drawElements(GLenum,GLsizei count,const GLushort* indices)
|
2002-07-19 06:35:54 +08:00
|
|
|
{
|
|
|
|
if (_vertices)
|
|
|
|
{
|
|
|
|
for(;count>0;--count,++indices)
|
|
|
|
{
|
|
|
|
_bb.expandBy(_vertices[*indices]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-06 18:24:33 +08:00
|
|
|
virtual void drawElements(GLenum,GLsizei count,const GLuint* indices)
|
2002-07-19 06:35:54 +08:00
|
|
|
{
|
|
|
|
if (_vertices)
|
|
|
|
{
|
|
|
|
for(;count>0;--count,++indices)
|
|
|
|
{
|
|
|
|
_bb.expandBy(_vertices[*indices]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void begin(GLenum) {}
|
|
|
|
virtual void vertex(const Vec3& vert) { _bb.expandBy(vert); }
|
|
|
|
virtual void vertex(float x,float y,float z) { _bb.expandBy(x,y,z); }
|
|
|
|
virtual void end() {}
|
|
|
|
|
2002-11-06 18:24:33 +08:00
|
|
|
const Vec3* _vertices;
|
2002-07-19 06:35:54 +08:00
|
|
|
BoundingBox _bb;
|
|
|
|
};
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
bool Drawable::computeBound() const
|
2002-07-19 06:35:54 +08:00
|
|
|
{
|
|
|
|
ComputeBound cb;
|
|
|
|
|
|
|
|
Drawable* non_const_this = const_cast<Drawable*>(this);
|
|
|
|
non_const_this->accept(cb);
|
|
|
|
|
|
|
|
_bbox = cb._bb;
|
|
|
|
_bbox_computed = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-03-12 05:53:28 +08:00
|
|
|
void Drawable::setBound(const BoundingBox& bb) const
|
|
|
|
{
|
|
|
|
_bbox = bb;
|
|
|
|
_bbox_computed = true;
|
|
|
|
}
|