]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/CameraGroup.cxx
Improve timing statistics
[flightgear.git] / src / Main / CameraGroup.cxx
index f218466bb4d2729b70792f30b888cd64f407cea3..e80e0c564bbc1cf61b5520055aa9cea426ea96a2 100644 (file)
 // along with this program; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
 #include "CameraGroup.hxx"
 
 #include "globals.hxx"
@@ -22,6 +26,8 @@
 #include "WindowBuilder.hxx"
 #include "WindowSystemAdapter.hxx"
 #include <simgear/props/props.hxx>
+#include <simgear/structure/OSGUtils.hxx>
+#include <simgear/scene/material/EffectCullVisitor.hxx>
 #include <simgear/scene/util/RenderConstants.hxx>
 
 #include <algorithm>
 #include <string>
 
 #include <osg/Camera>
+#include <osg/Geometry>
 #include <osg/GraphicsContext>
+#include <osg/io_utils>
 #include <osg/Math>
 #include <osg/Matrix>
+#include <osg/Notify>
+#include <osg/Program>
 #include <osg/Quat>
+#include <osg/TexMat>
 #include <osg/Vec3d>
 #include <osg/Viewport>
 
 #include <osgUtil/IntersectionVisitor>
 
 #include <osgViewer/GraphicsWindow>
