1 // Copyright (C) 2008 Tim Moore
2 // Copyright (C) 2011 Mathias Froehlich
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "CameraGroup.hxx"
24 #include <Main/fg_props.hxx>
25 #include <Main/globals.hxx>
26 #include "renderer.hxx"
27 #include "FGEventHandler.hxx"
28 #include "WindowBuilder.hxx"
29 #include "WindowSystemAdapter.hxx"
31 #include <simgear/math/SGRect.hxx>
32 #include <simgear/props/props.hxx>
33 #include <simgear/structure/OSGUtils.hxx>
34 #include <simgear/structure/OSGVersion.hxx>
35 #include <simgear/scene/material/EffectCullVisitor.hxx>
36 #include <simgear/scene/util/RenderConstants.hxx>
43 #include <osg/Geometry>
44 #include <osg/GraphicsContext>
45 #include <osg/io_utils>
49 #include <osg/Program>
53 #include <osg/Viewport>
55 #include <osgUtil/IntersectionVisitor>
57 #include <osgViewer/GraphicsWindow>
58 #include <osgViewer/Renderer>
60 namespace flightgear {
61 const char* MAIN_CAMERA = "main";
62 const char* FAR_CAMERA = "far";
63 const char* GEOMETRY_CAMERA = "geometry";
64 const char* SHADOW_CAMERA = "shadow";
65 const char* LIGHTING_CAMERA = "lighting";
66 const char* DISPLAY_CAMERA = "display";
70 invert(const osg::Matrix& matrix)
72 return osg::Matrix::inverse(matrix);
75 /// Returns the zoom factor of the master camera.
76 /// The reference fov is the historic 55 deg
80 double fov = fgGetDouble("/sim/current-view/field-of-view", 55);
83 return tan(55*0.5*SG_DEGREES_TO_RADIANS)/tan(fov*0.5*SG_DEGREES_TO_RADIANS);
87 preMult(const osg::Vec2d& v, const osg::Matrix& m)
89 osg::Vec3d tmp = m.preMult(osg::Vec3(v, 0));
90 return osg::Vec2d(tmp[0], tmp[1]);
94 relativeProjection(const osg::Matrix& P0, const osg::Matrix& R, const osg::Vec2d ref[2],
95 const osg::Matrix& pP, const osg::Matrix& pR, const osg::Vec2d pRef[2])
97 // Track the way from one projection space to the other:
100 // where P0 is the projection template sensible for the given window size,
101 // T is a translation matrix and S a scale matrix.
102 // We need to determine T and S so that the reference points in the parents
103 // projection space match the two reference points in this cameras projection space.
105 // Starting from the parents camera projection space, we get into this cameras
106 // projection space by the transform matrix:
107 // P*R*inv(pP*pR) = T*S*P0*R*inv(pP*pR)
108 // So, at first compute that matrix without T*S and determine S and T from that
110 // Ok, now osg uses the inverse matrix multiplication order, thus:
111 osg::Matrix PtoPwithoutTS = invert(pR*pP)*R*P0;
112 // Compute the parents reference points in the current projection space
113 // without the yet unknown T and S
114 osg::Vec2d pRefInThis[2] = {
115 preMult(pRef[0], PtoPwithoutTS),
116 preMult(pRef[1], PtoPwithoutTS)
119 // To get the same zoom, rescale to match the parents size
120 double s = (ref[0] - ref[1]).length()/(pRefInThis[0] - pRefInThis[1]).length();
121 osg::Matrix S = osg::Matrix::scale(s, s, 1);
123 // For the translation offset, incorporate the now known scale
124 // and recompute the position ot the first reference point in the
125 // currents projection space without the yet unknown T.
126 pRefInThis[0] = preMult(pRef[0], PtoPwithoutTS*S);
127 // The translation is then the difference of the reference points
128 osg::Matrix T = osg::Matrix::translate(osg::Vec3d(ref[0] - pRefInThis[0], 0));
130 // Compose and return the desired final projection matrix
141 ref_ptr<CameraGroup> CameraGroup::_defaultGroup;
143 CameraGroup::CameraGroup(osgViewer::Viewer* viewer) :
154 // Given a projection matrix, return a new one with the same frustum
155 // sides and new near / far values.
157 void makeNewProjMat(Matrixd& oldProj, double znear,
158 double zfar, Matrixd& projection)
160 projection = oldProj;
161 // Slightly inflate the near & far planes to avoid objects at the
162 // extremes being clipped out.
166 // Clamp the projection matrix z values to the range (near, far)
167 double epsilon = 1.0e-6;
168 if (fabs(projection(0,3)) < epsilon &&
169 fabs(projection(1,3)) < epsilon &&
170 fabs(projection(2,3)) < epsilon) {
171 // Projection is Orthographic
172 epsilon = -1.0/(zfar - znear); // Used as a temp variable
173 projection(2,2) = 2.0*epsilon;
174 projection(3,2) = (zfar + znear)*epsilon;
176 // Projection is Perspective
177 double trans_near = (-znear*projection(2,2) + projection(3,2)) /
178 (-znear*projection(2,3) + projection(3,3));
179 double trans_far = (-zfar*projection(2,2) + projection(3,2)) /
180 (-zfar*projection(2,3) + projection(3,3));
181 double ratio = fabs(2.0/(trans_near - trans_far));
182 double center = -0.5*(trans_near + trans_far);
184 projection.postMult(osg::Matrixd(1.0, 0.0, 0.0, 0.0,
186 0.0, 0.0, ratio, 0.0,
187 0.0, 0.0, center*ratio, 1.0));
194 void CameraInfo::updateCameras()
196 bufferSize->set( osg::Vec2f( width, height ) );
198 for (CameraMap::iterator ii = cameras.begin(); ii != cameras.end(); ++ii ) {
199 float f = ii->second.scaleFactor;
200 if ( f == 0.0f ) continue;
202 if (ii->second.camera->getRenderTargetImplementation() == osg::Camera::FRAME_BUFFER_OBJECT)
203 ii->second.camera->getViewport()->setViewport(0, 0, width*f, height*f);
205 ii->second.camera->getViewport()->setViewport(x*f, y*f, width*f, height*f);
208 for (RenderBufferMap::iterator ii = buffers.begin(); ii != buffers.end(); ++ii ) {
209 float f = ii->second.scaleFactor;
210 if ( f == 0.0f ) continue;
211 osg::Texture2D* texture = ii->second.texture.get();
212 if ( texture->getTextureHeight() != height*f || texture->getTextureWidth() != width*f ) {
213 texture->setTextureSize( width*f, height*f );
214 texture->dirtyTextureObject();
219 void CameraInfo::resized(double w, double h)
221 if (w == 1.0 && h == 1.0)
224 bufferSize->set( osg::Vec2f( w, h ) );
226 for (RenderBufferMap::iterator ii = buffers.begin(); ii != buffers.end(); ++ii) {
227 float s = ii->second.scaleFactor;
228 if ( s == 0.0f ) continue;
229 ii->second.texture->setTextureSize( w * s, h * s );
230 ii->second.texture->dirtyTextureObject();
233 for (CameraMap::iterator ii = cameras.begin(); ii != cameras.end(); ++ii) {
234 RenderStageInfo& rsi = ii->second;
235 if (!rsi.resizable ||
236 rsi.camera->getRenderTargetImplementation() != osg::Camera::FRAME_BUFFER_OBJECT ||
237 rsi.scaleFactor == 0.0f )
240 Viewport* vp = rsi.camera->getViewport();
241 vp->width() = w * rsi.scaleFactor;
242 vp->height() = h * rsi.scaleFactor;
244 osgViewer::Renderer* renderer
245 = static_cast<osgViewer::Renderer*>(rsi.camera->getRenderer());
246 for (int i = 0; i < 2; ++i) {
247 osgUtil::SceneView* sceneView = renderer->getSceneView(i);
248 sceneView->getRenderStage()->setFrameBufferObject(0);
249 sceneView->getRenderStage()->setCameraRequiresSetUp(true);
250 if (sceneView->getRenderStageLeft()) {
251 sceneView->getRenderStageLeft()->setFrameBufferObject(0);
252 sceneView->getRenderStageLeft()->setCameraRequiresSetUp(true);
254 if (sceneView->getRenderStageRight()) {
255 sceneView->getRenderStageRight()->setFrameBufferObject(0);
256 sceneView->getRenderStageRight()->setCameraRequiresSetUp(true);
262 osg::Camera* CameraInfo::getCamera(const std::string& k) const
264 CameraMap::const_iterator ii = cameras.find( k );
265 if (ii == cameras.end())
267 return ii->second.camera.get();
270 osg::Texture2D* CameraInfo::getBuffer(const std::string& k) const
272 RenderBufferMap::const_iterator ii = buffers.find(k);
273 if (ii == buffers.end())
275 return ii->second.texture.get();
278 int CameraInfo::getMainSlaveIndex() const
280 return cameras.find( MAIN_CAMERA )->second.slaveIndex;
283 void CameraInfo::setMatrices(osg::Camera* c)
285 view->set( c->getViewMatrix() );
286 osg::Matrixd vi = c->getInverseViewMatrix();
287 viewInverse->set( vi );
288 projInverse->set( osg::Matrix::inverse( c->getProjectionMatrix() ) );
289 osg::Vec4d pos = osg::Vec4d(0., 0., 0., 1.) * vi;
290 worldPosCart->set( osg::Vec3f( pos.x(), pos.y(), pos.z() ) );
291 SGGeod pos2 = SGGeod::fromCart( SGVec3d( pos.x(), pos.y(), pos.z() ) );
292 worldPosGeod->set( osg::Vec3f( pos2.getLongitudeRad(), pos2.getLatitudeRad(), pos2.getElevationM() ) );
295 void CameraGroup::update(const osg::Vec3d& position,
296 const osg::Quat& orientation)
298 const Matrix masterView(osg::Matrix::translate(-position)
299 * osg::Matrix::rotate(orientation.inverse()));
300 _viewer->getCamera()->setViewMatrix(masterView);
301 const Matrix& masterProj = _viewer->getCamera()->getProjectionMatrix();
302 double masterZoomFactor = zoomFactor();
303 for (CameraList::iterator i = _cameras.begin(); i != _cameras.end(); ++i) {
304 const CameraInfo* info = i->get();
306 Camera* camera = info->getCamera(MAIN_CAMERA);
308 const View::Slave& slave = _viewer->getSlave(info->getMainSlaveIndex());
309 #if SG_OSG_VERSION_LESS_THAN(3,0,0)
310 // refreshes camera viewports (for now)
311 info->updateCameras();
314 if (info->flags & GUI) {
315 viewMatrix = osg::Matrix(); // identifty transform on the GUI camera
316 } else if ((info->flags & VIEW_ABSOLUTE) != 0)
317 viewMatrix = slave._viewOffset;
319 viewMatrix = masterView * slave._viewOffset;
320 camera->setViewMatrix(viewMatrix);
321 Matrix projectionMatrix;
322 if (info->flags & GUI) {
323 projectionMatrix = osg::Matrix::ortho2D(0, info->width, 0, info->height);
324 } else if ((info->flags & PROJECTION_ABSOLUTE) != 0) {
325 if (info->flags & ENABLE_MASTER_ZOOM) {
326 if (info->relativeCameraParent < _cameras.size()) {
327 // template projection matrix and view matrix of the current camera
328 osg::Matrix P0 = slave._projectionOffset;
329 osg::Matrix R = viewMatrix;
331 // The already known projection and view matrix of the parent camera
332 const CameraInfo* parentInfo = _cameras[info->relativeCameraParent].get();
333 RenderStageInfo prsi = parentInfo->cameras.find(MAIN_CAMERA)->second;
334 osg::Matrix pP = prsi.camera->getProjectionMatrix();
335 osg::Matrix pR = prsi.camera->getViewMatrix();
337 // And the projection matrix derived from P0 so that the reference points match
338 projectionMatrix = relativeProjection(P0, R, info->thisReference,
339 pP, pR, info->parentReference);
342 // We want to zoom, so take the original matrix and apply the zoom to it.
343 projectionMatrix = slave._projectionOffset;
344 projectionMatrix.postMultScale(osg::Vec3d(masterZoomFactor, masterZoomFactor, 1));
347 projectionMatrix = slave._projectionOffset;
350 projectionMatrix = masterProj * slave._projectionOffset;
353 CameraMap::const_iterator ii = info->cameras.find(FAR_CAMERA);
354 if (ii == info->cameras.end() || !ii->second.camera.valid()) {
355 camera->setProjectionMatrix(projectionMatrix);
357 Camera* farCamera = ii->second.camera;
358 farCamera->setViewMatrix(viewMatrix);
359 double left, right, bottom, top, parentNear, parentFar;
360 projectionMatrix.getFrustum(left, right, bottom, top,
361 parentNear, parentFar);
362 if ((info->flags & FIXED_NEAR_FAR) == 0) {
366 if (parentFar < _nearField || _nearField == 0.0f) {
367 camera->setProjectionMatrix(projectionMatrix);
368 camera->setCullMask(camera->getCullMask()
369 | simgear::BACKGROUND_BIT);
370 camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
371 farCamera->setNodeMask(0);
373 Matrix nearProj, farProj;
374 makeNewProjMat(projectionMatrix, parentNear, _nearField,
376 makeNewProjMat(projectionMatrix, _nearField, parentFar,
378 camera->setProjectionMatrix(nearProj);
379 camera->setCullMask(camera->getCullMask()
380 & ~simgear::BACKGROUND_BIT);
381 camera->setClearMask(GL_DEPTH_BUFFER_BIT);
382 farCamera->setProjectionMatrix(farProj);
383 farCamera->setNodeMask(camera->getNodeMask());
387 bool viewDone = false;
389 bool projectionDone = false;
390 Matrix projectionMatrix;
391 for ( CameraMap::const_iterator ii = info->cameras.begin(); ii != info->cameras.end(); ++ii ) {
392 if ( ii->first == SHADOW_CAMERA ) {
393 globals->get_renderer()->updateShadowCamera(info, position);
396 if ( ii->second.fullscreen )
399 Camera* camera = ii->second.camera.get();
400 int slaveIndex = ii->second.slaveIndex;
401 const View::Slave& slave = _viewer->getSlave(slaveIndex);
404 if ((info->flags & VIEW_ABSOLUTE) != 0)
405 viewMatrix = slave._viewOffset;
407 viewMatrix = masterView * slave._viewOffset;
411 camera->setViewMatrix( viewMatrix );
413 if ( !projectionDone ) {
414 if ((info->flags & PROJECTION_ABSOLUTE) != 0) {
415 if (info->flags & ENABLE_MASTER_ZOOM) {
416 if (info->relativeCameraParent < _cameras.size()) {
417 // template projection matrix and view matrix of the current camera
418 osg::Matrix P0 = slave._projectionOffset;
419 osg::Matrix R = viewMatrix;
421 // The already known projection and view matrix of the parent camera
422 const CameraInfo* parentInfo = _cameras[info->relativeCameraParent].get();
423 RenderStageInfo prsi = parentInfo->cameras.find(MAIN_CAMERA)->second;
424 osg::Matrix pP = prsi.camera->getProjectionMatrix();
425 osg::Matrix pR = prsi.camera->getViewMatrix();
427 // And the projection matrix derived from P0 so that the reference points match
428 projectionMatrix = relativeProjection(P0, R, info->thisReference,
429 pP, pR, info->parentReference);
432 // We want to zoom, so take the original matrix and apply the zoom to it.
433 projectionMatrix = slave._projectionOffset;
434 projectionMatrix.postMultScale(osg::Vec3d(masterZoomFactor, masterZoomFactor, 1));
437 projectionMatrix = slave._projectionOffset;
440 projectionMatrix = masterProj * slave._projectionOffset;
442 projectionDone = true;
445 camera->setProjectionMatrix(projectionMatrix);
450 globals->get_renderer()->setPlanes( _zNear, _zFar );
453 void CameraGroup::setCameraParameters(float vfov, float aspectRatio)
455 if (vfov != 0.0f && aspectRatio != 0.0f)
457 ->setProjectionMatrixAsPerspective(vfov,
462 double CameraGroup::getMasterAspectRatio() const
464 if (_cameras.empty())
467 const CameraInfo* info = _cameras.front();
469 osg::Camera* camera = info->getCamera(MAIN_CAMERA);
471 camera = info->getCamera( GEOMETRY_CAMERA );
472 const osg::Viewport* viewport = camera->getViewport();
477 return static_cast<double>(viewport->height()) / viewport->width();
484 // A raw value for property nodes that references a class member via
486 template<class C, class T>
487 class RefMember : public SGRawValue<T>
490 RefMember (C *obj, T C::*ptr)
491 : _obj(obj), _ptr(ptr) {}
492 virtual ~RefMember () {}
493 virtual T getValue () const
495 return _obj.get()->*_ptr;
497 virtual bool setValue (T value)
499 _obj.get()->*_ptr = value;
502 virtual SGRawValue<T> * clone () const
504 return new RefMember(_obj.get(), _ptr);
511 template<typename C, typename T>
512 RefMember<C, T> makeRefMember(C *obj, T C::*ptr)
514 return RefMember<C, T>(obj, ptr);
517 template<typename C, typename T>
518 void bindMemberToNode(SGPropertyNode* parent, const char* childName,
519 C* obj, T C::*ptr, T value)
521 SGPropertyNode* valNode = parent->getNode(childName);
522 RefMember<C, T> refMember = makeRefMember(obj, ptr);
524 valNode = parent->getNode(childName, true);
525 valNode->tie(refMember, false);
526 setValue(valNode, value);
528 valNode->tie(refMember, true);
532 void buildViewport(flightgear::CameraInfo* info, SGPropertyNode* viewportNode,
533 const osg::GraphicsContext::Traits *traits)
535 using namespace flightgear;
536 bindMemberToNode(viewportNode, "x", info, &CameraInfo::x, 0.0);
537 bindMemberToNode(viewportNode, "y", info, &CameraInfo::y, 0.0);
538 bindMemberToNode(viewportNode, "width", info, &CameraInfo::width,
539 static_cast<double>(traits->width));
540 bindMemberToNode(viewportNode, "height", info, &CameraInfo::height,
541 static_cast<double>(traits->height));
548 // Mostly copied from osg's osgViewer/View.cpp
550 static osg::Geometry* createPanoramicSphericalDisplayDistortionMesh(
551 const Vec3& origin, const Vec3& widthVector, const Vec3& heightVector,
552 double sphere_radius, double collar_radius,
553 Image* intensityMap = 0, const Matrix& projectorMatrix = Matrix())
555 osg::Vec3d center(0.0,0.0,0.0);
556 osg::Vec3d eye(0.0,0.0,0.0);
558 double distance = sqrt(sphere_radius*sphere_radius - collar_radius*collar_radius);
560 bool texcoord_flip = false;
563 osg::Vec3d projector = eye - osg::Vec3d(0.0,0.0, distance);
565 OSG_INFO<<"createPanoramicSphericalDisplayDistortionMesh : Projector position = "<<projector<<std::endl;
566 OSG_INFO<<"createPanoramicSphericalDisplayDistortionMesh : distance = "<<distance<<std::endl;
568 // create the quad to visualize.
569 osg::Geometry* geometry = new osg::Geometry();
571 geometry->setSupportsDisplayList(false);
573 osg::Vec3 xAxis(widthVector);
574 float width = widthVector.length();
577 osg::Vec3 yAxis(heightVector);
578 float height = heightVector.length();
583 osg::Vec3Array* vertices = new osg::Vec3Array;
584 osg::Vec2Array* texcoords0 = new osg::Vec2Array;
585 osg::Vec2Array* texcoords1 = intensityMap==0 ? new osg::Vec2Array : 0;
586 osg::Vec4Array* colors = new osg::Vec4Array;
589 osg::Vec3 bottom = origin;
590 osg::Vec3 dx = xAxis*(width/((float)(noSteps-2)));
591 osg::Vec3 dy = yAxis*(height/((float)(noSteps-1)));
593 osg::Vec3 top = origin + yAxis*height;
595 osg::Vec3 screenCenter = origin + widthVector*0.5f + heightVector*0.5f;
596 float screenRadius = heightVector.length() * 0.5f;
598 geometry->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
600 for(int i=0;i<noSteps;++i)
602 //osg::Vec3 cursor = bottom+dy*(float)i;
603 for(int j=0;j<noSteps;++j)
605 osg::Vec2 texcoord(double(i)/double(noSteps-1), double(j)/double(noSteps-1));
606 double theta = texcoord.x() * 2.0 * osg::PI;
607 double phi = (1.0-texcoord.y()) * osg::PI;
609 if (texcoord_flip) texcoord.y() = 1.0f - texcoord.y();
611 osg::Vec3 pos(sin(phi)*sin(theta), sin(phi)*cos(theta), cos(phi));
612 pos = pos*projectorMatrix;
614 double alpha = atan2(pos.x(), pos.y());
615 if (alpha<0.0) alpha += 2.0*osg::PI;
617 double beta = atan2(sqrt(pos.x()*pos.x() + pos.y()*pos.y()), pos.z());
618 if (beta<0.0) beta += 2.0*osg::PI;
620 double gamma = atan2(sqrt(double(pos.x()*pos.x() + pos.y()*pos.y())), double(pos.z()+distance));
621 if (gamma<0.0) gamma += 2.0*osg::PI;
624 osg::Vec3 v = screenCenter + osg::Vec3(sin(alpha)*gamma*2.0/osg::PI, -cos(alpha)*gamma*2.0/osg::PI, 0.0f)*screenRadius;
627 vertices->push_back(osg::Vec3(v.x(), top.y()-(v.y()-origin.y()),v.z()));
629 vertices->push_back(v);
631 texcoords0->push_back( texcoord );
633 osg::Vec2 texcoord1(alpha/(2.0*osg::PI), 1.0f - beta/osg::PI);
636 colors->push_back(intensityMap->getColor(texcoord1));
640 colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
641 if (texcoords1) texcoords1->push_back( texcoord1 );
649 // pass the created vertex array to the points geometry object.
650 geometry->setVertexArray(vertices);
652 geometry->setColorArray(colors);
653 geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
655 geometry->setTexCoordArray(0,texcoords0);
656 if (texcoords1) geometry->setTexCoordArray(1,texcoords1);
658 osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES);
659 geometry->addPrimitiveSet(elements);
661 for(int i=0;i<noSteps-1;++i)
663 for(int j=0;j<noSteps-1;++j)
665 int i1 = j+(i+1)*noSteps;
666 int i2 = j+(i)*noSteps;
667 int i3 = j+1+(i)*noSteps;
668 int i4 = j+1+(i+1)*noSteps;
670 osg::Vec3& v1 = (*vertices)[i1];
671 osg::Vec3& v2 = (*vertices)[i2];
672 osg::Vec3& v3 = (*vertices)[i3];
673 osg::Vec3& v4 = (*vertices)[i4];
675 if ((v1-screenCenter).length()>screenRadius) continue;
676 if ((v2-screenCenter).length()>screenRadius) continue;
677 if ((v3-screenCenter).length()>screenRadius) continue;
678 if ((v4-screenCenter).length()>screenRadius) continue;
680 elements->push_back(i1);
681 elements->push_back(i2);
682 elements->push_back(i3);
684 elements->push_back(i1);
685 elements->push_back(i3);
686 elements->push_back(i4);
693 void CameraGroup::buildDistortionCamera(const SGPropertyNode* psNode,
696 const SGPropertyNode* texNode = psNode->getNode("texture");
701 string texName = texNode->getStringValue();
702 TextureMap::iterator itr = _textureTargets.find(texName);
703 if (itr == _textureTargets.end()) {
707 Viewport* viewport = camera->getViewport();
708 float width = viewport->width();
709 float height = viewport->height();
710 TextureRectangle* texRect = itr->second.get();
711 double radius = psNode->getDoubleValue("radius", 1.0);
712 double collar = psNode->getDoubleValue("collar", 0.45);
713 Geode* geode = new Geode();
714 geode->addDrawable(createPanoramicSphericalDisplayDistortionMesh(
715 Vec3(0.0f,0.0f,0.0f), Vec3(width,0.0f,0.0f),
716 Vec3(0.0f,height,0.0f), radius, collar));
718 // new we need to add the texture to the mesh, we do so by creating a
719 // StateSet to contain the Texture StateAttribute.
720 StateSet* stateset = geode->getOrCreateStateSet();
721 stateset->setTextureAttributeAndModes(0, texRect, StateAttribute::ON);
722 stateset->setMode(GL_LIGHTING, StateAttribute::OFF);
724 TexMat* texmat = new TexMat;
725 texmat->setScaleByTextureRectangleSize(true);
726 stateset->setTextureAttributeAndModes(0, texmat, osg::StateAttribute::ON);
728 if (!applyIntensityMapAsColours && intensityMap)
730 stateset->setTextureAttributeAndModes(1, new osg::Texture2D(intensityMap), osg::StateAttribute::ON);
733 // add subgraph to render
734 camera->addChild(geode);
735 camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
736 camera->setClearColor(osg::Vec4(0.0, 0.0, 0.0, 1.0));
737 camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
738 camera->setCullingMode(osg::CullSettings::NO_CULLING);
739 camera->setName("DistortionCorrectionCamera");
742 CameraInfo* CameraGroup::buildCamera(SGPropertyNode* cameraNode)
744 WindowBuilder *wBuild = WindowBuilder::getWindowBuilder();
745 const SGPropertyNode* windowNode = cameraNode->getNode("window");
746 GraphicsWindow* window = 0;
747 int cameraFlags = DO_INTERSECTION_TEST;
749 // New style window declaration / definition
750 window = wBuild->buildWindow(windowNode);
752 // Old style: suck window params out of camera block
753 window = wBuild->buildWindow(cameraNode);
758 Camera* camera = new Camera;
759 camera->setAllowEventFocus(false);
760 camera->setGraphicsContext(window->gc.get());
761 camera->setViewport(new Viewport);
762 camera->setCullingMode(CullSettings::SMALL_FEATURE_CULLING
763 | CullSettings::VIEW_FRUSTUM_CULLING);
764 camera->setInheritanceMask(CullSettings::ALL_VARIABLES
765 & ~(CullSettings::CULL_MASK
766 | CullSettings::CULLING_MODE
767 | CullSettings::CLEAR_MASK
771 const SGPropertyNode* viewNode = cameraNode->getNode("view");
773 double heading = viewNode->getDoubleValue("heading-deg", 0.0);
774 double pitch = viewNode->getDoubleValue("pitch-deg", 0.0);
775 double roll = viewNode->getDoubleValue("roll-deg", 0.0);
776 double x = viewNode->getDoubleValue("x", 0.0);
777 double y = viewNode->getDoubleValue("y", 0.0);
778 double z = viewNode->getDoubleValue("z", 0.0);
779 // Build a view matrix, which is the inverse of a model
780 // orientation matrix.
781 vOff = (Matrix::translate(-x, -y, -z)
782 * Matrix::rotate(-DegreesToRadians(heading),
783 Vec3d(0.0, 1.0, 0.0),
784 -DegreesToRadians(pitch),
785 Vec3d(1.0, 0.0, 0.0),
786 -DegreesToRadians(roll),
787 Vec3d(0.0, 0.0, 1.0)));
788 if (viewNode->getBoolValue("absolute", false))
789 cameraFlags |= VIEW_ABSOLUTE;
791 // Old heading parameter, works in the opposite direction
792 double heading = cameraNode->getDoubleValue("heading-deg", 0.0);
793 vOff.makeRotate(DegreesToRadians(heading), osg::Vec3(0, 1, 0));
795 // Configuring the physical dimensions of a monitor
796 SGPropertyNode* viewportNode = cameraNode->getNode("viewport", true);
797 double physicalWidth = viewportNode->getDoubleValue("width", 1024);
798 double physicalHeight = viewportNode->getDoubleValue("height", 768);
799 double bezelHeightTop = 0;
800 double bezelHeightBottom = 0;
801 double bezelWidthLeft = 0;
802 double bezelWidthRight = 0;
803 const SGPropertyNode* physicalDimensionsNode = 0;
804 if ((physicalDimensionsNode = cameraNode->getNode("physical-dimensions")) != 0) {
805 physicalWidth = physicalDimensionsNode->getDoubleValue("width", physicalWidth);
806 physicalHeight = physicalDimensionsNode->getDoubleValue("height", physicalHeight);
807 const SGPropertyNode* bezelNode = 0;
808 if ((bezelNode = physicalDimensionsNode->getNode("bezel")) != 0) {
809 bezelHeightTop = bezelNode->getDoubleValue("top", bezelHeightTop);
810 bezelHeightBottom = bezelNode->getDoubleValue("bottom", bezelHeightBottom);
811 bezelWidthLeft = bezelNode->getDoubleValue("left", bezelWidthLeft);
812 bezelWidthRight = bezelNode->getDoubleValue("right", bezelWidthRight);
816 unsigned parentCameraIndex = ~0u;
817 osg::Vec2d parentReference[2];
818 osg::Vec2d thisReference[2];
819 SGPropertyNode* projectionNode = 0;
820 if ((projectionNode = cameraNode->getNode("perspective")) != 0) {
821 double fovy = projectionNode->getDoubleValue("fovy-deg", 55.0);
822 double aspectRatio = projectionNode->getDoubleValue("aspect-ratio",
824 double zNear = projectionNode->getDoubleValue("near", 0.0);
825 double zFar = projectionNode->getDoubleValue("far", zNear + 20000);
826 double offsetX = projectionNode->getDoubleValue("offset-x", 0.0);
827 double offsetY = projectionNode->getDoubleValue("offset-y", 0.0);
828 double tan_fovy = tan(DegreesToRadians(fovy*0.5));
829 double right = tan_fovy * aspectRatio * zNear + offsetX;
830 double left = -tan_fovy * aspectRatio * zNear + offsetX;
831 double top = tan_fovy * zNear + offsetY;
832 double bottom = -tan_fovy * zNear + offsetY;
833 pOff.makeFrustum(left, right, bottom, top, zNear, zFar);
834 cameraFlags |= PROJECTION_ABSOLUTE;
835 if (projectionNode->getBoolValue("fixed-near-far", true))
836 cameraFlags |= FIXED_NEAR_FAR;
837 } else if ((projectionNode = cameraNode->getNode("frustum")) != 0
838 || (projectionNode = cameraNode->getNode("ortho")) != 0) {
839 double top = projectionNode->getDoubleValue("top", 0.0);
840 double bottom = projectionNode->getDoubleValue("bottom", 0.0);
841 double left = projectionNode->getDoubleValue("left", 0.0);
842 double right = projectionNode->getDoubleValue("right", 0.0);
843 double zNear = projectionNode->getDoubleValue("near", 0.0);
844 double zFar = projectionNode->getDoubleValue("far", zNear + 20000);
845 if (cameraNode->getNode("frustum")) {
846 pOff.makeFrustum(left, right, bottom, top, zNear, zFar);
847 cameraFlags |= PROJECTION_ABSOLUTE;
849 pOff.makeOrtho(left, right, bottom, top, zNear, zFar);
850 cameraFlags |= (PROJECTION_ABSOLUTE | ORTHO);
852 if (projectionNode->getBoolValue("fixed-near-far", true))
853 cameraFlags |= FIXED_NEAR_FAR;
854 } else if ((projectionNode = cameraNode->getNode("master-perspective")) != 0) {
855 double zNear = projectionNode->getDoubleValue("eye-distance", 0.4*physicalWidth);
856 double xoff = projectionNode->getDoubleValue("x-offset", 0);
857 double yoff = projectionNode->getDoubleValue("y-offset", 0);
858 double left = -0.5*physicalWidth - xoff;
859 double right = 0.5*physicalWidth - xoff;
860 double bottom = -0.5*physicalHeight - yoff;
861 double top = 0.5*physicalHeight - yoff;
862 pOff.makeFrustum(left, right, bottom, top, zNear, zNear*1000);
863 cameraFlags |= PROJECTION_ABSOLUTE | ENABLE_MASTER_ZOOM;
864 } else if ((projectionNode = cameraNode->getNode("right-of-perspective"))
865 || (projectionNode = cameraNode->getNode("left-of-perspective"))
866 || (projectionNode = cameraNode->getNode("above-perspective"))
867 || (projectionNode = cameraNode->getNode("below-perspective"))
868 || (projectionNode = cameraNode->getNode("reference-points-perspective"))) {
869 std::string name = projectionNode->getStringValue("parent-camera");
870 for (unsigned i = 0; i < _cameras.size(); ++i) {
871 if (_cameras[i]->name != name)
873 parentCameraIndex = i;
875 if (_cameras.size() <= parentCameraIndex) {
876 SG_LOG(SG_VIEW, SG_ALERT, "CameraGroup::buildCamera: "
877 "failed to find parent camera for relative camera!");
880 const CameraInfo* parentInfo = _cameras[parentCameraIndex].get();
881 if (projectionNode->getNameString() == "right-of-perspective") {
882 double tmp = (parentInfo->physicalWidth + 2*parentInfo->bezelWidthRight)/parentInfo->physicalWidth;
883 parentReference[0] = osg::Vec2d(tmp, -1);
884 parentReference[1] = osg::Vec2d(tmp, 1);
885 tmp = (physicalWidth + 2*bezelWidthLeft)/physicalWidth;
886 thisReference[0] = osg::Vec2d(-tmp, -1);
887 thisReference[1] = osg::Vec2d(-tmp, 1);
888 } else if (projectionNode->getNameString() == "left-of-perspective") {
889 double tmp = (parentInfo->physicalWidth + 2*parentInfo->bezelWidthLeft)/parentInfo->physicalWidth;
890 parentReference[0] = osg::Vec2d(-tmp, -1);
891 parentReference[1] = osg::Vec2d(-tmp, 1);
892 tmp = (physicalWidth + 2*bezelWidthRight)/physicalWidth;
893 thisReference[0] = osg::Vec2d(tmp, -1);
894 thisReference[1] = osg::Vec2d(tmp, 1);
895 } else if (projectionNode->getNameString() == "above-perspective") {
896 double tmp = (parentInfo->physicalHeight + 2*parentInfo->bezelHeightTop)/parentInfo->physicalHeight;
897 parentReference[0] = osg::Vec2d(-1, tmp);
898 parentReference[1] = osg::Vec2d(1, tmp);
899 tmp = (physicalHeight + 2*bezelHeightBottom)/physicalHeight;
900 thisReference[0] = osg::Vec2d(-1, -tmp);
901 thisReference[1] = osg::Vec2d(1, -tmp);
902 } else if (projectionNode->getNameString() == "below-perspective") {
903 double tmp = (parentInfo->physicalHeight + 2*parentInfo->bezelHeightBottom)/parentInfo->physicalHeight;
904 parentReference[0] = osg::Vec2d(-1, -tmp);
905 parentReference[1] = osg::Vec2d(1, -tmp);
906 tmp = (physicalHeight + 2*bezelHeightTop)/physicalHeight;
907 thisReference[0] = osg::Vec2d(-1, tmp);
908 thisReference[1] = osg::Vec2d(1, tmp);
909 } else if (projectionNode->getNameString() == "reference-points-perspective") {
910 SGPropertyNode* parentNode = projectionNode->getNode("parent", true);
911 SGPropertyNode* thisNode = projectionNode->getNode("this", true);
912 SGPropertyNode* pointNode;
914 pointNode = parentNode->getNode("point", 0, true);
915 parentReference[0][0] = pointNode->getDoubleValue("x", 0)*2/parentInfo->physicalWidth;
916 parentReference[0][1] = pointNode->getDoubleValue("y", 0)*2/parentInfo->physicalHeight;
917 pointNode = parentNode->getNode("point", 1, true);
918 parentReference[1][0] = pointNode->getDoubleValue("x", 0)*2/parentInfo->physicalWidth;
919 parentReference[1][1] = pointNode->getDoubleValue("y", 0)*2/parentInfo->physicalHeight;
921 pointNode = thisNode->getNode("point", 0, true);
922 thisReference[0][0] = pointNode->getDoubleValue("x", 0)*2/physicalWidth;
923 thisReference[0][1] = pointNode->getDoubleValue("y", 0)*2/physicalHeight;
924 pointNode = thisNode->getNode("point", 1, true);
925 thisReference[1][0] = pointNode->getDoubleValue("x", 0)*2/physicalWidth;
926 thisReference[1][1] = pointNode->getDoubleValue("y", 0)*2/physicalHeight;
929 pOff = osg::Matrix::perspective(45, physicalWidth/physicalHeight, 1, 20000);
930 cameraFlags |= PROJECTION_ABSOLUTE | ENABLE_MASTER_ZOOM;
932 // old style shear parameters
933 double shearx = cameraNode->getDoubleValue("shear-x", 0);
934 double sheary = cameraNode->getDoubleValue("shear-y", 0);
935 pOff.makeTranslate(-shearx, -sheary, 0);
937 const SGPropertyNode* textureNode = cameraNode->getNode("texture");
939 string texName = textureNode->getStringValue("name");
940 int tex_width = textureNode->getIntValue("width");
941 int tex_height = textureNode->getIntValue("height");
942 TextureRectangle* texture = new TextureRectangle;
944 texture->setTextureSize(tex_width, tex_height);
945 texture->setInternalFormat(GL_RGB);
946 texture->setFilter(Texture::MIN_FILTER, Texture::LINEAR);
947 texture->setFilter(Texture::MAG_FILTER, Texture::LINEAR);
948 texture->setWrap(Texture::WRAP_S, Texture::CLAMP_TO_EDGE);
949 texture->setWrap(Texture::WRAP_T, Texture::CLAMP_TO_EDGE);
950 camera->setDrawBuffer(GL_FRONT);
951 camera->setReadBuffer(GL_FRONT);
952 camera->setRenderTargetImplementation(Camera::FRAME_BUFFER_OBJECT);
953 camera->attach(Camera::COLOR_BUFFER, texture);
954 _textureTargets[texName] = texture;
956 camera->setDrawBuffer(GL_BACK);
957 camera->setReadBuffer(GL_BACK);
959 const SGPropertyNode* psNode = cameraNode->getNode("panoramic-spherical");
960 bool useMasterSceneGraph = !psNode;
961 CameraInfo* info = globals->get_renderer()->buildRenderingPipeline(this, cameraFlags, camera, vOff, pOff,
962 window->gc.get(), useMasterSceneGraph);
963 info->name = cameraNode->getStringValue("name");
964 info->physicalWidth = physicalWidth;
965 info->physicalHeight = physicalHeight;
966 info->bezelHeightTop = bezelHeightTop;
967 info->bezelHeightBottom = bezelHeightBottom;
968 info->bezelWidthLeft = bezelWidthLeft;
969 info->bezelWidthRight = bezelWidthRight;
970 info->relativeCameraParent = parentCameraIndex;
971 info->parentReference[0] = parentReference[0];
972 info->parentReference[1] = parentReference[1];
973 info->thisReference[0] = thisReference[0];
974 info->thisReference[1] = thisReference[1];
975 // If a viewport isn't set on the camera, then it's hard to dig it
976 // out of the SceneView objects in the viewer, and the coordinates
977 // of mouse events are somewhat bizzare.
978 buildViewport(info, viewportNode, window->gc->getTraits());
979 info->updateCameras();
980 // Distortion camera needs the viewport which is created by addCamera().
982 info->flags = info->flags | VIEW_ABSOLUTE;
983 buildDistortionCamera(psNode, camera);
988 CameraInfo* CameraGroup::buildGUICamera(SGPropertyNode* cameraNode,
989 GraphicsWindow* window)
991 WindowBuilder *wBuild = WindowBuilder::getWindowBuilder();
992 const SGPropertyNode* windowNode = (cameraNode
993 ? cameraNode->getNode("window")
995 if (!window && windowNode) {
996 // New style window declaration / definition
997 window = wBuild->buildWindow(windowNode);
1000 if (!window) { // buildWindow can fail
1001 SG_LOG(SG_VIEW, SG_WARN, "CameraGroup::buildGUICamera: failed to build a window");
1005 Camera* camera = new Camera;
1006 camera->setName( "GUICamera" );
1007 camera->setAllowEventFocus(false);
1008 camera->setGraphicsContext(window->gc.get());
1009 camera->setViewport(new Viewport);
1010 camera->setClearMask(0);
1011 camera->setInheritanceMask(CullSettings::ALL_VARIABLES
1012 & ~(CullSettings::COMPUTE_NEAR_FAR_MODE
1013 | CullSettings::CULLING_MODE
1014 | CullSettings::CLEAR_MASK
1016 camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
1017 camera->setCullingMode(osg::CullSettings::NO_CULLING);
1018 camera->setProjectionResizePolicy(Camera::FIXED);
1019 camera->setReferenceFrame(Transform::ABSOLUTE_RF);
1020 const int cameraFlags = GUI | DO_INTERSECTION_TEST;
1022 CameraInfo* result = new CameraInfo(cameraFlags);
1023 // The camera group will always update the camera
1024 camera->setReferenceFrame(Transform::ABSOLUTE_RF);
1026 getViewer()->addSlave(camera, Matrixd::identity(), Matrixd::identity(), false);
1027 //installCullVisitor(camera);
1028 int slaveIndex = getViewer()->getNumSlaves() - 1;
1029 result->addCamera( MAIN_CAMERA, camera, slaveIndex );
1030 camera->setRenderOrder(Camera::POST_RENDER, slaveIndex);
1033 // XXX Camera needs to be drawn last; eventually the render order
1034 // should be assigned by a camera manager.
1035 camera->setRenderOrder(osg::Camera::POST_RENDER, 10000);
1036 SGPropertyNode* viewportNode = cameraNode->getNode("viewport", true);
1037 buildViewport(result, viewportNode, window->gc->getTraits());
1039 // Disable statistics for the GUI camera.
1040 camera->setStats(0);
1041 result->updateCameras();
1045 CameraGroup* CameraGroup::buildCameraGroup(osgViewer::Viewer* viewer,
1046 SGPropertyNode* gnode)
1048 CameraGroup* cgroup = new CameraGroup(viewer);
1049 for (int i = 0; i < gnode->nChildren(); ++i) {
1050 SGPropertyNode* pNode = gnode->getChild(i);
1051 const char* name = pNode->getName();
1052 if (!strcmp(name, "camera")) {
1053 cgroup->buildCamera(pNode);
1054 } else if (!strcmp(name, "window")) {
1055 WindowBuilder::getWindowBuilder()->buildWindow(pNode);
1056 } else if (!strcmp(name, "gui")) {
1057 cgroup->buildGUICamera(pNode);
1060 bindMemberToNode(gnode, "znear", cgroup, &CameraGroup::_zNear, .1f);
1061 bindMemberToNode(gnode, "zfar", cgroup, &CameraGroup::_zFar, 120000.0f);
1062 bindMemberToNode(gnode, "near-field", cgroup, &CameraGroup::_nearField,
1067 void CameraGroup::setCameraCullMasks(Node::NodeMask nm)
1069 for (CameraIterator i = camerasBegin(), e = camerasEnd(); i != e; ++i) {
1070 CameraInfo* info = i->get();
1071 if (info->flags & GUI)
1073 osg::ref_ptr<osg::Camera> farCamera = info->getCamera(FAR_CAMERA);
1074 osg::Camera* camera = info->getCamera( MAIN_CAMERA );
1076 if (farCamera.valid() && farCamera->getNodeMask() != 0) {
1077 camera->setCullMask(nm & ~simgear::BACKGROUND_BIT);
1078 camera->setCullMaskLeft(nm & ~simgear::BACKGROUND_BIT);
1079 camera->setCullMaskRight(nm & ~simgear::BACKGROUND_BIT);
1080 farCamera->setCullMask(nm);
1081 farCamera->setCullMaskLeft(nm);
1082 farCamera->setCullMaskRight(nm);
1084 camera->setCullMask(nm);
1085 camera->setCullMaskLeft(nm);
1086 camera->setCullMaskRight(nm);
1089 camera = info->getCamera( GEOMETRY_CAMERA );
1090 if (camera == 0) continue;
1091 camera->setCullMask( nm & ~simgear::MODELLIGHT_BIT );
1093 camera = info->getCamera( LIGHTING_CAMERA );
1094 if (camera == 0) continue;
1095 osg::Switch* sw = camera->getChild(0)->asSwitch();
1096 for (unsigned int i = 0; i < sw->getNumChildren(); ++i) {
1097 osg::Camera* lc = dynamic_cast<osg::Camera*>(sw->getChild(i));
1098 if (lc == 0) continue;
1099 string name = lc->getName();
1100 if (name == "LightCamera") {
1101 lc->setCullMask( (nm & simgear::LIGHTS_BITS) | (lc->getCullMask() & ~simgear::LIGHTS_BITS) );
1108 void CameraGroup::resized()
1110 for (CameraIterator i = camerasBegin(), e = camerasEnd(); i != e; ++i) {
1111 CameraInfo *info = i->get();
1112 Camera* camera = info->getCamera( MAIN_CAMERA );
1114 camera = info->getCamera( DISPLAY_CAMERA );
1115 const Viewport* viewport = camera->getViewport();
1116 info->x = viewport->x();
1117 info->y = viewport->y();
1118 info->width = viewport->width();
1119 info->height = viewport->height();
1121 info->resized( info->width, info->height );
1125 const CameraInfo* CameraGroup::getGUICamera() const
1127 ConstCameraIterator result
1128 = std::find_if(camerasBegin(), camerasEnd(),
1129 FlagTester<CameraInfo>(GUI));
1130 if (result == camerasEnd()) {
1137 Camera* getGUICamera(CameraGroup* cgroup)
1139 const CameraInfo* info = cgroup->getGUICamera();
1144 return info->getCamera(MAIN_CAMERA);
1148 static bool computeCameraIntersection(const CameraInfo* cinfo, const osg::Vec2d& windowPos,
1149 osgUtil::LineSegmentIntersector::Intersections& intersections)
1151 using osgUtil::Intersector;
1152 using osgUtil::LineSegmentIntersector;
1154 if (!(cinfo->flags & CameraGroup::DO_INTERSECTION_TEST))
1157 const Camera* camera = cinfo->getCamera(MAIN_CAMERA);
1159 camera = cinfo->getCamera( GEOMETRY_CAMERA );
1161 // if (camera->getGraphicsContext() != ea->getGraphicsContext())
1164 const Viewport* viewport = camera->getViewport();
1165 SGRect<double> viewportRect(viewport->x(), viewport->y(),
1166 viewport->x() + viewport->width() - 1.0,
1167 viewport->y() + viewport->height()- 1.0);
1169 double epsilon = 0.5;
1170 if (!viewportRect.contains(windowPos.x(), windowPos.y(), epsilon))
1173 Vec4d start(windowPos.x(), windowPos.y(), 0.0, 1.0);
1174 Vec4d end(windowPos.x(), windowPos.y(), 1.0, 1.0);
1175 Matrix windowMat = viewport->computeWindowMatrix();
1176 Matrix startPtMat = Matrix::inverse(camera->getProjectionMatrix()
1179 const Camera* farCamera = cinfo->getCamera( FAR_CAMERA );
1180 if (!farCamera || farCamera->getNodeMask() == 0)
1181 endPtMat = startPtMat;
1183 endPtMat = Matrix::inverse(farCamera->getProjectionMatrix()
1185 start = start * startPtMat;
1187 end = end * endPtMat;
1189 ref_ptr<LineSegmentIntersector> picker
1190 = new LineSegmentIntersector(Intersector::VIEW,
1191 Vec3d(start.x(), start.y(), start.z()),
1192 Vec3d(end.x(), end.y(), end.z()));
1193 osgUtil::IntersectionVisitor iv(picker.get());
1194 iv.setTraversalMask( ~simgear::MODELLIGHT_BIT );
1195 const_cast<Camera*>(camera)->accept(iv);
1196 if (picker->containsIntersections()) {
1197 intersections = picker->getIntersections();
1204 bool computeIntersections(const CameraGroup* cgroup,
1205 const osg::Vec2d& windowPos,
1206 osgUtil::LineSegmentIntersector::Intersections& intersections)
1208 // test the GUI first
1209 const CameraInfo* guiCamera = cgroup->getGUICamera();
1210 if (guiCamera && computeCameraIntersection(guiCamera, windowPos, intersections))
1213 // Find camera that contains event
1214 for (CameraGroup::ConstCameraIterator iter = cgroup->camerasBegin(),
1215 e = cgroup->camerasEnd();
1218 const CameraInfo* cinfo = iter->get();
1219 if (cinfo == guiCamera)
1222 if (computeCameraIntersection(cinfo, windowPos, intersections))
1226 intersections.clear();
1230 void warpGUIPointer(CameraGroup* cgroup, int x, int y)
1232 using osgViewer::GraphicsWindow;
1233 Camera* guiCamera = getGUICamera(cgroup);
1236 Viewport* vport = guiCamera->getViewport();
1238 = dynamic_cast<GraphicsWindow*>(guiCamera->getGraphicsContext());
1241 globals->get_renderer()->getEventHandler()->setMouseWarped();
1242 // Translate the warp request into the viewport of the GUI camera,
1243 // send the request to the window, then transform the coordinates
1244 // for the Viewer's event queue.
1245 double wx = x + vport->x();
1246 double wyUp = vport->height() + vport->y() - y;
1248 const GraphicsContext::Traits* traits = gw->getTraits();
1249 if (gw->getEventQueue()->getCurrentEventState()->getMouseYOrientation()
1250 == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS) {
1251 wy = traits->height - wyUp;
1255 gw->getEventQueue()->mouseWarped(wx, wy);
1256 gw->requestWarpPointer(wx, wy);
1257 osgGA::GUIEventAdapter* eventState
1258 = cgroup->getViewer()->getEventQueue()->getCurrentEventState();
1260 = (eventState->getXmin()
1261 + ((wx / double(traits->width))
1262 * (eventState->getXmax() - eventState->getXmin())));
1264 = (eventState->getYmin()
1265 + ((wyUp / double(traits->height))
1266 * (eventState->getYmax() - eventState->getYmin())));
1267 cgroup->getViewer()->getEventQueue()->mouseWarped(viewerX, viewerY);