root/OpenSceneGraph/trunk/src/osgManipulator/RotateSphereDragger.cpp @ 13041

Revision 13041, 5.4 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13//osgManipulator - Copyright (C) 2007 Fugro-Jason B.V.
14
15#include <osgManipulator/RotateSphereDragger>
16#include <osgManipulator/Command>
17
18#include <osg/ShapeDrawable>
19#include <osg/Geometry>
20#include <osg/Material>
21
22#include <iostream>
23
24using namespace osgManipulator;
25
26RotateSphereDragger::RotateSphereDragger() : _prevPtOnSphere(true)
27{
28    _projector = new SpherePlaneProjector();
29    setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
30    setPickColor(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
31}
32
33RotateSphereDragger::~RotateSphereDragger()
34{
35}
36
37bool RotateSphereDragger::handle(const PointerInfo& pointer, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
38{
39    // Check if the dragger node is in the nodepath.
40    if (!pointer.contains(this)) return false;
41
42    switch (ea.getEventType())
43    {
44        // Pick start.
45        case (osgGA::GUIEventAdapter::PUSH):
46            {
47                // Get the LocalToWorld matrix for this node and set it for the projector.
48                osg::NodePath nodePathToRoot;
49                computeNodePathToRoot(*this,nodePathToRoot);
50                osg::Matrix localToWorld = osg::computeLocalToWorld(nodePathToRoot);
51                _projector->setLocalToWorld(localToWorld);
52
53                _startLocalToWorld = _projector->getLocalToWorld();
54                _startWorldToLocal = _projector->getWorldToLocal();
55
56                if (_projector->isPointInFront(pointer, _startLocalToWorld))
57                    _projector->setFront(true);
58                else
59                    _projector->setFront(false);
60
61                osg::Vec3d projectedPoint;
62                if (_projector->project(pointer, projectedPoint))
63                {
64                    // Generate the motion command.
65                    osg::ref_ptr<Rotate3DCommand> cmd = new Rotate3DCommand();
66                    cmd->setStage(MotionCommand::START);
67                    cmd->setLocalToWorldAndWorldToLocal(_startLocalToWorld,_startWorldToLocal);
68
69                    // Dispatch command.
70                    dispatch(*cmd);
71
72                    // Set color to pick color.
73                    setMaterialColor(_pickColor,*this);
74
75                    _prevRotation = osg::Quat();
76                    _prevWorldProjPt = projectedPoint * _projector->getLocalToWorld();
77                    _prevPtOnSphere = _projector->isProjectionOnSphere();
78
79                    aa.requestRedraw();
80                }
81
82                return true;
83            }
84
85        // Pick move.
86        case (osgGA::GUIEventAdapter::DRAG):
87            {
88                // Get the LocalToWorld matrix for this node and set it for the projector.
89                osg::Matrix localToWorld = osg::Matrix(_prevRotation) * _startLocalToWorld;
90                _projector->setLocalToWorld(localToWorld);
91
92                osg::Vec3d projectedPoint;
93                if (_projector->project(pointer, projectedPoint))
94                {
95                    osg::Vec3d prevProjectedPoint = _prevWorldProjPt * _projector->getWorldToLocal();
96                    osg::Quat  deltaRotation = _projector->getRotation(prevProjectedPoint, _prevPtOnSphere,
97                                                                      projectedPoint, _projector->isProjectionOnSphere(),1.0f);
98                    osg::Quat rotation = deltaRotation * _prevRotation;
99
100                    // Generate the motion command.
101                    osg::ref_ptr<Rotate3DCommand> cmd = new Rotate3DCommand();
102                    cmd->setStage(MotionCommand::MOVE);
103                    cmd->setLocalToWorldAndWorldToLocal(_startLocalToWorld,_startWorldToLocal);
104                    cmd->setRotation(rotation);
105
106                    // Dispatch command.
107                    dispatch(*cmd);
108
109                    _prevWorldProjPt = projectedPoint * _projector->getLocalToWorld();
110                    _prevRotation = rotation;
111                    _prevPtOnSphere = _projector->isProjectionOnSphere();
112                    aa.requestRedraw();
113                }
114                return true;
115            }
116
117        // Pick finish.
118        case (osgGA::GUIEventAdapter::RELEASE):
119            {
120                osg::ref_ptr<Rotate3DCommand> cmd = new Rotate3DCommand();
121
122                cmd->setStage(MotionCommand::FINISH);
123                cmd->setLocalToWorldAndWorldToLocal(_startLocalToWorld,_startWorldToLocal);
124
125                // Dispatch command.
126                dispatch(*cmd);
127
128                // Reset color.
129                setMaterialColor(_color,*this);
130
131                aa.requestRedraw();
132
133                return true;
134            }
135        default:
136            return false;
137    }
138}
139
140void RotateSphereDragger::setupDefaultGeometry()
141{
142    osg::Geode* geode = new osg::Geode;
143    geode->addDrawable(new osg::ShapeDrawable(const_cast<osg::Sphere*>(_projector->getSphere())));
144    addChild(geode);
145}
Note: See TracBrowser for help on using the browser.