+#include <osgViewer/Renderer>
 
 namespace flightgear
 {
@@ -99,19 +111,27 @@ void makeNewProjMat(Matrixd& oldProj, double znear,
 
 void installCullVisitor(Camera* camera)
 {
-#if 0 // Not yet
     osgViewer::Renderer* renderer
         = static_cast<osgViewer::Renderer*>(camera->getRenderer());
     for (int i = 0; i < 2; ++i) {
         osgUtil::SceneView* sceneView = renderer->getSceneView(i);
         sceneView->setCullVisitor(new simgear::EffectCullVisitor);
     }
-#endif
 }
 }
 
 namespace flightgear
 {
+void updateCameras(const CameraInfo* info)
+{
+    if (info->camera.valid())
+        info->camera->getViewport()->setViewport(info->x, info->y,
+                                                 info->width, info->height);
+    if (info->farCamera.valid())
+        info->farCamera->getViewport()->setViewport(info->x, info->y,
+                                                    info->width, info->height);
+}
+
 CameraInfo* CameraGroup::addCamera(unsigned flags, Camera* camera,
                                    const Matrix& view,
                                    const Matrix& projection,
@@ -123,25 +143,40 @@ CameraInfo* CameraGroup::addCamera(unsigned flags, Camera* camera,
 
     Camera* farCamera = 0;
     if ((flags & (GUI | ORTHO)) == 0) {
-        farCamera = osg::clone(camera);
+        farCamera = new Camera;
+        farCamera->setAllowEventFocus(camera->getAllowEventFocus());
         farCamera->setGraphicsContext(camera->getGraphicsContext());
+        farCamera->setCullingMode(camera->getCullingMode());
+        farCamera->setInheritanceMask(camera->getInheritanceMask());
+        farCamera->setReferenceFrame(Transform::ABSOLUTE_RF);
         // Each camera's viewport is written when the window is
         // resized; if the the viewport isn't copied here, it gets updated
         // twice and ends up with the wrong value.
-        farCamera->setViewport(osg::clone(camera->getViewport()));
-        _viewer->addSlave(farCamera, view, projection, useMasterSceneData);
+        farCamera->setViewport(simgear::clone(camera->getViewport()));
+        farCamera->setDrawBuffer(camera->getDrawBuffer());
+        farCamera->setReadBuffer(camera->getReadBuffer());
+        farCamera->setRenderTargetImplementation(
+            camera->getRenderTargetImplementation());
+        const Camera::BufferAttachmentMap& bufferMap
+            = camera->getBufferAttachmentMap();
+        if (bufferMap.count(Camera::COLOR_BUFFER) != 0) {
+            farCamera->attach(
+                Camera::COLOR_BUFFER,
+                bufferMap.find(Camera::COLOR_BUFFER)->second._texture.get());
+        }
+        _viewer->addSlave(farCamera, projection, view, useMasterSceneData);
         installCullVisitor(farCamera);
         info->farCamera = farCamera;
         info->farSlaveIndex = _viewer->getNumSlaves() - 1;
-        farCamera->setRenderOrder(Camera::NESTED_RENDER, info->farSlaveIndex);
+        farCamera->setRenderOrder(Camera::POST_RENDER, info->farSlaveIndex);
         camera->setCullMask(camera->getCullMask() & ~simgear::BACKGROUND_BIT);
+        camera->setClearMask(GL_DEPTH_BUFFER_BIT);
     }
-    camera->setClearMask(GL_DEPTH_BUFFER_BIT);
-    _viewer->addSlave(camera, view, projection, useMasterSceneData);
+    _viewer->addSlave(camera, projection, view, useMasterSceneData);
     installCullVisitor(camera);
     info->camera = camera;
     info->slaveIndex = _viewer->getNumSlaves() - 1;
-    camera->setRenderOrder(Camera::NESTED_RENDER, info->slaveIndex);
+    camera->setRenderOrder(Camera::POST_RENDER, info->slaveIndex);
     _cameras.push_back(info);
     return info;
 }
@@ -156,9 +191,9 @@ void CameraGroup::update(const osg::Vec3d& position,
     for (CameraList::iterator i = _cameras.begin(); i != _cameras.end(); ++i) {
         const CameraInfo* info = i->get();
         const View::Slave& slave = _viewer->getSlave(info->slaveIndex);
+        // refreshes camera viewports (for now)
+        updateCameras(info);
         Camera* camera = info->camera.get();
-        camera->getViewport()->setViewport(info->x, info->y, info->width,
-                                           info->height);
         Matrix viewMatrix;
         if ((info->flags & VIEW_ABSOLUTE) != 0)
             viewMatrix = slave._viewOffset;
@@ -175,24 +210,26 @@ void CameraGroup::update(const osg::Vec3d& position,
             camera->setProjectionMatrix(projectionMatrix);
         } else {
             Camera* farCamera = info->farCamera.get();
-            farCamera->getViewport()->setViewport(info->x, info->y, info->width,
-                                                  info->height);
             farCamera->setViewMatrix(viewMatrix);
             double left, right, bottom, top, parentNear, parentFar;
             projectionMatrix.getFrustum(left, right, bottom, top,
                                         parentNear, parentFar);
-            if (parentFar < 100.0) {
+            if (parentFar < _nearField || _nearField == 0.0f) {
                 camera->setProjectionMatrix(projectionMatrix);
                 camera->setCullMask(camera->getCullMask()
                                     | simgear::BACKGROUND_BIT);
+                camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
                 farCamera->setNodeMask(0);
             } else {
                 Matrix nearProj, farProj;
-                makeNewProjMat(projectionMatrix, parentNear, 100.0, nearProj);
-                makeNewProjMat(projectionMatrix, 100.0, parentFar, farProj);
+                makeNewProjMat(projectionMatrix, parentNear, _nearField,
+                               nearProj);
+                makeNewProjMat(projectionMatrix, _nearField, parentFar,
+                               farProj);
                 camera->setProjectionMatrix(nearProj);
                 camera->setCullMask(camera->getCullMask()
                                     & ~simgear::BACKGROUND_BIT);
+                camera->setClearMask(GL_DEPTH_BUFFER_BIT);
                 farCamera->setProjectionMatrix(farProj);
                 farCamera->setNodeMask(camera->getNodeMask());
             }
@@ -202,11 +239,11 @@ void CameraGroup::update(const osg::Vec3d& position,
 
 void CameraGroup::setCameraParameters(float vfov, float aspectRatio)
 {
-    const float zNear = .1f;
-    const float zFar = 120000.0f;
-    _viewer->getCamera()->setProjectionMatrixAsPerspective(vfov,
-                                                           1.0f / aspectRatio,
-                                                           zNear, zFar);
+    if (vfov != 0.0f && aspectRatio != 0.0f)
+        _viewer->getCamera()
+            ->setProjectionMatrixAsPerspective(vfov,
+                                               1.0f / aspectRatio,
+                                               _zNear, _zFar);
 }
 }
 
@@ -276,12 +313,204 @@ void buildViewport(flightgear::CameraInfo* info, SGPropertyNode* viewportNode,
 namespace flightgear
 {
 
+// Mostly copied from osg's osgViewer/View.cpp
+
+static osg::Geometry* createParoramicSphericalDisplayDistortionMesh(
+    const Vec3& origin, const Vec3& widthVector, const Vec3& heightVector,
+    double sphere_radius, double collar_radius,
+    Image* intensityMap = 0, const Matrix& projectorMatrix = Matrix())
+{
+    osg::Vec3d center(0.0,0.0,0.0);
+    osg::Vec3d eye(0.0,0.0,0.0);
+
+    double distance = sqrt(sphere_radius*sphere_radius - collar_radius*collar_radius);
+    bool flip = false;
+    bool texcoord_flip = false;
+
+    osg::Vec3d projector = eye - osg::Vec3d(0.0,0.0, distance);
+
+#if 0
+    OSG_INFO<<"createParoramicSphericalDisplayDistortionMesh : Projector position = "<<projector<<std::endl;
+    OSG_INFO<<"createParoramicSphericalDisplayDistortionMesh : distance = "<<distance<<std::endl;
+#endif
+    // create the quad to visualize.
+    osg::Geometry* geometry = new osg::Geometry();
+
+    geometry->setSupportsDisplayList(false);
+
+    osg::Vec3 xAxis(widthVector);
+    float width = widthVector.length();
+    xAxis /= width;
+
+    osg::Vec3 yAxis(heightVector);
+    float height = heightVector.length();
+    yAxis /= height;
+
+    int noSteps = 160;
+
+    osg::Vec3Array* vertices = new osg::Vec3Array;
+    osg::Vec2Array* texcoords0 = new osg::Vec2Array;
+    osg::Vec2Array* texcoords1 = intensityMap==0 ? new osg::Vec2Array : 0;
+    osg::Vec4Array* colors = new osg::Vec4Array;
+
+    osg::Vec3 bottom = origin;
+    osg::Vec3 dx = xAxis*(width/((float)(noSteps-2)));
+    osg::Vec3 dy = yAxis*(height/((float)(noSteps-1)));
+
+    osg::Vec3 top = origin + yAxis*height;
+
+    osg::Vec3 screenCenter = origin + widthVector*0.5f + heightVector*0.5f;
+    float screenRadius = heightVector.length() * 0.5f;
+
+    geometry->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
+
+    for(int i=0;i<noSteps;++i)
+    {
+        osg::Vec3 cursor = bottom+dy*(float)i;
+        for(int j=0;j<noSteps;++j)
+        {
+            osg::Vec2 texcoord(double(i)/double(noSteps-1), double(j)/double(noSteps-1));
+            double theta = texcoord.x() * 2.0 * osg::PI;
+            double phi = (1.0-texcoord.y()) * osg::PI;
+
+            if (texcoord_flip) texcoord.y() = 1.0f - texcoord.y();
+
+            osg::Vec3 pos(sin(phi)*sin(theta), sin(phi)*cos(theta), cos(phi));
+            pos = pos*projectorMatrix;
+
+            double alpha = atan2(pos.x(), pos.y());
+            if (alpha<0.0) alpha += 2.0*osg::PI;
+
+            double beta = atan2(sqrt(pos.x()*pos.x() + pos.y()*pos.y()), pos.z());
+            if (beta<0.0) beta += 2.0*osg::PI;
+
+            double gamma = atan2(sqrt(double(pos.x()*pos.x() + pos.y()*pos.y())), double(pos.z()+distance));
+            if (gamma<0.0) gamma += 2.0*osg::PI;
+
+
+            osg::Vec3 v = screenCenter + osg::Vec3(sin(alpha)*gamma*2.0/osg::PI, -cos(alpha)*gamma*2.0/osg::PI, 0.0f)*screenRadius;
+
+            if (flip)
+                vertices->push_back(osg::Vec3(v.x(), top.y()-(v.y()-origin.y()),v.z()));
+            else
+                vertices->push_back(v);
+
+            texcoords0->push_back( texcoord );
+
+            osg::Vec2 texcoord1(alpha/(2.0*osg::PI), 1.0f - beta/osg::PI);
+            if (intensityMap)
+            {
+                colors->push_back(intensityMap->getColor(texcoord1));
+            }
+            else
+            {
+                colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
+                if (texcoords1) texcoords1->push_back( texcoord1 );
+            }
+
+
+        }
+    }
+
+
+    // pass the created vertex array to the points geometry object.
+    geometry->setVertexArray(vertices);
+
+    geometry->setColorArray(colors);
+    geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
+
+    geometry->setTexCoordArray(0,texcoords0);
+    if (texcoords1) geometry->setTexCoordArray(1,texcoords1);
+
+    osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES);
+    geometry->addPrimitiveSet(elements);
+
+    for(int i=0;i<noSteps-1;++i)
+    {
+        for(int j=0;j<noSteps-1;++j)
+        {
+            int i1 = j+(i+1)*noSteps;
+            int i2 = j+(i)*noSteps;
+            int i3 = j+1+(i)*noSteps;
+            int i4 = j+1+(i+1)*noSteps;
+
+            osg::Vec3& v1 = (*vertices)[i1];
+            osg::Vec3& v2 = (*vertices)[i2];
+            osg::Vec3& v3 = (*vertices)[i3];
+            osg::Vec3& v4 = (*vertices)[i4];
+
+            if ((v1-screenCenter).length()>screenRadius) continue;
+            if ((v2-screenCenter).length()>screenRadius) continue;
+            if ((v3-screenCenter).length()>screenRadius) continue;
+            if ((v4-screenCenter).length()>screenRadius) continue;
+
+            elements->push_back(i1);
+            elements->push_back(i2);
+            elements->push_back(i3);
+
+            elements->push_back(i1);
+            elements->push_back(i3);
+            elements->push_back(i4);
+        }
+    }
+
+    return geometry;
+}
+
+void CameraGroup::buildDistortionCamera(const SGPropertyNode* psNode,
+                                        Camera* camera)
+{
+    const SGPropertyNode* texNode = psNode->getNode("texture");
+    if (!texNode) {
+        // error
+        return;
+    }
+    string texName = texNode->getStringValue();
+    TextureMap::iterator itr = _textureTargets.find(texName);
+    if (itr == _textureTargets.end()) {
+        // error
+        return;
+    }
+    Viewport* viewport = camera->getViewport();
+    float width = viewport->width();
+    float height = viewport->height();
+    TextureRectangle* texRect = itr->second.get();
+    double radius = psNode->getDoubleValue("radius", 1.0);
+    double collar = psNode->getDoubleValue("collar", 0.45);
+    Geode* geode = new Geode();
+    geode->addDrawable(createParoramicSphericalDisplayDistortionMesh(
+                           Vec3(0.0f,0.0f,0.0f), Vec3(width,0.0f,0.0f),
+                           Vec3(0.0f,height,0.0f), radius, collar));
+
+    // new we need to add the texture to the mesh, we do so by creating a
+    // StateSet to contain the Texture StateAttribute.
+    StateSet* stateset = geode->getOrCreateStateSet();
+    stateset->setTextureAttributeAndModes(0, texRect, StateAttribute::ON);
+    stateset->setMode(GL_LIGHTING, StateAttribute::OFF);
+
+    TexMat* texmat = new TexMat;
+    texmat->setScaleByTextureRectangleSize(true);
+    stateset->setTextureAttributeAndModes(0, texmat, osg::StateAttribute::ON);
+#if 0
+    if (!applyIntensityMapAsColours && intensityMap)
+    {
+        stateset->setTextureAttributeAndModes(1, new osg::Texture2D(intensityMap), osg::StateAttribute::ON);
+    }
+#endif
+    // add subgraph to render
+    camera->addChild(geode);
+    camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
+    camera->setClearColor(osg::Vec4(0.0, 0.0, 0.0, 1.0));
+    camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
+    camera->setCullingMode(osg::CullSettings::NO_CULLING);
+    camera->setName("DistortionCorrectionCamera");
+}
+
 CameraInfo* CameraGroup::buildCamera(SGPropertyNode* cameraNode)
 {
     WindowBuilder *wBuild = WindowBuilder::getWindowBuilder();
     const SGPropertyNode* windowNode = cameraNode->getNode("window");
     GraphicsWindow* window = 0;
-    static int cameraNum = 0;
     int cameraFlags = DO_INTERSECTION_TEST;
     if (windowNode) {
         // New style window declaration / definition
@@ -301,7 +530,11 @@ CameraInfo* CameraGroup::buildCamera(SGPropertyNode* cameraNode)
                            | CullSettings::VIEW_FRUSTUM_CULLING);
     camera->setInheritanceMask(CullSettings::ALL_VARIABLES
                                & ~(CullSettings::CULL_MASK
-                                   | CullSettings::CULLING_MODE));
+                                   | CullSettings::CULLING_MODE
+#if defined(HAVE_CULLSETTINGS_CLEAR_MASK)
+                                   | CullSettings::CLEAR_MASK
+#endif
+                                   ));
 
     osg::Matrix pOff;
     osg::Matrix vOff;
@@ -366,12 +599,43 @@ CameraInfo* CameraGroup::buildCamera(SGPropertyNode* cameraNode)
         double sheary = cameraNode->getDoubleValue("shear-y", 0);
         pOff.makeTranslate(-shearx, -sheary, 0);
     }
-    CameraInfo* info = addCamera(cameraFlags, camera, pOff, vOff);
+    const SGPropertyNode* textureNode = cameraNode->getNode("texture");
+    if (textureNode) {
+        string texName = textureNode->getStringValue("name");
+        int tex_width = textureNode->getIntValue("width");
+        int tex_height = textureNode->getIntValue("height");
+        TextureRectangle* texture = new TextureRectangle;
+
+        texture->setTextureSize(tex_width, tex_height);
+        texture->setInternalFormat(GL_RGB);
+        texture->setFilter(Texture::MIN_FILTER, Texture::LINEAR);
+        texture->setFilter(Texture::MAG_FILTER, Texture::LINEAR);
+        texture->setWrap(Texture::WRAP_S, Texture::CLAMP_TO_EDGE);
+        texture->setWrap(Texture::WRAP_T, Texture::CLAMP_TO_EDGE);
+        camera->setDrawBuffer(GL_FRONT);
+        camera->setReadBuffer(GL_FRONT);
+        camera->setRenderTargetImplementation(Camera::FRAME_BUFFER_OBJECT);
+        camera->attach(Camera::COLOR_BUFFER, texture);
+        _textureTargets[texName] = texture;
+    } else {
+        camera->setDrawBuffer(GL_BACK);
+        camera->setReadBuffer(GL_BACK);
+    }
+    const SGPropertyNode* psNode = cameraNode->getNode("panoramic-spherical");
+    bool useMasterSceneGraph = !psNode;
+    CameraInfo* info = addCamera(cameraFlags, camera, vOff, pOff,
+                                 useMasterSceneGraph);
     // If a viewport isn't set on the camera, then it's hard to dig it
     // out of the SceneView objects in the viewer, and the coordinates
     // of mouse events are somewhat bizzare.
     SGPropertyNode* viewportNode = cameraNode->getNode("viewport", true);
     buildViewport(info, viewportNode, window->gc->getTraits());
+    updateCameras(info);
+    // Distortion camera needs the viewport which is created by addCamera().
+    if (psNode) {
+        info->flags = info->flags | VIEW_ABSOLUTE;
+        buildDistortionCamera(psNode, camera);
+    }
     return info;
 }
 
@@ -382,16 +646,16 @@ CameraInfo* CameraGroup::buildGUICamera(SGPropertyNode* cameraNode,
     const SGPropertyNode* windowNode = (cameraNode
                                         ? cameraNode->getNode("window")
                                         : 0);
-    static int cameraNum = 0;
-    if (!window) {
-        if (windowNode) {
-            // New style window declaration / definition
-            window = wBuild->buildWindow(windowNode);
-            
-        } else {
-            return 0;
-        }
+    if (!window && windowNode) {
+      // New style window declaration / definition
+      window = wBuild->buildWindow(windowNode);
     }
+    
+    if (!window) { // buildWindow can fail
+      SG_LOG(SG_GENERAL, SG_WARN, "CameraGroup::buildGUICamera: failed to build a window");
+      return NULL;
+    }
+    
     Camera* camera = new Camera;
     camera->setAllowEventFocus(false);
     camera->setGraphicsContext(window->gc.get());
@@ -402,7 +666,11 @@ CameraInfo* CameraGroup::buildGUICamera(SGPropertyNode* cameraNode,
         camera->setClearMask(0);
     camera->setInheritanceMask(CullSettings::ALL_VARIABLES
                                & ~(CullSettings::COMPUTE_NEAR_FAR_MODE
-                                   | CullSettings::CULLING_MODE));
+                                   | CullSettings::CULLING_MODE
+#if defined(HAVE_CULLSETTINGS_CLEAR_MASK)
+                                   | CullSettings::CLEAR_MASK
+#endif
+                                   ));
     camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
     camera->setCullingMode(osg::CullSettings::NO_CULLING);
     camera->setProjectionResizePolicy(Camera::FIXED);
@@ -415,6 +683,7 @@ CameraInfo* CameraGroup::buildGUICamera(SGPropertyNode* cameraNode,
 
     // Disable statistics for the GUI camera.
     result->camera->setStats(0);
+    updateCameras(result);
     return result;
 }
 
@@ -433,16 +702,43 @@ CameraGroup* CameraGroup::buildCameraGroup(osgViewer::Viewer* viewer,
             cgroup->buildGUICamera(pNode);
         }
     }
+    bindMemberToNode(gnode, "znear", cgroup, &CameraGroup::_zNear, .1f);
+    bindMemberToNode(gnode, "zfar", cgroup, &CameraGroup::_zFar, 120000.0f);
+    bindMemberToNode(gnode, "near-field", cgroup, &CameraGroup::_nearField,
+                     100.0f);
     return cgroup;
 }
 
 void CameraGroup::setCameraCullMasks(Node::NodeMask nm)
 {
     for (CameraIterator i = camerasBegin(), e = camerasEnd(); i != e; ++i) {
-        if ((*i)->flags & GUI)
+        CameraInfo* info = i->get();
+        if (info->flags & GUI)
             continue;
-        (*i)->camera->setCullMask(nm & ~simgear::BACKGROUND_BIT);
-        (*i)->farCamera->setCullMask(nm);
+        if (info->farCamera.valid() && info->farCamera->getNodeMask() != 0) {
+            info->camera->setCullMask(nm & ~simgear::BACKGROUND_BIT);
+            info->camera->setCullMaskLeft(nm & ~simgear::BACKGROUND_BIT);
+            info->camera->setCullMaskRight(nm & ~simgear::BACKGROUND_BIT);
+            info->farCamera->setCullMask(nm);
+            info->farCamera->setCullMaskLeft(nm);
+            info->farCamera->setCullMaskRight(nm);
+        } else {
+            info->camera->setCullMask(nm);
+            info->camera->setCullMaskLeft(nm);
+            info->camera->setCullMaskRight(nm);
+        }
+    }
+}
+
+void CameraGroup::resized()
+{
+    for (CameraIterator i = camerasBegin(), e = camerasEnd(); i != e; ++i) {
+        CameraInfo *info = i->get();
+        const Viewport* viewport = info->camera->getViewport();
+        info->x = viewport->x();
+        info->y = viewport->y();
+        info->width = viewport->width();
+        info->height = viewport->height();
     }
 }
 
@@ -484,9 +780,25 @@ bool computeIntersections(const CameraGroup* cgroup,
               && y >= viewport->y() - epsilon
               && y < viewport->y() + viewport->height() -1.0 + epsilon))
             continue;
-        LineSegmentIntersector::CoordinateFrame cf = Intersector::WINDOW;
+        Vec4d start(x, y, 0.0, 1.0);
+        Vec4d end(x, y, 1.0, 1.0);
+        Matrix windowMat = viewport->computeWindowMatrix();
+        Matrix startPtMat = Matrix::inverse(camera->getProjectionMatrix()
+                                            * windowMat);
+        Matrix endPtMat;
+        if (!cinfo->farCamera.valid() || cinfo->farCamera->getNodeMask() == 0)
+            endPtMat = startPtMat;
+        else
+            endPtMat = Matrix::inverse(cinfo->farCamera->getProjectionMatrix()
+                                       * windowMat);
+        start = start * startPtMat;
+        start /= start.w();
+        end = end * endPtMat;
+        end /= end.w();
         ref_ptr<LineSegmentIntersector> picker
-            = new LineSegmentIntersector(cf, x, y);
+            = new LineSegmentIntersector(Intersector::VIEW,
+                                         Vec3d(start.x(), start.y(), start.z()),
+                                         Vec3d(end.x(), end.y(), end.z()));
         osgUtil::IntersectionVisitor iv(picker.get());
         const_cast<Camera*>(camera)->accept(iv);
         if (picker->containsIntersections()) {