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.
|
|
|
|
*/
|
2002-11-19 18:57:40 +08:00
|
|
|
|
|
|
|
#ifndef OSGSIM_BLINKSQUENCE
|
|
|
|
#define OSGSIM_BLINKSQUENCE 1
|
|
|
|
|
|
|
|
#include <osgSim/Export>
|
|
|
|
|
|
|
|
#include <osg/Quat>
|
|
|
|
#include <osg/Vec3>
|
|
|
|
#include <osg/Vec4>
|
2003-09-06 04:48:42 +08:00
|
|
|
#include <osg/Object>
|
2002-11-19 18:57:40 +08:00
|
|
|
#include <osg/ref_ptr>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace osgSim {
|
|
|
|
|
2003-07-22 20:48:31 +08:00
|
|
|
class OSGSIM_EXPORT BlinkSequence : public osg::Object
|
2002-11-19 18:57:40 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/** sequence group which can be used to synchronize related blink sequences.*/
|
2003-07-23 22:06:32 +08:00
|
|
|
class OSGSIM_EXPORT SequenceGroup : public osg::Object
|
2002-11-19 18:57:40 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
SequenceGroup();
|
2003-07-23 22:06:32 +08:00
|
|
|
SequenceGroup(const SequenceGroup& bs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
|
2002-11-19 18:57:40 +08:00
|
|
|
SequenceGroup(double baseTime);
|
|
|
|
|
2003-07-23 22:06:32 +08:00
|
|
|
META_Object(osgSim,SequenceGroup);
|
|
|
|
|
2002-11-19 18:57:40 +08:00
|
|
|
double _baseTime;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
BlinkSequence();
|
|
|
|
|
2003-07-22 20:48:31 +08:00
|
|
|
BlinkSequence(const BlinkSequence& bs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
|
2003-07-23 22:06:32 +08:00
|
|
|
|
2003-07-22 20:48:31 +08:00
|
|
|
META_Object(osgSim,BlinkSequence);
|
2002-11-19 18:57:40 +08:00
|
|
|
|
|
|
|
/** add a pulse of specified color and duration to the BlinkSequence.*/
|
|
|
|
inline void addPulse(double length,const osg::Vec4& color);
|
|
|
|
|
2003-07-23 22:06:32 +08:00
|
|
|
/** return the number of pulses. */
|
|
|
|
inline int getNumPulses() const { return _pulseData.size(); }
|
|
|
|
|
|
|
|
/** return the pulse data at position i. */
|
|
|
|
inline void getPulse(unsigned int i, double& length,osg::Vec4& color) const;
|
|
|
|
|
2002-11-19 18:57:40 +08:00
|
|
|
/** get the total pulse period of the blink sequence, which is equal to the sum of all the pulse periods.*/
|
|
|
|
inline double getPulsePeriod() const { return _pulsePeriod; }
|
|
|
|
|
|
|
|
/** set the sequence group which can be used to synchronize related blink sequences.*/
|
|
|
|
inline void setSequenceGroup(SequenceGroup* sg) { _sequenceGroup = sg; }
|
2003-07-23 22:06:32 +08:00
|
|
|
|
2002-11-19 18:57:40 +08:00
|
|
|
/** get the non const sequence group.*/
|
|
|
|
inline SequenceGroup* getSequenceGroup() { return _sequenceGroup.get(); }
|
2003-07-23 22:06:32 +08:00
|
|
|
|
2002-11-19 18:57:40 +08:00
|
|
|
/** get the const sequence group.*/
|
|
|
|
inline const SequenceGroup* getSequenceGroup() const { return _sequenceGroup.get(); }
|
|
|
|
|
|
|
|
/** set the phase shift of the blink sequence, this would be used to shift a sequence within a sequence group.*/
|
|
|
|
inline void setPhaseShift(double ps) { _phaseShift = ps; }
|
|
|
|
|
|
|
|
/** get the pahse shift.*/
|
|
|
|
inline double getPhaseShift() const { return _phaseShift; }
|
|
|
|
|
|
|
|
/** compute the local time clamped to this BlinkSequences period, and accounting for the phase shift and sequence group.*/
|
|
|
|
inline double localTime(double time) const;
|
|
|
|
|
|
|
|
/** compute the color for the time interval sepecifed. Averages the colors if the length is greater than the current pulse.*/
|
|
|
|
inline osg::Vec4 color(double time,double length) const;
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
|
|
typedef std::pair<double,osg::Vec4> IntervalColor;
|
|
|
|
typedef std::vector<IntervalColor> PulseData;
|
|
|
|
|
|
|
|
double _pulsePeriod;
|
|
|
|
double _phaseShift;
|
|
|
|
PulseData _pulseData;
|
|
|
|
osg::ref_ptr<SequenceGroup> _sequenceGroup;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
inline double BlinkSequence::localTime(double time) const
|
|
|
|
{
|
|
|
|
if (_sequenceGroup.valid()) time -= _sequenceGroup->_baseTime;
|
|
|
|
time -= _phaseShift;
|
|
|
|
return time - floor(time/_pulsePeriod)*_pulsePeriod;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void BlinkSequence::addPulse(double length,const osg::Vec4& color)
|
|
|
|
{
|
|
|
|
_pulseData.push_back(IntervalColor(length,color));
|
|
|
|
_pulsePeriod += length;
|
|
|
|
}
|
|
|
|
|
2003-07-23 22:06:32 +08:00
|
|
|
inline void BlinkSequence::getPulse(unsigned int i, double& length, osg::Vec4& color) const
|
|
|
|
{
|
|
|
|
const IntervalColor& ic = _pulseData[i];
|
|
|
|
length = ic.first;
|
|
|
|
color = ic.second;
|
|
|
|
}
|
|
|
|
|
2002-11-19 18:57:40 +08:00
|
|
|
inline osg::Vec4 BlinkSequence::color(double time,double length) const
|
|
|
|
{
|
|
|
|
if (_pulseData.empty()) return osg::Vec4(1.0f,1.0f,1.0f,1.0f);
|
|
|
|
double lt = localTime(time);
|
|
|
|
PulseData::const_iterator itr = _pulseData.begin();
|
|
|
|
|
|
|
|
// find the first sample at this time point.
|
|
|
|
while (lt>itr->first)
|
|
|
|
{
|
|
|
|
lt -= itr->first;
|
|
|
|
++itr;
|
|
|
|
if (itr==_pulseData.end()) itr = _pulseData.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
// if time interval fits inside the current pulse
|
|
|
|
// then simply return this pulses color value.
|
|
|
|
if (lt+length<=itr->first)
|
|
|
|
{
|
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// time length exceeds the current pulse therefore
|
|
|
|
// we have to average out the pules to get the correct
|
|
|
|
// results...
|
|
|
|
|
|
|
|
// accumulate final part of the first active pulses.
|
|
|
|
osg::Vec4 color(itr->second*(itr->first-lt));
|
|
|
|
double len = length-(itr->first-lt);
|
|
|
|
++itr;
|
|
|
|
if (itr==_pulseData.end()) itr = _pulseData.begin();
|
|
|
|
|
|
|
|
// accumulate all the whole pluses pulses.
|
|
|
|
while (len>itr->first)
|
|
|
|
{
|
|
|
|
len -= itr->first;
|
|
|
|
color += itr->second*itr->first;
|
|
|
|
++itr;
|
|
|
|
if (itr==_pulseData.end()) itr = _pulseData.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
// add remaining part of the final pulse.
|
|
|
|
color += itr->second*len;
|
|
|
|
|
|
|
|
// normalise the time waited color.
|
|
|
|
color /= length;
|
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|