]> git.mxchange.org Git - flightgear.git/blob - src/Main/CameraGroup.cxx
Update camera inheritance masks because of OSG changeset 10838
[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/material/EffectCullVisitor.hxx>
27 #include <simgear/scene/util/RenderConstants.hxx>
28
29 #include <algorithm>
30 #include <cstring>
31 #include <string>
32
33 #include <osg/Camera>
34 #include <osg/GraphicsContext>
35 #include <osg/Math>
36 #include <osg/Matrix>
37 #include <osg/Quat>
38 #include <osg/Vec3d>
39 #include <osg/Viewport>
40
41 #include <osgUtil/IntersectionVisitor>
42
43 #include <osgViewer/GraphicsWindow>
44 #include <osgViewer/Renderer>
45
46 namespace flightgear
47 {
48 using namespace osg;
49
50 using std::strcmp;
51 using std::string;
52
53 ref_ptr<CameraGroup> CameraGroup::_defaultGroup;
54
55 CameraGroup::CameraGroup(osgViewer::Viewer* viewer) :
56     _viewer(viewer)
57 {
58 }
59
60 }
61
62 namespace
63 {
64 using namespace osg;
65
66 // Given a projection matrix, return a new one with the same frustum
67 // sides and new near / far values.
68
69 void makeNewProjMat(Matrixd& oldProj, double znear,
70                                        double zfar, Matrixd& projection)
71 {
72     projection = oldProj;
73     // Slightly inflate the near & far planes to avoid objects at the
74     // extremes being clipped out.
75     znear *= 0.999;
76     zfar *= 1.001;
77
78     // Clamp the projection matrix z values to the range (near, far)
79     double epsilon = 1.0e-6;
80     if (fabs(projection(0,3)) < epsilon &&
81         fabs(projection(1,3)) < epsilon &&
82         fabs(projection(2,3)) < epsilon) {
83         // Projection is Orthographic
84         epsilon = -1.0/(zfar - znear); // Used as a temp variable
85         projection(2,2) = 2.0*epsilon;
86         projection(3,2) = (zfar + znear)*epsilon;
87     } else {
88         // Projection is Perspective
89         double trans_near = (-znear*projection(2,2) + projection(3,2)) /
90             (-znear*projection(2,3) + projection(3,3));
91         double trans_far = (-zfar*projection(2,2) + projection(3,2)) /
92             (-zfar*projection(2,3) + projection(3,3));
93         double ratio = fabs(2.0/(trans_near - trans_far));
94         double center = -0.5*(trans_near + trans_far);
95
96         projection.postMult(osg::Matrixd(1.0, 0.0, 0.0, 0.0,
97                                          0.0, 1.0, 0.0, 0.0,
98                                          0.0, 0.0, ratio, 0.0,
99                                          0.0, 0.0, center*ratio, 1.0));
100     }
101 }
102
103 void installCullVisitor(Camera* camera)
104 {
105     osgViewer::Renderer* renderer
106         = static_cast<osgViewer::Renderer*>(camera->getRenderer());
107     for (int i = 0; i < 2; ++i) {
108         osgUtil::SceneView* sceneView = renderer->getSceneView(i);
109         sceneView->setCullVisitor(new simgear::EffectCullVisitor);
110     }
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     if (vfov != 0.0f && aspectRatio != 0.0f)
223         _viewer->getCamera()
224             ->setProjectionMatrixAsPerspective(vfov,
225                                                1.0f / aspectRatio,
226                                                _zNear, _zFar);
227 }
228 }
229
230 namespace
231 {
232 // A raw value for property nodes that references a class member via
233 // an osg::ref_ptr.
234 template<class C, class T>
235 class RefMember : public SGRawValue<T>
236 {
237 public:
238     RefMember (C *obj, T C::*ptr)
239         : _obj(obj), _ptr(ptr) {}
240     virtual ~RefMember () {}
241     virtual T getValue () const
242     {
243         return _obj.get()->*_ptr;
244     }
245     virtual bool setValue (T value)
246     {
247         _obj.get()->*_ptr = value;
248         return true;
249     }
250     virtual SGRawValue<T> * clone () const
251     {
252         return new RefMember(_obj.get(), _ptr);
253     }
254 private:
255     ref_ptr<C> _obj;
256     T C::* const _ptr;
257 };
258
259 template<typename C, typename T>
260 RefMember<C, T> makeRefMember(C *obj, T C::*ptr)
261 {
262     return RefMember<C, T>(obj, ptr);
263 }
264
265 template<typename C, typename T>
266 void bindMemberToNode(SGPropertyNode* parent, const char* childName,
267                       C* obj, T C::*ptr, T value)
268 {
269     SGPropertyNode* valNode = parent->getNode(childName);
270     RefMember<C, T> refMember = makeRefMember(obj, ptr);
271     if (!valNode) {
272         valNode = parent->getNode(childName, true);
273         valNode->tie(refMember, false);
274         setValue(valNode, value);
275     } else {
276         valNode->tie(refMember, true);
277     }
278 }
279
280 void buildViewport(flightgear::CameraInfo* info, SGPropertyNode* viewportNode,
281                    const osg::GraphicsContext::Traits *traits)
282 {
283     using namespace flightgear;
284     bindMemberToNode(viewportNode, "x", info, &CameraInfo::x, 0.0);
285     bindMemberToNode(viewportNode, "y", info, &CameraInfo::y, 0.0);
286     bindMemberToNode(viewportNode, "width", info, &CameraInfo::width,
287                      static_cast<double>(traits->width));
288     bindMemberToNode(viewportNode, "height", info, &CameraInfo::height,
289                      static_cast<double>(traits->height));
290 }
291 }
292
293 namespace flightgear
294 {
295
296 CameraInfo* CameraGroup::buildCamera(SGPropertyNode* cameraNode)
297 {
298     WindowBuilder *wBuild = WindowBuilder::getWindowBuilder();
299     const SGPropertyNode* windowNode = cameraNode->getNode("window");
300     GraphicsWindow* window = 0;
301     int cameraFlags = DO_INTERSECTION_TEST;
302     if (windowNode) {
303         // New style window declaration / definition
304         window = wBuild->buildWindow(windowNode);
305     } else {
306         // Old style: suck window params out of camera block
307         window = wBuild->buildWindow(cameraNode);
308     }
309     if (!window) {
310         return 0;
311     }
312     Camera* camera = new Camera;
313     camera->setAllowEventFocus(false);
314     camera->setGraphicsContext(window->gc.get());
315     camera->setViewport(new Viewport);
316     camera->setCullingMode(CullSettings::SMALL_FEATURE_CULLING
317                            | CullSettings::VIEW_FRUSTUM_CULLING);
318     camera->setInheritanceMask(CullSettings::ALL_VARIABLES
319                                & ~(CullSettings::CULL_MASK
320                                    | CullSettings::CULLING_MODE
321                                    | CullSettings::CLEAR_MASK));
322
323     osg::Matrix pOff;
324     osg::Matrix vOff;
325     const SGPropertyNode* viewNode = cameraNode->getNode("view");
326     if (viewNode) {
327         double heading = viewNode->getDoubleValue("heading-deg", 0.0);
328         double pitch = viewNode->getDoubleValue("pitch-deg", 0.0);
329         double roll = viewNode->getDoubleValue("roll-deg", 0.0);
330         double x = viewNode->getDoubleValue("x", 0.0);
331         double y = viewNode->getDoubleValue("y", 0.0);
332         double z = viewNode->getDoubleValue("z", 0.0);
333         // Build a view matrix, which is the inverse of a model
334         // orientation matrix.
335         vOff = (Matrix::translate(-x, -y, -z)
336                 * Matrix::rotate(-DegreesToRadians(heading),
337                                  Vec3d(0.0, 1.0, 0.0),
338                                  -DegreesToRadians(pitch),
339                                  Vec3d(1.0, 0.0, 0.0),
340                                  -DegreesToRadians(roll),
341                                  Vec3d(0.0, 0.0, 1.0)));
342         if (viewNode->getBoolValue("absolute", false))
343             cameraFlags |= VIEW_ABSOLUTE;
344     } else {
345         // Old heading parameter, works in the opposite direction
346         double heading = cameraNode->getDoubleValue("heading-deg", 0.0);
347         vOff.makeRotate(DegreesToRadians(heading), osg::Vec3(0, 1, 0));
348     }
349     const SGPropertyNode* projectionNode = 0;
350     if ((projectionNode = cameraNode->getNode("perspective")) != 0) {
351         double fovy = projectionNode->getDoubleValue("fovy-deg", 55.0);
352         double aspectRatio = projectionNode->getDoubleValue("aspect-ratio",
353                                                             1.0);
354         double zNear = projectionNode->getDoubleValue("near", 0.0);
355         double zFar = projectionNode->getDoubleValue("far", 0.0);
356         double offsetX = projectionNode->getDoubleValue("offset-x", 0.0);
357         double offsetY = projectionNode->getDoubleValue("offset-y", 0.0);
358         double tan_fovy = tan(DegreesToRadians(fovy*0.5));
359         double right = tan_fovy * aspectRatio * zNear + offsetX;
360         double left = -tan_fovy * aspectRatio * zNear + offsetX;
361         double top = tan_fovy * zNear + offsetY;
362         double bottom = -tan_fovy * zNear + offsetY;
363         pOff.makeFrustum(left, right, bottom, top, zNear, zFar);
364         cameraFlags |= PROJECTION_ABSOLUTE;
365     } else if ((projectionNode = cameraNode->getNode("frustum")) != 0
366                || (projectionNode = cameraNode->getNode("ortho")) != 0) {
367         double top = projectionNode->getDoubleValue("top", 0.0);
368         double bottom = projectionNode->getDoubleValue("bottom", 0.0);
369         double left = projectionNode->getDoubleValue("left", 0.0);
370         double right = projectionNode->getDoubleValue("right", 0.0);
371         double zNear = projectionNode->getDoubleValue("near", 0.0);
372         double zFar = projectionNode->getDoubleValue("far", 0.0);
373         if (cameraNode->getNode("frustum")) {
374             pOff.makeFrustum(left, right, bottom, top, zNear, zFar);
375             cameraFlags |= PROJECTION_ABSOLUTE;
376         } else {
377             pOff.makeOrtho(left, right, bottom, top, zNear, zFar);
378             cameraFlags |= (PROJECTION_ABSOLUTE | ORTHO);
379         }
380     } else {
381         // old style shear parameters
382         double shearx = cameraNode->getDoubleValue("shear-x", 0);
383         double sheary = cameraNode->getDoubleValue("shear-y", 0);
384         pOff.makeTranslate(-shearx, -sheary, 0);
385     }
386     CameraInfo* info = addCamera(cameraFlags, camera, pOff, vOff);
387     // If a viewport isn't set on the camera, then it's hard to dig it
388     // out of the SceneView objects in the viewer, and the coordinates
389     // of mouse events are somewhat bizzare.
390     SGPropertyNode* viewportNode = cameraNode->getNode("viewport", true);
391     buildViewport(info, viewportNode, window->gc->getTraits());
392     updateCameras(info);
393     return info;
394 }
395
396 CameraInfo* CameraGroup::buildGUICamera(SGPropertyNode* cameraNode,
397                                         GraphicsWindow* window)
398 {
399     WindowBuilder *wBuild = WindowBuilder::getWindowBuilder();
400     const SGPropertyNode* windowNode = (cameraNode
401                                         ? cameraNode->getNode("window")
402                                         : 0);
403     if (!window) {
404         if (windowNode) {
405             // New style window declaration / definition
406             window = wBuild->buildWindow(windowNode);
407             
408         } else {
409             return 0;
410         }
411     }
412     Camera* camera = new Camera;
413     camera->setAllowEventFocus(false);
414     camera->setGraphicsContext(window->gc.get());
415     camera->setViewport(new Viewport);
416     // XXX Camera needs to be drawn last; eventually the render order
417     // should be assigned by a camera manager.
418     camera->setRenderOrder(osg::Camera::POST_RENDER, 100);
419         camera->setClearMask(0);
420     camera->setInheritanceMask(CullSettings::ALL_VARIABLES
421                                & ~(CullSettings::COMPUTE_NEAR_FAR_MODE
422                                    | CullSettings::CULLING_MODE
423                                    | CullSettings::CLEAR_MASK));
424     camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
425     camera->setCullingMode(osg::CullSettings::NO_CULLING);
426     camera->setProjectionResizePolicy(Camera::FIXED);
427     camera->setReferenceFrame(Transform::ABSOLUTE_RF);
428     const int cameraFlags = GUI;
429     CameraInfo* result = addCamera(cameraFlags, camera, Matrixd::identity(),
430                                    Matrixd::identity(), false);
431     SGPropertyNode* viewportNode = cameraNode->getNode("viewport", true);
432     buildViewport(result, viewportNode, window->gc->getTraits());
433
434     // Disable statistics for the GUI camera.
435     result->camera->setStats(0);
436     updateCameras(result);
437     return result;
438 }
439
440 CameraGroup* CameraGroup::buildCameraGroup(osgViewer::Viewer* viewer,
441                                            SGPropertyNode* gnode)
442 {
443     CameraGroup* cgroup = new CameraGroup(viewer);
444     for (int i = 0; i < gnode->nChildren(); ++i) {
445         SGPropertyNode* pNode = gnode->getChild(i);
446         const char* name = pNode->getName();
447         if (!strcmp(name, "camera")) {
448             cgroup->buildCamera(pNode);
449         } else if (!strcmp(name, "window")) {
450             WindowBuilder::getWindowBuilder()->buildWindow(pNode);
451         } else if (!strcmp(name, "gui")) {
452             cgroup->buildGUICamera(pNode);
453         }
454     }
455     bindMemberToNode(gnode, "znear", cgroup, &CameraGroup::_zNear, .1f);
456     bindMemberToNode(gnode, "zfar", cgroup, &CameraGroup::_zFar, 120000.0f);
457     bindMemberToNode(gnode, "near-field", cgroup, &CameraGroup::_nearField,
458                      100.0f);
459     return cgroup;
460 }
461
462 void CameraGroup::setCameraCullMasks(Node::NodeMask nm)
463 {
464     for (CameraIterator i = camerasBegin(), e = camerasEnd(); i != e; ++i) {
465         CameraInfo* info = i->get();
466         if (info->flags & GUI)
467             continue;
468         if (info->farCamera.valid() && info->farCamera->getNodeMask() != 0) {
469             info->camera->setCullMask(nm & ~simgear::BACKGROUND_BIT);
470             info->farCamera->setCullMask(nm);
471         } else {
472             info->camera->setCullMask(nm);
473         }
474     }
475 }
476
477 void CameraGroup::resized()
478 {
479     for (CameraIterator i = camerasBegin(), e = camerasEnd(); i != e; ++i) {
480         CameraInfo *info = i->get();
481         const Viewport* viewport = info->camera->getViewport();
482         info->x = viewport->x();
483         info->y = viewport->y();
484         info->width = viewport->width();
485         info->height = viewport->height();
486     }
487 }
488
489 Camera* getGUICamera(CameraGroup* cgroup)
490 {
491     CameraGroup::CameraIterator end = cgroup->camerasEnd();
492     CameraGroup::CameraIterator result
493         = std::find_if(cgroup->camerasBegin(), end,
494                        FlagTester<CameraInfo>(CameraGroup::GUI));
495     if (result != end)
496         return (*result)->camera.get();
497     else
498         return 0;
499 }
500
501 bool computeIntersections(const CameraGroup* cgroup,
502                           const osgGA::GUIEventAdapter* ea,
503                           osgUtil::LineSegmentIntersector::Intersections& intersections)
504 {
505     using osgUtil::Intersector;
506     using osgUtil::LineSegmentIntersector;
507     double x, y;
508     eventToWindowCoords(ea, x, y);
509     // Find camera that contains event
510     for (CameraGroup::ConstCameraIterator iter = cgroup->camerasBegin(),
511              e = cgroup->camerasEnd();
512          iter != e;
513          ++iter) {
514         const CameraInfo* cinfo = iter->get();
515         if ((cinfo->flags & CameraGroup::DO_INTERSECTION_TEST) == 0)
516             continue;
517         const Camera* camera = cinfo->camera.get();
518         if (camera->getGraphicsContext() != ea->getGraphicsContext())
519             continue;
520         const Viewport* viewport = camera->getViewport();
521         double epsilon = 0.5;
522         if (!(x >= viewport->x() - epsilon
523               && x < viewport->x() + viewport->width() -1.0 + epsilon
524               && y >= viewport->y() - epsilon
525               && y < viewport->y() + viewport->height() -1.0 + epsilon))
526             continue;
527         Vec4d start(x, y, 0.0, 1.0);
528         Vec4d end(x, y, 1.0, 1.0);
529         Matrix windowMat = viewport->computeWindowMatrix();
530         Matrix startPtMat = Matrix::inverse(camera->getProjectionMatrix()
531                                             * windowMat);
532         Matrix endPtMat;
533         if (!cinfo->farCamera.valid() || cinfo->farCamera->getNodeMask() == 0)
534             endPtMat = startPtMat;
535         else
536             endPtMat = Matrix::inverse(cinfo->farCamera->getProjectionMatrix()
537                                        * windowMat);
538         start = start * startPtMat;
539         start /= start.w();
540         end = end * endPtMat;
541         end /= end.w();
542         ref_ptr<LineSegmentIntersector> picker
543             = new LineSegmentIntersector(Intersector::VIEW,
544                                          Vec3d(start.x(), start.y(), start.z()),
545                                          Vec3d(end.x(), end.y(), end.z()));
546         osgUtil::IntersectionVisitor iv(picker.get());
547         const_cast<Camera*>(camera)->accept(iv);
548         if (picker->containsIntersections()) {
549             intersections = picker->getIntersections();
550             return true;
551         } else {
552             break;
553         }
554     }
555     intersections.clear();
556     return false;
557 }
558
559 void warpGUIPointer(CameraGroup* cgroup, int x, int y)
560 {
561     using osgViewer::GraphicsWindow;
562     Camera* guiCamera = getGUICamera(cgroup);
563     if (!guiCamera)
564         return;
565     Viewport* vport = guiCamera->getViewport();
566     GraphicsWindow* gw
567         = dynamic_cast<GraphicsWindow*>(guiCamera->getGraphicsContext());
568     if (!gw)
569         return;
570     globals->get_renderer()->getEventHandler()->setMouseWarped();    
571     // Translate the warp request into the viewport of the GUI camera,
572     // send the request to the window, then transform the coordinates
573     // for the Viewer's event queue.
574     double wx = x + vport->x();
575     double wyUp = vport->height() + vport->y() - y;
576     double wy;
577     const GraphicsContext::Traits* traits = gw->getTraits();
578     if (gw->getEventQueue()->getCurrentEventState()->getMouseYOrientation()
579         == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS) {
580         wy = traits->height - wyUp;
581     } else {
582         wy = wyUp;
583     }
584     gw->getEventQueue()->mouseWarped(wx, wy);
585     gw->requestWarpPointer(wx, wy);
586     osgGA::GUIEventAdapter* eventState
587         = cgroup->getViewer()->getEventQueue()->getCurrentEventState();
588     double viewerX
589         = (eventState->getXmin()
590            + ((wx / double(traits->width))
591               * (eventState->getXmax() - eventState->getXmin())));
592     double viewerY
593         = (eventState->getYmin()
594            + ((wyUp / double(traits->height))
595               * (eventState->getYmax() - eventState->getYmin())));
596     cgroup->getViewer()->getEventQueue()->mouseWarped(viewerX, viewerY);
597 }
598 }