]> git.mxchange.org Git - flightgear.git/blob - src/Main/CameraGroup.cxx
Merge branch 'jt/runway' into next
[flightgear.git] / src / Main / CameraGroup.cxx
1 // Copyright (C) 2008  Tim Moore
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License as
5 // published by the Free Software Foundation; either version 2 of the
6 // License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but
9 // WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16
17 #include "CameraGroup.hxx"
18
19 #include "globals.hxx"
20 #include "renderer.hxx"
21 #include "FGEventHandler.hxx"
22 #include "WindowBuilder.hxx"
23 #include "WindowSystemAdapter.hxx"
24 #include <simgear/props/props.hxx>
25 #include <simgear/structure/OSGUtils.hxx>
26 #include <simgear/scene/util/RenderConstants.hxx>
27
28 #include <algorithm>
29 #include <cstring>
30 #include <string>
31
32 #include <osg/Camera>
33 #include <osg/GraphicsContext>
34 #include <osg/Math>
35 #include <osg/Matrix>
36 #include <osg/Quat>
37 #include <osg/Vec3d>
38 #include <osg/Viewport>
39
40 #include <osgUtil/IntersectionVisitor>
41
42 #include <osgViewer/GraphicsWindow>
43
44 namespace flightgear
45 {
46 using namespace osg;
47
48 using std::strcmp;
49 using std::string;
50
51 ref_ptr<CameraGroup> CameraGroup::_defaultGroup;
52
53 CameraGroup::CameraGroup(osgViewer::Viewer* viewer) :
54     _viewer(viewer)
55 {
56 }
57
58 }
59
60 namespace
61 {
62 using namespace osg;
63
64 // Given a projection matrix, return a new one with the same frustum
65 // sides and new near / far values.
66
67 void makeNewProjMat(Matrixd& oldProj, double znear,
68                                        double zfar, Matrixd& projection)
69 {
70     projection = oldProj;
71     // Slightly inflate the near & far planes to avoid objects at the
72     // extremes being clipped out.
73     znear *= 0.999;
74     zfar *= 1.001;
75
76     // Clamp the projection matrix z values to the range (near, far)
77     double epsilon = 1.0e-6;
78     if (fabs(projection(0,3)) < epsilon &&
79         fabs(projection(1,3)) < epsilon &&
80         fabs(projection(2,3)) < epsilon) {
81         // Projection is Orthographic
82         epsilon = -1.0/(zfar - znear); // Used as a temp variable
83         projection(2,2) = 2.0*epsilon;
84         projection(3,2) = (zfar + znear)*epsilon;
85     } else {
86         // Projection is Perspective
87         double trans_near = (-znear*projection(2,2) + projection(3,2)) /
88             (-znear*projection(2,3) + projection(3,3));
89         double trans_far = (-zfar*projection(2,2) + projection(3,2)) /
90             (-zfar*projection(2,3) + projection(3,3));
91         double ratio = fabs(2.0/(trans_near - trans_far));
92         double center = -0.5*(trans_near + trans_far);
93
94         projection.postMult(osg::Matrixd(1.0, 0.0, 0.0, 0.0,
95                                          0.0, 1.0, 0.0, 0.0,
96                                          0.0, 0.0, ratio, 0.0,
97                                          0.0, 0.0, center*ratio, 1.0));
98     }
99 }
100
101 void installCullVisitor(Camera* camera)
102 {
103 #if 0 // Not yet
104     osgViewer::Renderer* renderer
105         = static_cast<osgViewer::Renderer*>(camera->getRenderer());
106     for (int i = 0; i < 2; ++i) {
107         osgUtil::SceneView* sceneView = renderer->getSceneView(i);
108         sceneView->setCullVisitor(new simgear::EffectCullVisitor);
109     }
110 #endif
111 }
112 }
113
114 namespace flightgear
115 {
116 void updateCameras(const CameraInfo* info)
117 {
118     if (info->camera.valid())
119         info->camera->getViewport()->setViewport(info->x, info->y,
120                                                  info->width, info->height);
121     if (info->farCamera.valid())
122         info->farCamera->getViewport()->setViewport(info->x, info->y,
123                                                     info->width, info->height);
124 }
125
126 CameraInfo* CameraGroup::addCamera(unsigned flags, Camera* camera,
127                                    const Matrix& view,
128                                    const Matrix& projection,
129                                    bool useMasterSceneData)
130 {
131     CameraInfo* info = new CameraInfo(flags);
132     // The camera group will always update the camera
133     camera->setReferenceFrame(Transform::ABSOLUTE_RF);
134
135     Camera* farCamera = 0;
136     if ((flags & (GUI | ORTHO)) == 0) {
137         farCamera = new Camera;
138         farCamera->setAllowEventFocus(camera->getAllowEventFocus());
139         farCamera->setGraphicsContext(camera->getGraphicsContext());
140         farCamera->setCullingMode(camera->getCullingMode());
141         farCamera->setInheritanceMask(camera->getInheritanceMask());
142         farCamera->setReferenceFrame(Transform::ABSOLUTE_RF);
143         // Each camera's viewport is written when the window is
144         // resized; if the the viewport isn't copied here, it gets updated
145         // twice and ends up with the wrong value.
146         farCamera->setViewport(simgear::clone(camera->getViewport()));
147         _viewer->addSlave(farCamera, view, projection, useMasterSceneData);
148         installCullVisitor(farCamera);
149         info->farCamera = farCamera;
150         info->farSlaveIndex = _viewer->getNumSlaves() - 1;
151         farCamera->setRenderOrder(Camera::POST_RENDER, info->farSlaveIndex);
152         camera->setCullMask(camera->getCullMask() & ~simgear::BACKGROUND_BIT);
153         camera->setClearMask(GL_DEPTH_BUFFER_BIT);
154     }
155     _viewer->addSlave(camera, view, projection, useMasterSceneData);
156     installCullVisitor(camera);
157     info->camera = camera;
158     info->slaveIndex = _viewer->getNumSlaves() - 1;
159     camera->setRenderOrder(Camera::POST_RENDER, info->slaveIndex);
160     _cameras.push_back(info);
161     return info;
162 }
163
164 void CameraGroup::update(const osg::Vec3d& position,
165                          const osg::Quat& orientation)
166 {
167     const Matrix masterView(osg::Matrix::translate(-position)
168                             * osg::Matrix::rotate(orientation.inverse()));
169     _viewer->getCamera()->setViewMatrix(masterView);
170     const Matrix& masterProj = _viewer->getCamera()->getProjectionMatrix();
171     for (CameraList::iterator i = _cameras.begin(); i != _cameras.end(); ++i) {
172         const CameraInfo* info = i->get();
173         const View::Slave& slave = _viewer->getSlave(info->slaveIndex);
174         // refreshes camera viewports (for now)
175         updateCameras(info);
176         Camera* camera = info->camera.get();
177         Matrix viewMatrix;
178         if ((info->flags & VIEW_ABSOLUTE) != 0)
179             viewMatrix = slave._viewOffset;
180         else
181             viewMatrix = masterView * slave._viewOffset;
182         camera->setViewMatrix(viewMatrix);
183         Matrix projectionMatrix;
184         if ((info->flags & PROJECTION_ABSOLUTE) != 0)
185             projectionMatrix = slave._projectionOffset;
186         else
187             projectionMatrix = masterProj * slave._projectionOffset;
188
189         if (!info->farCamera.valid()) {
190             camera->setProjectionMatrix(projectionMatrix);
191         } else {
192             Camera* farCamera = info->farCamera.get();
193             farCamera->setViewMatrix(viewMatrix);
194             double left, right, bottom, top, parentNear, parentFar;
195             projectionMatrix.getFrustum(left, right, bottom, top,
196                                         parentNear, parentFar);
197             if (parentFar < _nearField || _nearField == 0.0f) {
198                 camera->setProjectionMatrix(projectionMatrix);
199                 camera->setCullMask(camera->getCullMask()
200                                     | simgear::BACKGROUND_BIT);
201                 camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
202                 farCamera->setNodeMask(0);
203             } else {
204                 Matrix nearProj, farProj;
205                 makeNewProjMat(projectionMatrix, parentNear, _nearField,
206                                nearProj);
207                 makeNewProjMat(projectionMatrix, _nearField, parentFar,
208                                farProj);
209                 camera->setProjectionMatrix(nearProj);
210                 camera->setCullMask(camera->getCullMask()
211                                     & ~simgear::BACKGROUND_BIT);
212                 camera->setClearMask(GL_DEPTH_BUFFER_BIT);
213                 farCamera->setProjectionMatrix(farProj);
214                 farCamera->setNodeMask(camera->getNodeMask());
215             }
216         }
217     }
218 }
219
220 void CameraGroup::setCameraParameters(float vfov, float aspectRatio)
221 {
222     _viewer->getCamera()->setProjectionMatrixAsPerspective(vfov,
223                                                            1.0f / aspectRatio,
224                                                            _zNear, _zFar);
225 }
226 }
227
228 namespace
229 {
230 // A raw value for property nodes that references a class member via
231 // an osg::ref_ptr.
232 template<class C, class T>
233 class RefMember : public SGRawValue<T>
234 {
235 public:
236     RefMember (C *obj, T C::*ptr)
237         : _obj(obj), _ptr(ptr) {}
238     virtual ~RefMember () {}
239     virtual T getValue () const
240     {
241         return _obj.get()->*_ptr;
242     }
243     virtual bool setValue (T value)
244     {
245         _obj.get()->*_ptr = value;
246         return true;
247     }
248     virtual SGRawValue<T> * clone () const
249     {
250         return new RefMember(_obj.get(), _ptr);
251     }
252 private:
253     ref_ptr<C> _obj;
254     T C::* const _ptr;
255 };
256
257 template<typename C, typename T>
258 RefMember<C, T> makeRefMember(C *obj, T C::*ptr)
259 {
260     return RefMember<C, T>(obj, ptr);
261 }
262
263 template<typename C, typename T>
264 void bindMemberToNode(SGPropertyNode* parent, const char* childName,
265                       C* obj, T C::*ptr, T value)
266 {
267     SGPropertyNode* valNode = parent->getNode(childName);
268     RefMember<C, T> refMember = makeRefMember(obj, ptr);
269     if (!valNode) {
270         valNode = parent->getNode(childName, true);
271         valNode->tie(refMember, false);
272         setValue(valNode, value);
273     } else {
274         valNode->tie(refMember, true);
275     }
276 }
277
278 void buildViewport(flightgear::CameraInfo* info, SGPropertyNode* viewportNode,
279                    const osg::GraphicsContext::Traits *traits)
280 {
281     using namespace flightgear;
282     bindMemberToNode(viewportNode, "x", info, &CameraInfo::x, 0.0);
283     bindMemberToNode(viewportNode, "y", info, &CameraInfo::y, 0.0);
284     bindMemberToNode(viewportNode, "width", info, &CameraInfo::width,
285                      static_cast<double>(traits->width));
286     bindMemberToNode(viewportNode, "height", info, &CameraInfo::height,
287                      static_cast<double>(traits->height));
288 }
289 }
290
291 namespace flightgear
292 {
293
294 CameraInfo* CameraGroup::buildCamera(SGPropertyNode* cameraNode)
295 {
296     WindowBuilder *wBuild = WindowBuilder::getWindowBuilder();
297     const SGPropertyNode* windowNode = cameraNode->getNode("window");
298     GraphicsWindow* window = 0;
299     static int cameraNum = 0;
300     int cameraFlags = DO_INTERSECTION_TEST;
301     if (windowNode) {
302         // New style window declaration / definition
303         window = wBuild->buildWindow(windowNode);
304     } else {
305         // Old style: suck window params out of camera block
306         window = wBuild->buildWindow(cameraNode);
307     }
308     if (!window) {
309         return 0;
310     }
311     Camera* camera = new Camera;
312     camera->setAllowEventFocus(false);
313     camera->setGraphicsContext(window->gc.get());
314     camera->setViewport(new Viewport);
315     camera->setCullingMode(CullSettings::SMALL_FEATURE_CULLING
316                            | CullSettings::VIEW_FRUSTUM_CULLING);
317     camera->setInheritanceMask(CullSettings::ALL_VARIABLES
318                                & ~(CullSettings::CULL_MASK
319                                    | CullSettings::CULLING_MODE));
320
321     osg::Matrix pOff;
322     osg::Matrix vOff;
323     const SGPropertyNode* viewNode = cameraNode->getNode("view");
324     if (viewNode) {
325         double heading = viewNode->getDoubleValue("heading-deg", 0.0);
326         double pitch = viewNode->getDoubleValue("pitch-deg", 0.0);
327         double roll = viewNode->getDoubleValue("roll-deg", 0.0);
328         double x = viewNode->getDoubleValue("x", 0.0);
329         double y = viewNode->getDoubleValue("y", 0.0);
330         double z = viewNode->getDoubleValue("z", 0.0);
331         // Build a view matrix, which is the inverse of a model
332         // orientation matrix.
333         vOff = (Matrix::translate(-x, -y, -z)
334                 * Matrix::rotate(-DegreesToRadians(heading),
335                                  Vec3d(0.0, 1.0, 0.0),
336                                  -DegreesToRadians(pitch),
337                                  Vec3d(1.0, 0.0, 0.0),
338                                  -DegreesToRadians(roll),
339                                  Vec3d(0.0, 0.0, 1.0)));
340         if (viewNode->getBoolValue("absolute", false))
341             cameraFlags |= VIEW_ABSOLUTE;
342     } else {
343         // Old heading parameter, works in the opposite direction
344         double heading = cameraNode->getDoubleValue("heading-deg", 0.0);
345         vOff.makeRotate(DegreesToRadians(heading), osg::Vec3(0, 1, 0));
346     }
347     const SGPropertyNode* projectionNode = 0;
348     if ((projectionNode = cameraNode->getNode("perspective")) != 0) {
349         double fovy = projectionNode->getDoubleValue("fovy-deg", 55.0);
350         double aspectRatio = projectionNode->getDoubleValue("aspect-ratio",
351                                                             1.0);
352         double zNear = projectionNode->getDoubleValue("near", 0.0);
353         double zFar = projectionNode->getDoubleValue("far", 0.0);
354         double offsetX = projectionNode->getDoubleValue("offset-x", 0.0);
355         double offsetY = projectionNode->getDoubleValue("offset-y", 0.0);
356         double tan_fovy = tan(DegreesToRadians(fovy*0.5));
357         double right = tan_fovy * aspectRatio * zNear + offsetX;
358         double left = -tan_fovy * aspectRatio * zNear + offsetX;
359         double top = tan_fovy * zNear + offsetY;
360         double bottom = -tan_fovy * zNear + offsetY;
361         pOff.makeFrustum(left, right, bottom, top, zNear, zFar);
362         cameraFlags |= PROJECTION_ABSOLUTE;
363     } else if ((projectionNode = cameraNode->getNode("frustum")) != 0
364                || (projectionNode = cameraNode->getNode("ortho")) != 0) {
365         double top = projectionNode->getDoubleValue("top", 0.0);
366         double bottom = projectionNode->getDoubleValue("bottom", 0.0);
367         double left = projectionNode->getDoubleValue("left", 0.0);
368         double right = projectionNode->getDoubleValue("right", 0.0);
369         double zNear = projectionNode->getDoubleValue("near", 0.0);
370         double zFar = projectionNode->getDoubleValue("far", 0.0);
371         if (cameraNode->getNode("frustum")) {
372             pOff.makeFrustum(left, right, bottom, top, zNear, zFar);
373             cameraFlags |= PROJECTION_ABSOLUTE;
374         } else {
375             pOff.makeOrtho(left, right, bottom, top, zNear, zFar);
376             cameraFlags |= (PROJECTION_ABSOLUTE | ORTHO);
377         }
378     } else {
379         // old style shear parameters
380         double shearx = cameraNode->getDoubleValue("shear-x", 0);
381         double sheary = cameraNode->getDoubleValue("shear-y", 0);
382         pOff.makeTranslate(-shearx, -sheary, 0);
383     }
384     CameraInfo* info = addCamera(cameraFlags, camera, pOff, vOff);
385     // If a viewport isn't set on the camera, then it's hard to dig it
386     // out of the SceneView objects in the viewer, and the coordinates
387     // of mouse events are somewhat bizzare.
388     SGPropertyNode* viewportNode = cameraNode->getNode("viewport", true);
389     buildViewport(info, viewportNode, window->gc->getTraits());
390     updateCameras(info);
391     return info;
392 }
393
394 CameraInfo* CameraGroup::buildGUICamera(SGPropertyNode* cameraNode,
395                                         GraphicsWindow* window)
396 {
397     WindowBuilder *wBuild = WindowBuilder::getWindowBuilder();
398     const SGPropertyNode* windowNode = (cameraNode
399                                         ? cameraNode->getNode("window")
400                                         : 0);
401     static int cameraNum = 0;
402     if (!window) {
403         if (windowNode) {
404             // New style window declaration / definition
405             window = wBuild->buildWindow(windowNode);
406             
407         } else {
408             return 0;
409         }
410     }
411     Camera* camera = new Camera;
412     camera->setAllowEventFocus(false);
413     camera->setGraphicsContext(window->gc.get());
414     camera->setViewport(new Viewport);
415     // XXX Camera needs to be drawn last; eventually the render order
416     // should be assigned by a camera manager.
417     camera->setRenderOrder(osg::Camera::POST_RENDER, 100);
418         camera->setClearMask(0);
419     camera->setInheritanceMask(CullSettings::ALL_VARIABLES
420                                & ~(CullSettings::COMPUTE_NEAR_FAR_MODE
421                                    | CullSettings::CULLING_MODE));
422     camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
423     camera->setCullingMode(osg::CullSettings::NO_CULLING);
424     camera->setProjectionResizePolicy(Camera::FIXED);
425     camera->setReferenceFrame(Transform::ABSOLUTE_RF);
426     const int cameraFlags = GUI;
427     CameraInfo* result = addCamera(cameraFlags, camera, Matrixd::identity(),
428                                    Matrixd::identity(), false);
429     SGPropertyNode* viewportNode = cameraNode->getNode("viewport", true);
430     buildViewport(result, viewportNode, window->gc->getTraits());
431
432     // Disable statistics for the GUI camera.
433     result->camera->setStats(0);
434     updateCameras(result);
435     return result;
436 }
437
438 CameraGroup* CameraGroup::buildCameraGroup(osgViewer::Viewer* viewer,
439                                            SGPropertyNode* gnode)
440 {
441     CameraGroup* cgroup = new CameraGroup(viewer);
442     for (int i = 0; i < gnode->nChildren(); ++i) {
443         SGPropertyNode* pNode = gnode->getChild(i);
444         const char* name = pNode->getName();
445         if (!strcmp(name, "camera")) {
446             cgroup->buildCamera(pNode);
447         } else if (!strcmp(name, "window")) {
448             WindowBuilder::getWindowBuilder()->buildWindow(pNode);
449         } else if (!strcmp(name, "gui")) {
450             cgroup->buildGUICamera(pNode);
451         }
452     }
453     bindMemberToNode(gnode, "znear", cgroup, &CameraGroup::_zNear, .4f);
454     bindMemberToNode(gnode, "zfar", cgroup, &CameraGroup::_zFar, 120000.0f);
455     bindMemberToNode(gnode, "near-field", cgroup, &CameraGroup::_nearField,
456                      100.0f);
457     return cgroup;
458 }
459
460 void CameraGroup::setCameraCullMasks(Node::NodeMask nm)
461 {
462     for (CameraIterator i = camerasBegin(), e = camerasEnd(); i != e; ++i) {
463         CameraInfo* info = i->get();
464         if (info->flags & GUI)
465             continue;
466         if (info->farCamera.valid() && info->farCamera->getNodeMask() != 0) {
467             info->camera->setCullMask(nm & ~simgear::BACKGROUND_BIT);
468             info->farCamera->setCullMask(nm);
469         } else {
470             info->camera->setCullMask(nm);
471         }
472     }
473 }
474
475 void CameraGroup::resized()
476 {
477     for (CameraIterator i = camerasBegin(), e = camerasEnd(); i != e; ++i) {
478         CameraInfo *info = i->get();
479         const Viewport* viewport = info->camera->getViewport();
480         info->x = viewport->x();
481         info->y = viewport->y();
482         info->width = viewport->width();
483         info->height = viewport->height();
484     }
485 }
486
487 Camera* getGUICamera(CameraGroup* cgroup)
488 {
489     CameraGroup::CameraIterator end = cgroup->camerasEnd();
490     CameraGroup::CameraIterator result
491         = std::find_if(cgroup->camerasBegin(), end,
492                        FlagTester<CameraInfo>(CameraGroup::GUI));
493     if (result != end)
494         return (*result)->camera.get();
495     else
496         return 0;
497 }
498
499 bool computeIntersections(const CameraGroup* cgroup,
500                           const osgGA::GUIEventAdapter* ea,
501                           osgUtil::LineSegmentIntersector::Intersections& intersections)
502 {
503     using osgUtil::Intersector;
504     using osgUtil::LineSegmentIntersector;
505     double x, y;
506     eventToWindowCoords(ea, x, y);
507     // Find camera that contains event
508     for (CameraGroup::ConstCameraIterator iter = cgroup->camerasBegin(),
509              e = cgroup->camerasEnd();
510          iter != e;
511          ++iter) {
512         const CameraInfo* cinfo = iter->get();
513         if ((cinfo->flags & CameraGroup::DO_INTERSECTION_TEST) == 0)
514             continue;
515         const Camera* camera = cinfo->camera.get();
516         if (camera->getGraphicsContext() != ea->getGraphicsContext())
517             continue;
518         const Viewport* viewport = camera->getViewport();
519         double epsilon = 0.5;
520         if (!(x >= viewport->x() - epsilon
521               && x < viewport->x() + viewport->width() -1.0 + epsilon
522               && y >= viewport->y() - epsilon
523               && y < viewport->y() + viewport->height() -1.0 + epsilon))
524             continue;
525         Vec4d start(x, y, 0.0, 1.0);
526         Vec4d end(x, y, 1.0, 1.0);
527         Matrix windowMat = viewport->computeWindowMatrix();
528         Matrix startPtMat = Matrix::inverse(camera->getProjectionMatrix()
529                                             * windowMat);
530         Matrix endPtMat;
531         if (!cinfo->farCamera.valid() || cinfo->farCamera->getNodeMask() == 0)
532             endPtMat = startPtMat;
533         else
534             endPtMat = Matrix::inverse(cinfo->farCamera->getProjectionMatrix()
535                                        * windowMat);
536         start = start * startPtMat;
537         start /= start.w();
538         end = end * endPtMat;
539         end /= end.w();
540         ref_ptr<LineSegmentIntersector> picker
541             = new LineSegmentIntersector(Intersector::VIEW,
542                                          Vec3d(start.x(), start.y(), start.z()),
543                                          Vec3d(end.x(), end.y(), end.z()));
544         osgUtil::IntersectionVisitor iv(picker.get());
545         const_cast<Camera*>(camera)->accept(iv);
546         if (picker->containsIntersections()) {
547             intersections = picker->getIntersections();
548             return true;
549         } else {
550             break;
551         }
552     }
553     intersections.clear();
554     return false;
555 }
556
557 void warpGUIPointer(CameraGroup* cgroup, int x, int y)
558 {
559     using osgViewer::GraphicsWindow;
560     Camera* guiCamera = getGUICamera(cgroup);
561     if (!guiCamera)
562         return;
563     Viewport* vport = guiCamera->getViewport();
564     GraphicsWindow* gw
565         = dynamic_cast<GraphicsWindow*>(guiCamera->getGraphicsContext());
566     if (!gw)
567         return;
568     globals->get_renderer()->getEventHandler()->setMouseWarped();    
569     // Translate the warp request into the viewport of the GUI camera,
570     // send the request to the window, then transform the coordinates
571     // for the Viewer's event queue.
572     double wx = x + vport->x();
573     double wyUp = vport->height() + vport->y() - y;
574     double wy;
575     const GraphicsContext::Traits* traits = gw->getTraits();
576     if (gw->getEventQueue()->getCurrentEventState()->getMouseYOrientation()
577         == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS) {
578         wy = traits->height - wyUp;
579     } else {
580         wy = wyUp;
581     }
582     gw->getEventQueue()->mouseWarped(wx, wy);
583     gw->requestWarpPointer(wx, wy);
584     osgGA::GUIEventAdapter* eventState
585         = cgroup->getViewer()->getEventQueue()->getCurrentEventState();
586     double viewerX
587         = (eventState->getXmin()
588            + ((wx / double(traits->width))
589               * (eventState->getXmax() - eventState->getXmin())));
590     double viewerY
591         = (eventState->getYmin()
592            + ((wyUp / double(traits->height))
593               * (eventState->getYmax() - eventState->getYmin())));
594     cgroup->getViewer()->getEventQueue()->mouseWarped(viewerX, viewerY);
595 }
596 }