2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* 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
|
2003-01-22 00:45:36 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2003-01-22 00:45:36 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-01-22 00:45:36 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
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);
|
2006-10-15 05:47:13 +08:00
|
|
|
//_normal.set(0.0f,-1.0f,0.0f);
|
|
|
|
setNormal(Vec3(0.0f,-1.0f,0.0f));
|
2002-08-10 00:27:39 +08:00
|
|
|
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),
|
2012-02-07 19:21:11 +08:00
|
|
|
_side(billboard._side)
|
|
|
|
{
|
|
|
|
setNormal(_normal);
|
|
|
|
}
|
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();
|
2006-10-15 05:47:13 +08:00
|
|
|
|
|
|
|
// Build rotation from normal to z-axis,
|
|
|
|
// for use with POINT_ROT_EYE
|
|
|
|
Vec3 z(0.0, 0.0, 1.0);
|
|
|
|
Vec3 cp(z^_normal);
|
|
|
|
float dot = z*_normal;
|
|
|
|
float cp_len = cp.length();
|
|
|
|
if (cp_len != 0.0f)
|
|
|
|
{
|
|
|
|
cp /= cp_len;
|
|
|
|
float rotation_cp = acosf(dot);
|
|
|
|
Matrix rotateNormalToZ;
|
|
|
|
_rotateNormalToZAxis.makeRotate(-rotation_cp, cp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_rotateNormalToZAxis.makeIdentity();
|
2002-08-10 00:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2004-10-28 21:26:06 +08:00
|
|
|
else if( _mode == POINT_ROT_WORLD )
|
|
|
|
{
|
|
|
|
if(_axis==Vec3(0.0f, 0.0, 1.0f) && _normal==Vec3(0.0f, -1.0f, 0.0f)) _cachedMode = POINT_ROT_WORLD_Z_AXIS;
|
|
|
|
else _cachedMode = _mode;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-10-28 21:26:06 +08:00
|
|
|
}
|
2002-01-19 06:15:59 +08:00
|
|
|
else _cachedMode = _mode;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2002-08-10 00:27:39 +08:00
|
|
|
_side = _axis^_normal;
|
2012-03-22 01:36:20 +08:00
|
|
|
_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);
|
2005-05-12 22:03:22 +08:00
|
|
|
dirtyBound();
|
2001-01-11 00:32:10 +08:00
|
|
|
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
|
|
|
{
|
2003-08-17 04:42:54 +08:00
|
|
|
case(AXIAL_ROT_Z_AXIS):
|
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)
|
|
|
|
{
|
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;
|
|
|
|
}
|
2003-08-17 04:42:54 +08:00
|
|
|
case(AXIAL_ROT_Y_AXIS):
|
2002-08-10 00:27:39 +08:00
|
|
|
{
|
|
|
|
ev.y() = 0.0f;
|
|
|
|
float ev_length = ev.length();
|
|
|
|
if (ev_length>0.0f)
|
|
|
|
{
|
|
|
|
//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;
|
|
|
|
}
|
2003-08-17 04:42:54 +08:00
|
|
|
case(AXIAL_ROT_X_AXIS):
|
2002-08-10 00:27:39 +08:00
|
|
|
{
|
|
|
|
ev.x() = 0.0f;
|
|
|
|
float ev_length = ev.length();
|
|
|
|
if (ev_length>0.0f)
|
|
|
|
{
|
|
|
|
|
|
|
|
//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;
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
case(AXIAL_ROT): // need to implement
|
2004-01-24 05:47:39 +08:00
|
|
|
{
|
|
|
|
float ev_side = ev*_side;
|
|
|
|
float ev_normal = ev*_normal;
|
|
|
|
float rotation = atan2f(ev_side,ev_normal);
|
|
|
|
matrix.makeRotate(rotation,_axis);
|
|
|
|
break;
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
case(POINT_ROT_WORLD):
|
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;
|
|
|
|
}
|
2006-10-15 05:47:13 +08:00
|
|
|
case(POINT_ROT_EYE):
|
|
|
|
{
|
|
|
|
float ev_len = ev.length();
|
|
|
|
if (ev_len != 0.0f)
|
|
|
|
{
|
|
|
|
ev /= ev_len;
|
|
|
|
|
|
|
|
Vec3 up(modelview(0,1), modelview(1,1), modelview(2,1));
|
|
|
|
Vec3 right(up^ev);
|
|
|
|
right.normalize();
|
|
|
|
up = ev^right;
|
|
|
|
up.normalize();
|
|
|
|
|
|
|
|
matrix(0,0) = right.x();
|
|
|
|
matrix(0,1) = right.y();
|
|
|
|
matrix(0,2) = right.z();
|
|
|
|
matrix(1,0) = up.x();
|
|
|
|
matrix(1,1) = up.y();
|
|
|
|
matrix(1,2) = up.z();
|
|
|
|
matrix(2,0) = ev.x();
|
|
|
|
matrix(2,1) = ev.y();
|
|
|
|
matrix(2,2) = ev.z();
|
|
|
|
|
|
|
|
matrix.preMult(_rotateNormalToZAxis);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2004-10-28 21:26:06 +08:00
|
|
|
case(POINT_ROT_WORLD_Z_AXIS):
|
|
|
|
{
|
|
|
|
// float rotation_about_z = atan2( -ev.y(), ev.x() );
|
|
|
|
// float xy_distance = sqrt( ev.x()*ev.x() + ev.y()*ev.y() );
|
|
|
|
// float rotation_from_xy = atan2( xy_distance, -ev.z() );
|
|
|
|
|
|
|
|
Vec2 about_z( -ev.y(), ev.x() );
|
|
|
|
if( about_z.normalize() == 0.0f ) about_z.x() = 1.0f;
|
|
|
|
float xy_distance = sqrt( ev.x()*ev.x() + ev.y()*ev.y() );
|
|
|
|
Vec2 from_xy( xy_distance, -ev.z() );
|
|
|
|
if( from_xy.normalize() == 0.0f ) from_xy.x() = 1.0f;
|
|
|
|
|
|
|
|
matrix(0,0) = about_z.x();
|
|
|
|
matrix(0,1) = about_z.y();
|
|
|
|
matrix(1,0) = -about_z.y()*from_xy.x();
|
|
|
|
matrix(1,1) = about_z.x()*from_xy.x();
|
|
|
|
matrix(1,2) = from_xy.y();
|
|
|
|
matrix(2,0) = about_z.y()*from_xy.y();
|
|
|
|
matrix(2,1) = -about_z.x()*from_xy.y();
|
|
|
|
matrix(2,2) = from_xy.x();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
BoundingSphere 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
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
if( ngsets == 0 ) return BoundingSphere();
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
BoundingSphere bsphere;
|
|
|
|
bsphere._center.set(0.0f,0.0f,0.0f);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
bsphere._center += bbox.center();
|
|
|
|
bsphere._center += _positionList[i];
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
bsphere._center /= (float)(ngsets);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
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();
|
2005-05-12 22:03:22 +08:00
|
|
|
Vec3 local_center = bsphere._center-_positionList[i];
|
2001-01-11 00:32:10 +08:00
|
|
|
for(unsigned int c=0;c<8;++c)
|
|
|
|
{
|
|
|
|
float d = (bbox.corner(c)-local_center).length2();
|
|
|
|
if( d > maxd ) maxd = d;
|
|
|
|
}
|
|
|
|
}
|
2005-05-12 22:03:22 +08:00
|
|
|
bsphere._radius = sqrtf(maxd);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
return bsphere;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|