Added osgSim library which encapulsulates light points.

Added osglightpoint demo.
This commit is contained in:
Robert Osfield
2002-11-19 10:57:40 +00:00
parent 5fca8ea229
commit e02ae73edc
19 changed files with 1618 additions and 4 deletions

View File

@@ -0,0 +1,151 @@
//C++ header - Open Scene Graph Simulation - Copyright (C) 1998-2002 Robert Osfield
// Distributed under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation.
//
// All software using osgSim must be GPL'd or excempted via the
// purchase of the Open Scene Graph Professional License (OSGPL)
// for further information contact robert@openscenegraph.com.
#ifndef OSGSIM_BLINKSQUENCE
#define OSGSIM_BLINKSQUENCE 1
#include <osgSim/Export>
#include <osg/Quat>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/ref_ptr>
#include <vector>
namespace osgSim {
class OSGSIM_EXPORT BlinkSequence : public osg::Referenced
{
public:
/** sequence group which can be used to synchronize related blink sequences.*/
class OSGSIM_EXPORT SequenceGroup : public osg::Referenced
{
public:
SequenceGroup();
SequenceGroup(double baseTime);
double _baseTime;
};
BlinkSequence();
BlinkSequence(const BlinkSequence& bs);
/** add a pulse of specified color and duration to the BlinkSequence.*/
inline void addPulse(double length,const osg::Vec4& color);
/** 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; }
/** get the non const sequence group.*/
inline SequenceGroup* getSequenceGroup() { return _sequenceGroup.get(); }
/** 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;
}
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

40
include/osgSim/Export Normal file
View File

@@ -0,0 +1,40 @@
//C++ header - Open Scene Graph Simulation - Copyright (C) 1998-2002 Robert Osfield
// Distributed under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation.
//
// All software using osgSim must be GPL'd or excempted via the
// purchase of the Open Scene Graph Professional License (OSGPL)
// for further information contact robert@openscenegraph.com.
#ifndef OSGSIM_EXPORT_
#define OSGSIM_EXPORT_ 1
#if defined(WIN32) && !(defined(__CYGWIN__) || defined(__MINGW32__))
#pragma warning( disable : 4244 )
#pragma warning( disable : 4251 )
#pragma warning( disable : 4275 )
#pragma warning( disable : 4786 )
#pragma warning( disable : 4290 )
#endif
#if defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW32__) || defined( __BCPLUSPLUS__) || defined( __MWERKS__)
# ifdef OSGSIM_LIBRARY
# define OSGSIM_EXPORT __declspec(dllexport)
# else
# define OSGSIM_EXPORT __declspec(dllimport)
# endif /* SG_LIBRARY */
#else
# define OSGSIM_EXPORT
#endif
/* Define NULL pointer value */
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
#endif

44
include/osgSim/LightPoint Normal file
View File

@@ -0,0 +1,44 @@
//C++ header - Open Scene Graph Simulation - Copyright (C) 1998-2002 Robert Osfield
// Distributed under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation.
//
// All software using osgSim must be GPL'd or excempted via the
// purchase of the Open Scene Graph Professional License (OSGPL)
// for further information contact robert@openscenegraph.com.
#ifndef OSGSIM_LGIHTPOINT
#define OSGSIM_LIGHTPOINT 1
#include <osgSim/Export>
#include <osgSim/Sector>
#include <osgSim/BlinkSequence>
#include <osg/Quat>
#include <osg/Vec3>
#include <osg/Vec4>
namespace osgSim {
class OSGSIM_EXPORT LightPoint
{
public:
LightPoint();
LightPoint(const LightPoint& lp);
bool _on;
osg::Vec3 _position;
osg::Vec4 _color;
float _intensity;
float _radius;
float _maxPixelSize;
osg::ref_ptr<Sector> _sector;
osg::ref_ptr<BlinkSequence> _blinkSequence;
};
}
#endif

View File

@@ -0,0 +1,113 @@
//C++ header - Open Scene Graph Simulation - Copyright (C) 1998-2002 Robert Osfield
// Distributed under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation.
//
// All software using osgSim must be GPL'd or excempted via the
// purchase of the Open Scene Graph Professional License (OSGPL)
// for further information contact robert@openscenegraph.com.
#ifndef OSGSIM_LIGHTPOINTDRAWABLE
#define OSGSIM_LIGHTPOINTDRAWABLE 1
#include <osgSim/Export>
#include <osg/Drawable>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Depth>
#include <osg/BlendFunc>
#include <osg/ColorMask>
#include <osg/Point>
#include <vector>
namespace osgSim {
class OSGSIM_EXPORT LightPointDrawable : public osg::Drawable
{
public :
LightPointDrawable();
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
LightPointDrawable(const LightPointDrawable&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
virtual osg::Object* cloneType() const { return osgNew LightPointDrawable(); }
virtual osg::Object* clone(const osg::CopyOp&) const { return osgNew LightPointDrawable(); }
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const LightPointDrawable*>(obj)!=NULL; }
virtual const char* className() const { return "LightPointDrawable"; }
//typedef std::pair<unsigned long,osg::Vec3> ColorPosition;
struct ColorPosition
{
unsigned long first;
osg::Vec3 second;
ColorPosition() {}
ColorPosition(unsigned long f,const osg::Vec3& s):first(f),second(s) {}
};
void reset()
{
for(SizedLightPointList::iterator itr=_sizedLightPointList.begin();
itr!=_sizedLightPointList.end();
++itr)
{
if (!itr->empty())
itr->erase(itr->begin(),itr->end());
}
}
inline void addLightPoint(unsigned int pointSize,const osg::Vec3& position,const osg::Vec4& color)
{
if (pointSize>=_sizedLightPointList.size()) _sizedLightPointList.resize(pointSize+1);
_sizedLightPointList[pointSize].push_back(ColorPosition(color.asRGBA(),position));
}
/** draw LightPoints. */
virtual void drawImplementation(osg::State& state) const;
void setReferenceTime(double time)
{
_referenceTime = time;
_referenceTimeInterval = 0.0;
}
void updateReferenceTime(double time)
{
_referenceTimeInterval = osg::clampAbove(time-_referenceTime,0.0);
_referenceTime = time;
}
double getReferenceTime() const { return _referenceTime; }
double getReferenceTimeInterval() const { return _referenceTimeInterval; }
protected:
virtual bool computeBound() const;
~LightPointDrawable() {}
double _referenceTime;
double _referenceTimeInterval;
typedef std::vector<ColorPosition> LightPointList;
typedef std::vector<LightPointList> SizedLightPointList;
SizedLightPointList _sizedLightPointList;
osg::ref_ptr<osg::Depth> _depthOff;
osg::ref_ptr<osg::Depth> _depthOn;
osg::ref_ptr<osg::BlendFunc> _blendOn;
osg::ref_ptr<osg::ColorMask> _colorMaskOff;
osg::ref_ptr<osg::Point> _point;
};
}
#endif

View File

@@ -0,0 +1,72 @@
//C++ header - Open Scene Graph Simulation - Copyright (C) 1998-2002 Robert Osfield
// Distributed under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation.
//
// All software using osgSim must be GPL'd or excempted via the
// purchase of the Open Scene Graph Professional License (OSGPL)
// for further information contact robert@openscenegraph.com.
#ifndef OSGSIM_LIGHTPOINTNODE
#define OSGSIM_LIGHTPOINTNODE 1
#include <osgSim/Export>
#include <osgSim/LightPoint>
#include <osgSim/LightPointDrawable>
#include <osg/Node>
#include <osg/NodeVisitor>
#include <osg/BoundingBox>
#include <osg/Quat>
#include <osg/Vec4>
#include <vector>
#include <set>
namespace osgSim {
class OSGSIM_EXPORT LightPointNode : public osg::Node
{
public :
typedef std::vector< LightPoint > LightPointList;
LightPointNode();
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
LightPointNode(const LightPointNode&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Node(osgSim,LightPointNode);
virtual void traverse(osg::NodeVisitor& nv);
unsigned int addLightPoint(const LightPoint& lp);
LightPoint& getLightPoint(unsigned int pos) { return _lightPointList[pos]; }
const LightPoint& getLightPoint(unsigned int pos) const { return _lightPointList[pos]; }
void removeLightPoint(unsigned int pos);
void removeLightPoints(LightPointList::iterator start,LightPointList::iterator end);
LightPointList _lightPointList;
protected:
~LightPointNode() {}
// used to cache the bouding box of the lightpoints as a tighter
// view frustum check.
mutable osg::BoundingBox _bbox;
virtual bool computeBound() const;
};
}
#endif

203
include/osgSim/Sector Normal file
View File

@@ -0,0 +1,203 @@
//C++ header - Open Scene Graph Simulation - Copyright (C) 1998-2002 Robert Osfield
// Distributed under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation.
//
// All software using osgSim must be GPL'd or excempted via the
// purchase of the Open Scene Graph Professional License (OSGPL)
// for further information contact robert@openscenegraph.com.
#ifndef OSGSIM_SECTOR
#define OSGSIM_SECTOR 1
#include <osgSim/Export>
#include <osg/Quat>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Math>
namespace osgSim {
class Sector : public osg::Referenced
{
public:
virtual float operator() (const osg::Vec3& /*eyeLocal*/) const = 0;
protected:
virtual ~Sector() {}
};
class OSGSIM_EXPORT AzimRange
{
public:
AzimRange():
_cosAzim(1.0f),
_sinAzim(0.0f),
_cosAngle(-1.0f),
_cosFadeAngle(-1.0f) {}
void setAzimuthRange(float minAzimuth,float maxAzimuth,float fadeAngle=0.0f);
inline float azimSector(const osg::Vec3& eyeLocal) const
{
float dotproduct = eyeLocal.x()*_sinAzim+eyeLocal.y()*_cosAzim;
float length = sqrt(osg::square(eyeLocal.x())+osg::square(eyeLocal.y()));
if (dotproduct<_cosFadeAngle*length) return 0.0f; // out of sector.
if (dotproduct>_cosAngle*length) return 1.0f; // fully in sector.
return (dotproduct-_cosFadeAngle*length)/((_cosAngle-_cosFadeAngle)*length);
}
protected:
float _cosAzim;
float _sinAzim;
float _cosAngle;
float _cosFadeAngle;
};
class OSGSIM_EXPORT ElevationRange
{
public:
ElevationRange():
_cosMinElevation(-1.0f),
_cosMinFadeElevation(-1.0f),
_cosMaxElevation(1.0),
_cosMaxFadeElevation(1.0) {}
void setElevationRange(float minElevation,float maxElevation,float fadeAngle=0.0f);
float getMinElevation() const;
float getMaxElevation() const;
inline float elevationSector(const osg::Vec3& eyeLocal) const
{
float dotproduct = eyeLocal.z(); // against z axis - eyeLocal*(0,0,1).
float length = eyeLocal.length();
if (dotproduct>_cosMaxFadeElevation*length) return 0.0f; // out of sector
if (dotproduct<_cosMinFadeElevation*length) return 0.0f; // out of sector
if (dotproduct>_cosMaxElevation*length)
{
// in uppoer fade band.
return (dotproduct-_cosMaxFadeElevation*length)/((_cosMaxElevation-_cosMaxFadeElevation)*length);
}
if (dotproduct<_cosMinElevation*length)
{
// in lower fade band.
return (dotproduct-_cosMinFadeElevation*length)/((_cosMinElevation-_cosMinFadeElevation)*length);
}
return 1.0f; // fully in sector
}
protected:
float _cosMinElevation;
float _cosMinFadeElevation;
float _cosMaxElevation;
float _cosMaxFadeElevation;
};
class OSGSIM_EXPORT AzimSector : public Sector, public AzimRange
{
public:
AzimSector():
Sector(),
AzimRange() {}
AzimSector(float minAzimuth,float maxAzimuth,float fadeAngle=0.0f);
virtual float operator() (const osg::Vec3& eyeLocal) const;
protected:
virtual ~AzimSector() {}
};
class OSGSIM_EXPORT ElevationSector : public Sector, public ElevationRange
{
public:
ElevationSector():
Sector(),
ElevationRange() {}
ElevationSector(float minElevation,float maxElevation,float fadeAngle=0.0f);
virtual float operator() (const osg::Vec3& eyeLocal) const;
protected:
virtual ~ElevationSector() {}
float _cosMinElevation;
float _cosMinFadeElevation;
float _cosMaxElevation;
float _cosMaxFadeElevation;
};
class OSGSIM_EXPORT AzimElevationSector : public Sector, public AzimRange, public ElevationRange
{
public:
AzimElevationSector():
Sector(),
AzimRange(),
ElevationRange() {}
AzimElevationSector(float minAzimuth,float maxAzimuth,float minElevation,float maxElevation,float fadeAngle=0.0f);
virtual float operator() (const osg::Vec3& eyeLocal) const;
protected:
virtual ~AzimElevationSector() {}
};
class OSGSIM_EXPORT ConeSector : public Sector
{
public:
ConeSector():
Sector(),
_axis(0.0f,0.0f,1.0f),
_cosAngle(-1.0f),
_cosAngleFade(-1.0f) {}
ConeSector(const osg::Vec3& axis,float angle,float fadeangle=0.0f);
void setAxis(const osg::Vec3& axis);
const osg::Vec3& getAxis() const;
void setAngle(float angle,float fadeangle=0.0f);
float getAngle() const;
float getFadeAngle() const;
virtual float operator() (const osg::Vec3& eyeLocal) const;
protected:
virtual ~ConeSector() {}
osg::Vec3 _axis;
float _cosAngle;
float _cosAngleFade;
};
}
#endif

41
include/osgSim/Version Normal file
View File

@@ -0,0 +1,41 @@
//C++ header - Open Scene Graph Simulation - Copyright (C) 1998-2002 Robert Osfield
// Distributed under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation.
//
// All software using osgSim must be GPL'd or excempted via the
// purchase of the Open Scene Graph Professional License (OSGPL)
// for further information contact robert@openscenegraph.com.
#ifndef OSGSIM_VERSION
#define OSGSIM_VERSION 1
#include <osgSim/Export>
extern "C" {
/**
* osgSimGetVersion() returns the library version number.
* Numbering convention : OpenSceneGraph-Sim-0.1 will return 0.1 from osgSimgetVersion.
*
* This C function can be also used to check for the existence of the OpenSceneGraph
* library using autoconf and its m4 macro AC_CHECK_LIB.
*
* Here is the code to add to your configure.in:
\verbatim
#
# Check for the OpenSceneGraph-Sim library
#
AC_CHECK_LIB(osg, osgSimGetVersion, ,
[AC_MSG_ERROR(OpenSceneGraph library not found. See http://www.openscenegraph.org)],)
\endverbatim
*/
extern SG_EXPORT const char* osgSimGetVersion();
/**
* osgSimGetLibraryName() returns the library name in human friendly form.
*/
extern SG_EXPORT const char* osgSimGetLibraryName();
}
#endif