00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "Globals.h"
00018
00019
00020 #include "AnimationPathTimedCallback.h"
00021 #include <osg/NodeVisitor>
00022 #include "Dispatcher.h"
00023 #include <stdio.h>
00024 #include <iostream>
00025
00026 #include <osg/AnimationPath>
00027 #include <osg/MatrixTransform>
00028 #include <osg/PositionAttitudeTransform>
00029 #include <osg/CameraNode>
00030 #include <osg/CameraView>
00031 #include <osg/io_utils>
00032
00033 using namespace std;
00034 using namespace osg;
00035
00036 class AnimationPathCallbackVisitor : public NodeVisitor
00037 {
00038 public:
00039
00040 AnimationPathCallbackVisitor(const AnimationPath::ControlPoint& cp, const osg::Vec3d& pivotPoint, bool useInverseMatrix):
00041 _cp(cp),
00042 _pivotPoint(pivotPoint),
00043 _useInverseMatrix(useInverseMatrix) {}
00044
00045 virtual void apply(CameraNode& camera)
00046 {
00047 Matrix matrix;
00048 if (_useInverseMatrix)
00049 _cp.getInverse(matrix);
00050 else
00051 _cp.getMatrix(matrix);
00052
00053 camera.setViewMatrix(osg::Matrix::translate(-_pivotPoint)*matrix);
00054 }
00055
00056
00057 virtual void apply(CameraView& cv)
00058 {
00059 if (_useInverseMatrix)
00060 {
00061 Matrix matrix;
00062 _cp.getInverse(matrix);
00063 cv.setPosition(matrix.getTrans());
00064 cv.setAttitude(_cp.getRotation().inverse());
00065 cv.setFocalLength(1.0f/_cp.getScale().x());
00066
00067 }
00068 else
00069 {
00070 cv.setPosition(_cp.getPosition());
00071 cv.setAttitude(_cp.getRotation());
00072 cv.setFocalLength(_cp.getScale().x());
00073 }
00074 }
00075
00076 virtual void apply(MatrixTransform& mt)
00077 {
00078 Matrix matrix;
00079 if (_useInverseMatrix)
00080 _cp.getInverse(matrix);
00081 else
00082 _cp.getMatrix(matrix);
00083
00084 mt.setMatrix(osg::Matrix::translate(-_pivotPoint)*matrix);
00085 }
00086
00087 virtual void apply(PositionAttitudeTransform& pat)
00088 {
00089 if (_useInverseMatrix)
00090 {
00091 Matrix matrix;
00092 _cp.getInverse(matrix);
00093 pat.setPosition(matrix.getTrans());
00094 pat.setAttitude(_cp.getRotation().inverse());
00095 pat.setScale(osg::Vec3(1.0f/_cp.getScale().x(),1.0f/_cp.getScale().y(),1.0f/_cp.getScale().z()));
00096 pat.setPivotPoint(_pivotPoint);
00097
00098 }
00099 else
00100 {
00101 pat.setPosition(_cp.getPosition());
00102 pat.setAttitude(_cp.getRotation());
00103 pat.setScale(_cp.getScale());
00104 pat.setPivotPoint(_pivotPoint);
00105 }
00106 }
00107
00108 AnimationPath::ControlPoint _cp;
00109 osg::Vec3d _pivotPoint;
00110 bool _useInverseMatrix;
00111 };
00112
00113 AnimationPathTimedCallback::AnimationPathTimedCallback()
00114 : osg::AnimationPathCallback()
00115 {
00116 elapsed = 0;
00117 }
00118
00119 double AnimationPathTimedCallback::getAnimationTime () const
00120 {
00121 return elapsed;
00122 }
00123
00124 void AnimationPathTimedCallback::update(osg::Node& node)
00125 {
00126 osg::AnimationPath::ControlPoint cp;
00127 if (_animationPath->getInterpolatedControlPoint(getAnimationTime(),cp))
00128 {
00129 AnimationPathCallbackVisitor apcv(cp,_pivotPoint,_useInverseMatrix);
00130 node.accept(apcv);
00131 }
00132 }
00133
00134 void AnimationPathTimedCallback::operator()(osg::Node* node, osg::NodeVisitor* nv){
00135
00136
00137
00138 double timeScale = timeKeeper->getTimeFlow();
00139
00140 if ( AnimationPathTimedCallback::_timeMultiplier != timeScale ) {
00141 AnimationPathTimedCallback::_timeMultiplier = timeScale;
00142 }
00143
00144
00145 if (_animationPath.valid() && nv->getVisitorType()==osg::NodeVisitor::UPDATE_VISITOR && nv->getFrameStamp()){
00146 double time = nv->getFrameStamp()->getReferenceTime();
00147 elapsed += (time - _latestTime) * _timeMultiplier;
00148 _latestTime = time;
00149 if (!_pause){
00150
00151 if (_firstTime==DBL_MAX) {
00152 _firstTime = time;
00153 elapsed = 0;
00154 }
00155 update(*node);
00156 }
00157 }
00158
00159 NodeCallback::traverse(node,nv);
00160 }
00161
00162 void AnimationPathTimedCallback::setTimeReference(Dispatcher* dp){
00163 timeKeeper = dp;
00164 }