]> git.mxchange.org Git - flightgear.git/blob - src/Main/CameraGroup.cxx
938008967df26f7c23fdd7665b32ec22a9ab1647
[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 = simgear::clone(camera);
138         farCamera->setGraphicsContext(camera->getGraphicsContext());
139         // Each camera's viewport is written when the window is
140         // resized; if the the viewport isn't copied here, it gets updated
141         // twice and ends up with the wrong value.
142         farCamera->setViewport(simgear::clone(camera->getViewport()));
143         _viewer->addSlave(farCamera, view, projection, useMasterSceneData);
144         installCullVisitor(farCamera);
145         info->farCamera = farCamera;
146         info->farSlaveIndex = _viewer->getNumSlaves() - 1;
147         farCamera->setRenderOrder(Camera::NESTED_RENDER, info->farSlaveIndex);
148         camera->setCullMask(camera->getCullMask() & ~simgear::BACKGROUND_BIT);
149         camera->setClearMask(GL_DEPTH_BUFFER_BIT);
150     }
151     _viewer->addSlave(camera, view, projection, useMasterSceneData);
152     installCullVisitor(camera);
153     info->camera = camera;
154     info->slaveIndex = _viewer->getNumSlaves() - 1;
155     camera->setRenderOrder(Camera::NESTED_RENDER, info->slaveIndex);
156     _cameras.push_back(info);
157     return info;
158 }
159
160 void CameraGroup::update(const osg::Vec3d& position,
161                          const osg::Quat& orientation)
162 {
163     const Matrix masterView(osg::Matrix::translate(-position)
164                             * osg::Matrix::rotate(orientation.inverse()));
165     _viewer->getCamera()->setViewMatrix(masterView);
166     const Matrix& masterProj = _viewer->getCamera()->getProjectionMatrix();
167     for (CameraList::iterator i = _cameras.begin(); i != _cameras.end(); ++i) {
168         const CameraInfo* info = i->get();
169         const View::Slave& slave = _viewer->getSlave(info->slaveIndex);
170         // refreshes camera viewports (for now)
171         updateCameras(info);
172         Camera* camera = info->camera.get();
173         Matrix viewMatrix;
174         if ((info->flags & VIEW_ABSOLUTE) != 0)
175             viewMatrix = slave._viewOffset;
176         else
177             viewMatrix = masterView * slave._viewOffset;
178         camera->setViewMatrix(viewMatrix);
179         Matrix projectionMatrix;
180         if ((info->flags & PROJECTION_ABSOLUTE) != 0)
181             projectionMatrix = slave._projectionOffset;
182         else
183             projectionMatrix = masterProj * slave._projectionOffset;
184
185         if (!info->farCamera.valid()) {
186             camera->setProjectionMatrix(projectionMatrix);
187         } else {
188             Camera* farCamera = info->farCamera.get();
189             farCamera->setViewMatrix(viewMatrix);
190             double left, right, bottom, top, parentNear, parentFar;
191             projectionMatrix.getFrustum(left, right, bottom, top,
192                                         parentNear, parentFar);
193             if (parentFar < _nearField || _nearField == 0.0f) {
194                 camera->setProjectionMatrix(projectionMatrix);
195                 camera->setCullMask(camera->getCullMask()
196                                     | simgear::BACKGROUND_BIT);
197                 camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
198                 farCamera->setNodeMask(0);
199             } else {
200                 Matrix nearProj, farProj;
201                 makeNewProjMat(projectionMatrix, parentNear, _nearField,
202                                nearProj);
203                 makeNewProjMat(projectionMatrix, _nearField, parentFar,
204                                farProj);
205                 camera->setProjectionMatrix(nearProj);
206                 camera->setCullMask(camera->getCullMask()
207                                     & ~simgear::BACKGROUND_BIT);
208                 camera->setClearMask(GL_DEPTH_BUFFER_BIT);
209                 farCamera->setProjectionMatrix(farProj);
210                 farCamera->setNodeMask(camera->getNodeMask());
211             }
212         }
213     }
214 }
215
216 void CameraGroup::setCameraParameters(float vfov, float aspectRatio)
217 {
218     _viewer->getCamera()->setProjectionMatrixAsPerspective(vfov,
219                                                            1.0f / aspectRatio,
220                                                            _zNear, _zFar);
221 }
222 }
223
224 namespace
225 {
226 // A raw value for property nodes that references a class member via
227 // an osg::ref_ptr.
228 template<class C, class T>
229 class RefMember : public SGRawValue<T>
230 {
231 public:
232     RefMember (C *obj, T C::*ptr)
233         : _obj(obj), _ptr(ptr) {}
234     virtual ~RefMember () {}
235     virtual T getValue () const
236     {
237         return _obj.get()->*_ptr;
238     }
239     virtual bool setValue (T value)
240     {
241         _obj.get()->*_ptr = value;
242         return true;
243     }
244     virtual SGRawValue<T> * clone () const
245     {
246         return new RefMember(_obj.get(), _ptr);
247     }
248 private:
249     ref_ptr<C> _obj;
250     T C::* const _ptr;
251 };
252
253 template<typename C, typename T>
254 RefMember<C, T> makeRefMember(C *obj, T C::*ptr)
255 {
256     return RefMember<C, T>(obj, ptr);
257 }
258
259 template<typename C, typename T>
260 void bindMemberToNode(SGPropertyNode* parent, const char* childName,
261                       C* obj, T C::*ptr, T value)
262 {
263     SGPropertyNode* valNode = parent->getNode(childName);
264     RefMember<C, T> refMember = makeRefMember(obj, ptr);
265     if (!valNode) {
266         valNode = parent->getNode(childName, true);
267         valNode->tie(refMember, false);
268         setValue(valNode, value);
269     } else {
270         valNode->tie(refMember, true);
271     }
272 }
273
274 void buildViewport(flightgear::CameraInfo* info, SGPropertyNode* viewportNode,
275                    const osg::GraphicsContext::Traits *traits)
276 {
277     using namespace flightgear;
278     bindMemberToNode(viewportNode, "x", info, &CameraInfo::x, 0.0);
279     bindMemberToNode(viewportNode, "y", info, &CameraInfo::y, 0.0);
280     bindMemberToNode(viewportNode, "width", info, &CameraInfo::width,
281                      static_cast<double>(traits->width));
282     bindMemberToNode(viewportNode, "height", info, &CameraInfo::height,
283                      static_cast<double>(traits->height));
284 }
285 }
286
287 namespace flightgear
288 {
289
290 CameraInfo* CameraGroup::buildCamera(SGPropertyNode* cameraNode)
291 {
292     WindowBuilder *wBuild = WindowBuilder::getWindowBuilder();
293     const SGPropertyNode* windowNode = cameraNode->getNode("window");
294     GraphicsWindow* window = 0;
295     static int cameraNum = 0;
296     int cameraFlags = DO_INTERSECTION_TEST;
297     if (windowNode) {
298         // New style window declaration / definition
299         window = wBuild->buildWindow(windowNode);
300     } else {
301         // Old style: suck window params out of camera block
302         window = wBuild->buildWindow(cameraNode);
303     }
304     if (!window) {
305         return 0;
306     }
307     Camera* camera = new Camera;
308     camera->setAllowEventFocus(false);
309     camera->setGraphicsContext(window->gc.get());
310     camera->setViewport(new Viewport);
311     camera->setCullingMode(CullSettings::SMALL_FEATURE_CULLING
312                            | CullSettings::VIEW_FRUSTUM_CULLING);
313     camera->setInheritanceMask(CullSettings::ALL_VARIABLES
314                                & ~(CullSettings::CULL_MASK
315                                    | CullSettings::CULLING_MODE));
316
317     osg::Matrix pOff;
318     osg::Matrix vOff;
319     const SGPropertyNode* viewNode = cameraNode->getNode("view");
320     if (viewNode) {
321         double heading = viewNode->getDoubleValue("heading-deg", 0.0);
322         double pitch = viewNode->getDoubleValue("pitch-deg", 0.0);
323         double roll = viewNode->getDoubleValue("roll-deg", 0.0);
324         double x = viewNode->getDoubleValue("x", 0.0);
325         double y = viewNode->getDoubleValue("y", 0.0);
326         double z = viewNode->getDoubleValue("z", 0.0);
327         // Build a view matrix, which is the inverse of a model
328         // orientation matrix.
329         vOff = (Matrix::translate(-x, -y, -z)
330                 * Matrix::rotate(-DegreesToRadians(heading),
331                                  Vec3d(0.0, 1.0, 0.0),
332                                  -DegreesToRadians(pitch),
333                                  Vec3d(1.0, 0.0, 0.0),
334                                  -DegreesToRadians(roll),
335                                  Vec3d(0.0, 0.0, 1.0)));
336         if (viewNode->getBoolValue("absolute", false))
337             cameraFlags |= VIEW_ABSOLUTE;
338     } else {
339         // Old heading parameter, works in the opposite direction
340         double heading = cameraNode->getDoubleValue("heading-deg", 0.0);
341         vOff.makeRotate(DegreesToRadians(heading), osg::Vec3(0, 1, 0));
342     }
343     const SGPropertyNode* projectionNode = 0;
344     if ((projectionNode = cameraNode->getNode("perspective")) != 0) {
345         double fovy = projectionNode->getDoubleValue("fovy-deg", 55.0);
346         double aspectRatio = projectionNode->getDoubleValue("aspect-ratio",
347                                                             1.0);
348         double zNear = projectionNode->getDoubleValue("near", 0.0);
349         double zFar = projectionNode->getDoubleValue("far", 0.0);
350         double offsetX = projectionNode->getDoubleValue("offset-x", 0.0);
351         double offsetY = projectionNode->getDoubleValue("offset-y", 0.0);
352         double tan_fovy = tan(DegreesToRadians(fovy*0.5));
353         double right = tan_fovy * aspectRatio * zNear + offsetX;
354         double left = -tan_fovy * aspectRatio * zNear + offsetX;
355         double top = tan_fovy * zNear + offsetY;
356         double bottom = -tan_fovy * zNear + offsetY;
357         pOff.makeFrustum(left, right, bottom, top, zNear, zFar);
358         cameraFlags |= PROJECTION_ABSOLUTE;
359     } else if ((projectionNode = cameraNode->getNode("frustum")) != 0
360                || (projectionNode = cameraNode->getNode("ortho")) != 0) {
361         double top = projectionNode->getDoubleValue("top", 0.0);
362         double bottom = projectionNode->getDoubleValue("bottom", 0.0);
363         double left = projectionNode->getDoubleValue("left", 0.0);
364         double right = projectionNode->getDoubleValue("right", 0.0);
365         double zNear = projectionNode->getDoubleValue("near", 0.0);
366         double zFar = projectionNode->getDoubleValue("far", 0.0);
367         if (cameraNode->getNode("frustum")) {
368             pOff.makeFrustum(left, right, bottom, top, zNear, zFar);
369             cameraFlags |= PROJECTION_ABSOLUTE;
370         } else {
371             pOff.makeOrtho(left, right, bottom, top, zNear, zFar);
372             cameraFlags |= (PROJECTION_ABSOLUTE | ORTHO);
373         }
374     } else {
375         // old style shear parameters
376         double shearx = cameraNode->getDoubleValue("shear-x", 0);
377         double sheary = cameraNode->getDoubleValue("shear-y", 0);
378         pOff.makeTranslate(-shearx, -sheary, 0);
379     }
380     CameraInfo* info = addCamera(cameraFlags, camera, pOff, vOff);
381     // If a viewport isn't set on the camera, then it's hard to dig it
382     // out of the SceneView objects in the viewer, and the coordinates
383     // of mouse events are somewhat bizzare.
384     SGPropertyNode* viewportNode = cameraNode->getNode("viewport", true);
385     buildViewport(info, viewportNode, window->gc->getTraits());
386     updateCameras(info);
387     return info;
388 }
389
390 CameraInfo* CameraGroup::buildGUICamera(SGPropertyNode* cameraNode,
391                                         GraphicsWindow* window)
392 {
393     WindowBuilder *wBuild = WindowBuilder::getWindowBuilder();
394     const SGPropertyNode* windowNode = (cameraNode
395                                         ? cameraNode->getNode("window")
396                                         : 0);
397     static int cameraNum = 0;
398     if (!window) {
399         if (windowNode) {
400             // New style window declaration / definition
401             window = wBuild->buildWindow(windowNode);
402             
403         } else {
404             return 0;
405         }
406     }
407     Camera* camera = new Camera;
408     camera->setAllowEventFocus(false);
409     camera->setGraphicsContext(window->gc.get());
410     camera->setViewport(new Viewport);
411     // XXX Camera needs to be drawn last; eventually the render order
412     // should be assigned by a camera manager.
413     camera->setRenderOrder(osg::Camera::POST_RENDER, 100);
414         camera->setClearMask(0);
415     camera->setInheritanceMask(CullSettings::ALL_VARIABLES
416                                & ~(CullSettings::COMPUTE_NEAR_FAR_MODE
417                                    | CullSettings::CULLING_MODE));
418     camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
419     camera->setCullingMode(osg::CullSettings::NO_CULLING);
420     camera->setProjectionResizePolicy(Camera::FIXED);
421     camera->setReferenceFrame(Transform::ABSOLUTE_RF);
422     const int cameraFlags = GUI;
423     CameraInfo* result = addCamera(cameraFlags, camera, Matrixd::identity(),
424                                    Matrixd::identity(), false);
425     SGPropertyNode* viewportNode = cameraNode->getNode("viewport", true);
426     buildViewport(result, viewportNode, window->gc->getTraits());
427
428     // Disable statistics for the GUI camera.
429     result->camera->setStats(0);
430     updateCameras(result);
431     return result;
432 }
433
434 CameraGroup* CameraGroup::buildCameraGroup(osgViewer::Viewer* viewer,
435                                            SGPropertyNode* gnode)
436 {
437     CameraGroup* cgroup = new CameraGroup(viewer);
438     for (int i = 0; i < gnode->nChildren(); ++i) {
439         SGPropertyNode* pNode = gnode->getChild(i);
440         const char* name = pNode->getName();
441         if (!strcmp(name, "camera")) {
442             cgroup->buildCamera(pNode);
443         } else if (!strcmp(name, "window")) {
444             WindowBuilder::getWindowBuilder()->buildWindow(pNode);
445         } else if (!strcmp(name, "gui")) {
446             cgroup->buildGUICamera(pNode);
447         }
448     }
449     bindMemberToNode(gnode, "znear", cgroup, &CameraGroup::_zNear, .4f);
450     bindMemberToNode(gnode, "zfar", cgroup, &CameraGroup::_zFar, 120000.0f);
451     bindMemberToNode(gnode, "near-field", cgroup, &CameraGroup::_nearField,
452                      100.0f);
453     return cgroup;
454 }
455
456 void CameraGroup::setCameraCullMasks(Node::NodeMask nm)
457 {
458     for (CameraIterator i = camerasBegin(), e = camerasEnd(); i != e; ++i) {
459         CameraInfo* info = i->get();
460         if (info->flags & GUI)
461             continue;
462         if (info->farCamera.valid() && info->farCamera->getNodeMask() != 0) {
463             info->camera->setCullMask(nm & ~simgear::BACKGROUND_BIT);
464             info->farCamera->setCullMask(nm);
465         } else {
466             info->camera->setCullMask(nm);
467         }
468     }
469 }
470
471 void CameraGroup::resized()
472 {
473     for (CameraIterator i = camerasBegin(), e = camerasEnd(); i != e; ++i) {
474         CameraInfo *info = i->get();
475         const Viewport* viewport = info->camera->getViewport();
476         info->x = viewport->x();
477         info->y = viewport->y();
478         info->width = viewport->width();
479         info->height = viewport->height();
480     }
481 }
482
483 Camera* getGUICamera(CameraGroup* cgroup)
484 {
485     CameraGroup::CameraIterator end = cgroup->camerasEnd();
486     CameraGroup::CameraIterator result
487         = std::find_if(cgroup->camerasBegin(), end,
488                        FlagTester<CameraInfo>(CameraGroup::GUI));
489     if (result != end)
490         return (*result)->camera.get();
491     else
492         return 0;
493 }
494
495 bool computeIntersections(const CameraGroup* cgroup,
496                           const osgGA::GUIEventAdapter* ea,
497                           osgUtil::LineSegmentIntersector::Intersections& intersections)
498 {
499     using osgUtil::Intersector;
500     using osgUtil::LineSegmentIntersector;
501     double x, y;
502     eventToWindowCoords(ea, x, y);
503     // Find camera that contains event
504     for (CameraGroup::ConstCameraIterator iter = cgroup->camerasBegin(),
505              e = cgroup->camerasEnd();
506          iter != e;
507          ++iter) {
508         const CameraInfo* cinfo = iter->get();
509         if ((cinfo->flags & CameraGroup::DO_INTERSECTION_TEST) == 0)
510             continue;
511         const Camera* camera = cinfo->camera.get();
512         if (camera->getGraphicsContext() != ea->getGraphicsContext())
513             continue;
514         const Viewport* viewport = camera->getViewport();
515         double epsilon = 0.5;
516         if (!(x >= viewport->x() - epsilon
517               && x < viewport->x() + viewport->width() -1.0 + epsilon
518               && y >= viewport->y() - epsilon
519               && y < viewport->y() + viewport->height() -1.0 + epsilon))
520             continue;
521         Vec4d start(x, y, 0.0, 1.0);
522         Vec4d end(x, y, 1.0, 1.0);
523         Matrix windowMat = viewport->computeWindowMatrix();
524         Matrix startPtMat = Matrix::inverse(camera->getProjectionMatrix()
525                                             * windowMat);
526         Matrix endPtMat;
527         if (!cinfo->farCamera.valid() || cinfo->farCamera->getNodeMask() == 0)
528             endPtMat = startPtMat;
529         else
530             endPtMat = Matrix::inverse(cinfo->farCamera->getProjectionMatrix()
531                                        * windowMat);
532         start = start * startPtMat;
533         start /= start.w();
534         end = end * endPtMat;
535         end /= end.w();
536         ref_ptr<LineSegmentIntersector> picker
537             = new LineSegmentIntersector(Intersector::VIEW,
538                                          Vec3d(start.x(), start.y(), start.z()),
539                                          Vec3d(end.x(), end.y(), end.z()));
540         osgUtil::IntersectionVisitor iv(picker.get());
541         const_cast<Camera*>(camera)->accept(iv);
542         if (picker->containsIntersections()) {
543             intersections = picker->getIntersections();
544             return true;
545         } else {
546             break;
547         }
548     }
549     intersections.clear();
550     return false;
551 }
552
553 void warpGUIPointer(CameraGroup* cgroup, int x, int y)
554 {
555     using osgViewer::GraphicsWindow;
556     Camera* guiCamera = getGUICamera(cgroup);
557     if (!guiCamera)
558         return;
559     Viewport* vport = guiCamera->getViewport();
560     GraphicsWindow* gw
561         = dynamic_cast<GraphicsWindow*>(guiCamera->getGraphicsContext());
562     if (!gw)
563         return;
564     globals->get_renderer()->getEventHandler()->setMouseWarped();    
565     // Translate the warp request into the viewport of the GUI camera,
566     // send the request to the window, then transform the coordinates
567     // for the Viewer's event queue.
568     double wx = x + vport->x();
569     double wyUp = vport->height() + vport->y() - y;
570     double wy;
571     const GraphicsContext::Traits* traits = gw->getTraits();
572     if (gw->getEventQueue()->getCurrentEventState()->getMouseYOrientation()
573         == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS) {
574         wy = traits->height - wyUp;
575     } else {
576         wy = wyUp;
577     }
578     gw->getEventQueue()->mouseWarped(wx, wy);
579     gw->requestWarpPointer(wx, wy);
580     osgGA::GUIEventAdapter* eventState
581         = cgroup->getViewer()->getEventQueue()->getCurrentEventState();
582     double viewerX
583         = (eventState->getXmin()
584            + ((wx / double(traits->width))
585               * (eventState->getXmax() - eventState->getXmin())));
586     double viewerY
587         = (eventState->getYmin()
588            + ((wyUp / double(traits->height))
589               * (eventState->getYmax() - eventState->getYmin())));
590     cgroup->getViewer()->getEventQueue()->mouseWarped(viewerX, viewerY);
591 }
592 }