root/OpenSceneGraph/trunk/src/osgPlugins/Inventor/PendulumCallback.cpp @ 6051

Revision 6051, 1.6 kB (checked in by robert, 8 years ago)

Added osg::FrameStamp::set/getSimulationTime().

Added setting of osg_SimulationTime and osg_DeltaSimulationTime to the uniforms set by SceneView?

Added frame(double simulationTime) and advance(double simulationTime) parameters to
osgViewer::SimpleViewer?, Vewer and CompositeViewer?.

Updated various examples and Nodes to use SimulationTime? where appropriate.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[2225]1#include <osg/MatrixTransform>
2
3#include "PendulumCallback.h"
4
5PendulumCallback::PendulumCallback(const osg::Vec3& axis,
6                                         float startAngle, float endAngle,
7                                         float frequency)
8{
9    _axis = axis;
10    _startAngle = startAngle;
11    _endAngle = endAngle;
12    _frequency = frequency;
13   
14    _previousTraversalNumber = -1;
15    _previousTime = -1.0;
16    _angle = 0.0;
17}
18
19void PendulumCallback::operator() (osg::Node* node, osg::NodeVisitor* nv)
20{
21    if (!nv)
22        return;
23
24    osg::MatrixTransform* transform = dynamic_cast<osg::MatrixTransform*>(node);
25    if (!transform)
26        return;
27
28    const osg::FrameStamp* fs = nv->getFrameStamp();
29    if (!fs)
30        return;
31   
32    // ensure that we do not operate on this node more than
33    // once during this traversal.  This is an issue since node
34    // can be shared between multiple parents.
35    if (nv->getTraversalNumber()!=_previousTraversalNumber)
36    {
[6051]37        double currentTime = fs->getSimulationTime();
[2915]38        _angle += (currentTime - _previousTime) * 2 * osg::PI * _frequency;
[2225]39       
40        double frac = 0.5 + 0.5 * sin(_angle);
[2915]41        double rotAngle = _endAngle  - _startAngle - osg::PI
[2225]42                + (1.0 - frac) * _startAngle + frac * _endAngle;
43
44        // update the specified transform
45        transform->setMatrix(osg::Matrix::rotate(rotAngle, _axis));
46           
47        _previousTraversalNumber = nv->getTraversalNumber();
48        _previousTime = currentTime;
49    }
50
51    // must call any nested node callbacks and continue subgraph traversal.
52    traverse(node,nv);
53
54}
Note: See TracBrowser for help on using the browser.