2001-01-11 00:32:10 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
2001-09-28 00:34:41 +08:00
|
|
|
|
|
|
|
#include <osg/Billboard>
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
|
|
|
#define square(x) ((x)*(x))
|
|
|
|
|
|
|
|
Billboard::Billboard()
|
|
|
|
{
|
|
|
|
_mode = AXIAL_ROT;
|
|
|
|
_axis.set(0.0f,0.0f,1.0f);
|
2002-08-10 00:27:39 +08:00
|
|
|
_normal.set(0.0f,-1.0f,0.0f);
|
|
|
|
updateCache();
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2002-01-29 22:04:06 +08:00
|
|
|
Billboard::Billboard(const Billboard& billboard,const CopyOp& copyop):
|
|
|
|
Geode(billboard,copyop),
|
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
|
|
|
_mode(billboard._mode),
|
|
|
|
_axis(billboard._axis),
|
2002-08-10 00:27:39 +08:00
|
|
|
_normal(billboard._normal),
|
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
|
|
|
_positionList(billboard._positionList),
|
2002-08-10 00:27:39 +08:00
|
|
|
_cachedMode(billboard._cachedMode),
|
|
|
|
_side(billboard._side) {}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
Billboard::~Billboard()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
void Billboard::setMode(Mode mode)
|
2002-01-19 06:15:59 +08:00
|
|
|
{
|
|
|
|
_mode = mode;
|
2002-08-10 00:27:39 +08:00
|
|
|
_cachedMode = CACHE_DIRTY;
|
|
|
|
updateCache();
|
2002-01-19 06:15:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Billboard::setAxis(const Vec3& axis)
|
|
|
|
{
|
|
|
|
_axis = axis;
|
2002-08-10 00:27:39 +08:00
|
|
|
_axis.normalize();
|
|
|
|
updateCache();
|
2002-01-19 06:15:59 +08:00
|
|
|
}
|
|
|
|
|
2002-08-10 00:27:39 +08:00
|
|
|
void Billboard::setNormal(const Vec3& normal)
|
|
|
|
{
|
|
|
|
_normal = normal;
|
|
|
|
_normal.normalize();
|
|
|
|
updateCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Billboard::updateCache()
|
2002-01-19 06:15:59 +08:00
|
|
|
{
|
|
|
|
if (_mode==AXIAL_ROT)
|
|
|
|
{
|
2002-08-10 00:27:39 +08:00
|
|
|
if (_axis==Vec3(1.0f,0.0,0.0f) && _normal==Vec3(0.0f,-1.0,0.0f)) _cachedMode = AXIAL_ROT_X_AXIS;
|
|
|
|
else if (_axis==Vec3(0.0f,1.0,0.0f) && _normal==Vec3(1.0f, 0.0,0.0f)) _cachedMode = AXIAL_ROT_Y_AXIS;
|
|
|
|
else if (_axis==Vec3(0.0f,0.0,1.0f) && _normal==Vec3(0.0f,-1.0,0.0f)) _cachedMode = AXIAL_ROT_Z_AXIS;
|
|
|
|
else _cachedMode = AXIAL_ROT;
|
2002-01-19 06:15:59 +08:00
|
|
|
}
|
|
|
|
else _cachedMode = _mode;
|
2002-08-10 00:27:39 +08:00
|
|
|
|
|
|
|
_side = _axis^_normal;
|
|
|
|
_side.normalize();
|
2002-01-19 06:15:59 +08:00
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
bool Billboard::addDrawable(Drawable *gset)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
if (Geode::addDrawable(gset))
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
Vec3 pos(0.0f,0.0f,0.0f);
|
2001-09-20 05:08:56 +08:00
|
|
|
while (_positionList.size()<_drawables.size())
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
_positionList.push_back(pos);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
bool Billboard::addDrawable(Drawable *gset,const Vec3& pos)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
if (Geode::addDrawable(gset))
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
while (_positionList.size()<_drawables.size())
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
_positionList.push_back(pos);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
bool Billboard::removeDrawable( Drawable *gset )
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
PositionList::iterator pitr = _positionList.begin();
|
2001-09-20 05:08:56 +08:00
|
|
|
for (DrawableList::iterator itr=_drawables.begin();
|
|
|
|
itr!=_drawables.end();
|
|
|
|
++itr,++pitr)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
if (itr->get()==gset)
|
|
|
|
{
|
|
|
|
// note ref_ptr<> automatically handles decrementing gset's reference count.
|
2001-09-20 05:08:56 +08:00
|
|
|
_drawables.erase(itr);
|
2001-01-11 00:32:10 +08:00
|
|
|
_positionList.erase(pitr);
|
|
|
|
_bsphere_computed = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
return false;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
bool Billboard::computeMatrix(Matrix& modelview, const Vec3& eye_local, const Vec3& pos_local) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-04-17 17:48:19 +08:00
|
|
|
//Vec3 up_local(matrix(0,1),matrix(1,1),matrix(2,1));
|
|
|
|
|
|
|
|
Matrix matrix;
|
2002-02-07 09:07:11 +08:00
|
|
|
|
2002-08-10 00:27:39 +08:00
|
|
|
Vec3 ev(eye_local-pos_local);
|
2002-01-19 06:15:59 +08:00
|
|
|
switch(_cachedMode)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-02-07 09:07:11 +08:00
|
|
|
case(AXIAL_ROT_Z_AXIS): // need to implement
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-08-10 00:27:39 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
ev.z() = 0.0f;
|
|
|
|
float ev_length = ev.length();
|
2001-09-20 05:08:56 +08:00
|
|
|
if (ev_length>0.0f)
|
|
|
|
{
|
2002-01-19 06:15:59 +08:00
|
|
|
|
2002-02-07 09:07:11 +08:00
|
|
|
matrix.makeIdentity();
|
2001-01-11 00:32:10 +08:00
|
|
|
//float rotation_zrotation_z = atan2f(ev.x(),ev.y());
|
2001-12-13 04:29:10 +08:00
|
|
|
//mat.makeRotate(inRadians(rotation_z),0.0f,0.0f,1.0f);
|
2001-01-11 00:32:10 +08:00
|
|
|
float inv = 1.0f/ev_length;
|
|
|
|
float s = ev.x()*inv;
|
2002-08-10 00:27:39 +08:00
|
|
|
float c = -ev.y()*inv;
|
2002-02-07 09:07:11 +08:00
|
|
|
matrix(0,0) = c;
|
2002-08-10 00:27:39 +08:00
|
|
|
matrix(1,0) = -s;
|
|
|
|
matrix(0,1) = s;
|
2002-02-07 09:07:11 +08:00
|
|
|
matrix(1,1) = c;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2002-08-10 00:27:39 +08:00
|
|
|
case(AXIAL_ROT_Y_AXIS): // need to implement
|
|
|
|
{
|
|
|
|
ev.y() = 0.0f;
|
|
|
|
float ev_length = ev.length();
|
|
|
|
if (ev_length>0.0f)
|
|
|
|
{
|
2002-01-19 06:15:59 +08:00
|
|
|
|
2002-08-10 00:27:39 +08:00
|
|
|
matrix.makeIdentity();
|
|
|
|
//float rotation_zrotation_z = atan2f(ev.x(),ev.y());
|
|
|
|
//mat.makeRotate(inRadians(rotation_z),0.0f,0.0f,1.0f);
|
|
|
|
float inv = 1.0f/ev_length;
|
|
|
|
float s = -ev.z()*inv;
|
|
|
|
float c = ev.x()*inv;
|
|
|
|
matrix(0,0) = c;
|
|
|
|
matrix(2,0) = s;
|
|
|
|
matrix(0,2) = -s;
|
|
|
|
matrix(2,2) = c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(AXIAL_ROT_X_AXIS): // implemented correctly..
|
|
|
|
{
|
|
|
|
ev.x() = 0.0f;
|
|
|
|
float ev_length = ev.length();
|
|
|
|
if (ev_length>0.0f)
|
|
|
|
{
|
|
|
|
|
|
|
|
matrix.makeIdentity();
|
|
|
|
//float rotation_zrotation_z = atan2f(ev.x(),ev.y());
|
|
|
|
//mat.makeRotate(inRadians(rotation_z),0.0f,0.0f,1.0f);
|
|
|
|
float inv = 1.0f/ev_length;
|
|
|
|
float s = -ev.z()*inv;
|
|
|
|
float c = -ev.y()*inv;
|
|
|
|
matrix(1,1) = c;
|
|
|
|
matrix(2,1) = -s;
|
|
|
|
matrix(1,2) = s;
|
|
|
|
matrix(2,2) = c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(AXIAL_ROT): // need to implement
|
2001-09-20 05:08:56 +08:00
|
|
|
case(POINT_ROT_WORLD):
|
2001-01-11 00:32:10 +08:00
|
|
|
case(POINT_ROT_EYE):
|
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
// currently treat both POINT_ROT_WORLD and POINT_ROT_EYE the same,
|
|
|
|
// this is only a temporary and 'incorrect' implementation, will
|
|
|
|
// implement fully on second pass of Billboard.
|
|
|
|
// Will also need up vector to calc POINT_ROT_EYE, so this will
|
|
|
|
// have to be added to the above method paramters.
|
|
|
|
// Robert Osfield, Jan 2001.
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
float ev_len = ev.length();
|
|
|
|
if (ev_len != 0.0f)
|
|
|
|
{
|
|
|
|
ev /= ev_len;
|
|
|
|
|
2002-08-10 00:27:39 +08:00
|
|
|
Vec3 cp(ev^_normal);
|
|
|
|
float dot = ev*_normal;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
float cp_len = cp.length();
|
|
|
|
if (cp_len != 0.0f)
|
|
|
|
{
|
|
|
|
cp /= cp_len;
|
|
|
|
|
|
|
|
float rotation_cp = acosf(dot);
|
2002-02-07 09:07:11 +08:00
|
|
|
matrix.makeRotate(-inRadians(rotation_cp),cp[0],cp[1],cp[2]);
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-07 09:07:11 +08:00
|
|
|
matrix.setTrans(pos_local);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-04-17 17:48:19 +08:00
|
|
|
modelview.preMult(matrix);
|
|
|
|
|
2002-02-08 17:30:02 +08:00
|
|
|
return true;
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
bool Billboard::computeBound() const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
int i;
|
2001-09-20 05:08:56 +08:00
|
|
|
int ngsets = _drawables.size();
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
if( ngsets == 0 ) return false;
|
|
|
|
|
|
|
|
_bsphere._center.set(0.0f,0.0f,0.0f);
|
|
|
|
|
|
|
|
for( i = 0; i < ngsets; i++ )
|
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
const Drawable *gset = _drawables[i].get();
|
2001-01-11 00:32:10 +08:00
|
|
|
const BoundingBox& bbox = gset->getBound();
|
|
|
|
|
|
|
|
_bsphere._center += bbox.center();
|
|
|
|
_bsphere._center += _positionList[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
_bsphere._center /= (float)(ngsets);
|
|
|
|
|
|
|
|
float maxd = 0.0;
|
|
|
|
for( i = 0; i < ngsets; ++i )
|
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
const Drawable *gset = _drawables[i].get();
|
2001-01-11 00:32:10 +08:00
|
|
|
const BoundingBox& bbox = gset->getBound();
|
|
|
|
Vec3 local_center = _bsphere._center-_positionList[i];
|
|
|
|
for(unsigned int c=0;c<8;++c)
|
|
|
|
{
|
|
|
|
float d = (bbox.corner(c)-local_center).length2();
|
|
|
|
if( d > maxd ) maxd = d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_bsphere._radius = sqrtf(maxd);
|
|
|
|
|
|
|
|
_bsphere_computed=true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|