root/OpenSceneGraph/trunk/examples/osghangglide/GliderManipulator.cpp @ 5863

Revision 5863, 7.2 kB (checked in by robert, 8 years ago)

Various work on osgViewer library, including warp point and graphics window resize support

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include "GliderManipulator.h"
2#include <osg/Notify>
3
4using namespace osg;
5using namespace osgGA;
6
7GliderManipulator::GliderManipulator()
8{
9    _modelScale = 0.01f;
10    _velocity = 0.2f;
11    _yawMode = YAW_AUTOMATICALLY_WHEN_BANKED;
12
13    _distance = 1.0f;
14}
15
16
17GliderManipulator::~GliderManipulator()
18{
19}
20
21
22void GliderManipulator::setNode(osg::Node* node)
23{
24    _node = node;
25    if (_node.get())
26    {
27        const osg::BoundingSphere& boundingSphere=_node->getBound();
28        _modelScale = boundingSphere._radius;
29    }
30}
31
32
33const osg::Node* GliderManipulator::getNode() const
34{
35    return _node.get();
36}
37
38
39
40osg::Node* GliderManipulator::getNode()
41{
42    return _node.get();
43}
44
45void GliderManipulator::home(const GUIEventAdapter& ea,GUIActionAdapter& us)
46{
47    if(_node.get())
48    {
49
50        const osg::BoundingSphere& boundingSphere=_node->getBound();
51
52        osg::Vec3 eye = boundingSphere._center+osg::Vec3(-boundingSphere._radius*0.25f,-boundingSphere._radius*0.25f,-boundingSphere._radius*0.03f);
53
54        computePosition(eye,
55            osg::Vec3(1.0f,1.0f,-0.1f),
56            osg::Vec3(0.0f,0.0f,1.0f));
57
58        _velocity = boundingSphere._radius*0.01f;
59
60        us.requestRedraw();
61
62        us.requestWarpPointer((ea.getXmin()+ea.getXmax())/2.0f,(ea.getYmin()+ea.getYmax())/2.0f);
63
64        flushMouseEventStack();
65
66    }
67
68}
69
70
71void GliderManipulator::init(const GUIEventAdapter& ea,GUIActionAdapter& us)
72{
73    flushMouseEventStack();
74
75    us.requestContinuousUpdate(false);
76
77    _velocity = 0.2f;
78
79    if (ea.getEventType()!=GUIEventAdapter::RESIZE)
80    {
81        us.requestWarpPointer((ea.getXmin()+ea.getXmax())/2.0f,(ea.getYmin()+ea.getYmax())/2.0f);
82    }
83}
84
85
86bool GliderManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& us)
87{
88    switch(ea.getEventType())
89    {
90#if 0   
91        case(GUIEventAdapter::PUSH):
92        {
93
94            addMouseEvent(ea);
95            us.requestContinuousUpdate(true);
96            if (calcMovement()) us.requestRedraw();
97            return true;
98        }
99
100        case(GUIEventAdapter::RELEASE):
101        {
102
103            addMouseEvent(ea);
104            us.requestContinuousUpdate(true);
105            // if (calcMovement()) us.requestRedraw();
106            return true;
107        }
108
109        case(GUIEventAdapter::DRAG):
110        {
111
112            addMouseEvent(ea);
113            us.requestContinuousUpdate(true);
114            // if (calcMovement()) us.requestRedraw();
115            return true;
116        }
117
118        case(GUIEventAdapter::MOVE):
119        {
120
121            addMouseEvent(ea);
122            us.requestContinuousUpdate(true);
123            // if (calcMovement()) us.requestRedraw();
124
125            return true;
126        }
127#endif
128        case(GUIEventAdapter::KEYDOWN):
129            if (ea.getKey()==' ')
130            {
131                flushMouseEventStack();
132                home(ea,us);
133                us.requestRedraw();
134                us.requestContinuousUpdate(false);
135                return true;
136            }
137            else if (ea.getKey()=='q')
138            {
139                _yawMode = YAW_AUTOMATICALLY_WHEN_BANKED;
140                return true;
141            }
142            else if (ea.getKey()=='a')
143            {
144                _yawMode = NO_AUTOMATIC_YAW;
145                return true;
146            }
147            return false;
148
149        case(GUIEventAdapter::FRAME):
150            addMouseEvent(ea);
151            if (calcMovement()) us.requestRedraw();
152            return true;
153
154        case(GUIEventAdapter::RESIZE):
155            init(ea,us);
156            us.requestRedraw();
157            return true;
158
159        default:
160            return false;
161    }
162}
163
164void GliderManipulator::getUsage(osg::ApplicationUsage& usage) const
165{
166    usage.addKeyboardMouseBinding("Flight: Space","Reset the viewing position to home");
167    usage.addKeyboardMouseBinding("Flight: q","Automatically yaw when banked (default)");
168    usage.addKeyboardMouseBinding("Flight: a","No yaw when banked");
169}
170
171void GliderManipulator::flushMouseEventStack()
172{
173    _ga_t1 = NULL;
174    _ga_t0 = NULL;
175}
176
177
178void GliderManipulator::addMouseEvent(const GUIEventAdapter& ea)
179{
180    _ga_t1 = _ga_t0;
181    _ga_t0 = &ea;
182}
183
184
185void GliderManipulator::setByMatrix(const osg::Matrixd& matrix)
186{
187    _eye = matrix.getTrans();
188    _rotation = matrix.getRotate();
189    _distance = 1.0f;
190}
191
192osg::Matrixd GliderManipulator::getMatrix() const
193{
194    return osg::Matrixd::rotate(_rotation)*osg::Matrixd::translate(_eye);
195}
196
197osg::Matrixd GliderManipulator::getInverseMatrix() const
198{
199    return osg::Matrixd::translate(-_eye)*osg::Matrixd::rotate(_rotation.inverse());
200}
201
202void GliderManipulator::computePosition(const osg::Vec3& eye,const osg::Vec3& lv,const osg::Vec3& up)
203{
204    osg::Vec3 f(lv);
205    f.normalize();
206    osg::Vec3 s(f^up);
207    s.normalize();
208    osg::Vec3 u(s^f);
209    u.normalize();
210   
211    osg::Matrixd rotation_matrix(s[0],     u[0],     -f[0],     0.0f,
212                                s[1],     u[1],     -f[1],     0.0f,
213                                s[2],     u[2],     -f[2],     0.0f,
214                                0.0f,     0.0f,     0.0f,      1.0f);
215                   
216    _eye = eye;
217    _distance = lv.length();
218    _rotation = rotation_matrix.getRotate().inverse();
219}
220
221
222bool GliderManipulator::calcMovement()
223{
224    //_camera->setFusionDistanceMode(osg::Camera::PROPORTIONAL_TO_SCREEN_DISTANCE);
225
226    // return if less then two events have been added.
227    if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false;
228
229
230    double dt = _ga_t0->getTime()-_ga_t1->getTime();
231
232    if (dt<0.0f)
233    {
234        notify(INFO) << "warning dt = "<<dt<< std::endl;
235        dt = 0.0f;
236    }
237
238    unsigned int buttonMask = _ga_t1->getButtonMask();
239    if (buttonMask==GUIEventAdapter::LEFT_MOUSE_BUTTON)
240    {
241        // pan model.
242
243        _velocity += dt*_modelScale*0.05f;
244
245    }
246    else if (buttonMask==GUIEventAdapter::MIDDLE_MOUSE_BUTTON ||
247        buttonMask==(GUIEventAdapter::LEFT_MOUSE_BUTTON|GUIEventAdapter::RIGHT_MOUSE_BUTTON))
248    {
249
250        _velocity = 0.0f;
251
252    }
253    else if (buttonMask==GUIEventAdapter::RIGHT_MOUSE_BUTTON)
254    {
255
256        _velocity -= dt*_modelScale*0.05f;
257
258    }
259
260    float dx = _ga_t0->getXnormalized();
261    float dy = _ga_t0->getYnormalized();
262   
263    // osg::notify(osg::NOTICE)<<"dx = "<<dx<<" dy = "<<dy<<"dt = "<<dt<<std::endl;
264
265    // mew - flag to reverse mouse-control mapping
266    if( getenv( "OSGHANGGLIDE_REVERSE_CONTROLS" ) )
267    {
268        dx = -dx;
269        dy = -dy;
270    }
271
272    osg::Matrixd rotation_matrix;
273    rotation_matrix.makeRotate(_rotation);
274   
275    osg::Vec3 up = osg::Vec3(0.0f,1.0f,0.0) * rotation_matrix;
276    osg::Vec3 lv = osg::Vec3(0.0f,0.0f,-1.0f) * rotation_matrix;
277
278    osg::Vec3 sv = lv^up;
279    sv.normalize();
280
281    float pitch = -inDegrees(dy*75.0f*dt);
282    float roll = inDegrees(dx*50.0f*dt);
283
284    osg::Quat delta_rotate;
285
286    osg::Quat roll_rotate;
287    osg::Quat pitch_rotate;
288
289    pitch_rotate.makeRotate(pitch,sv.x(),sv.y(),sv.z());
290    roll_rotate.makeRotate(roll,lv.x(),lv.y(),lv.z());
291
292    delta_rotate = pitch_rotate*roll_rotate;
293
294    if (_yawMode==YAW_AUTOMATICALLY_WHEN_BANKED)
295    {
296        float bank = asinf(sv.z());
297        float yaw = inRadians(bank)*dt;
298       
299        osg::Quat yaw_rotate;
300        yaw_rotate.makeRotate(yaw,0.0f,0.0f,1.0f);
301
302        delta_rotate = delta_rotate*yaw_rotate;
303    }
304
305    lv *= (_velocity*dt);
306
307    _eye += lv;
308    _rotation = _rotation*delta_rotate;
309
310    return true;
311}
Note: See TracBrowser for help on using the browser.