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