Here the math bases http://bookofhook.com/mousepick.pdf

Here the script that I have written for another engine, I hope that help, it should be easy to understand ;)

function VRCamera::unProject(vx, vy, vz)
{
   var tempVector = vector(3);
   var clip = vector(4);
   var view = vector(4);
tempVector.x = vx;
    tempVector.y = vy;
    tempVector.z = vz;

   //View to Clip
   clip.x = 2.0 * (tempVector[0]-m_viewPort[0])/m_viewPort[2] - 1.0;
   clip.y = 2.0 * (tempVector[1]-m_viewPort[1])/m_viewPort[3] - 1.0;
   clip.z = 2.0 * tempVector[2] - 1.0;
   clip.w = 1.0;
   //Clip to view
   view.x = clip.x / projectionMatrix[0];
   view.y = clip.y / projectionMatrix[5];
   view.z = clip.w / projectionMatrix[11];
view.w = clip.z / projectionMatrix[14] - clip.w * projectionMatrix[10] / (projectionMatrix[11] * projectionMatrix[14]);
   //Normalization
   view /= view.w;
   // View to Global
return matrixMultiplyVector_4(matrixInverse_4(m_cachedMatrix), view); //cachedMatrix is the model view matrix
}

why nobody have written an unProject function for the osg::Camera? o.O

Ruqin Zhang ha scritto:

Hi, I got a problem when implementing code to transform from screen coordinate to world coordinate.
Here is the code:

void ASDSPickHandler::pick(const osgGA::GUIEventAdapter &ea, osgViewer::Viewer *viewer)
{
       ......
        osg::Vec3f primitivePosition(ea.getX(), ea.getY(), 0.0f);
        osg::Matrix viewMatrix = viewer->getCamera()->getViewMatrix();
osg::Matrix projectionMatrix = viewer->getCamera()->getProjectionMatrix(); osg::Matrix windowMatrix = viewer->getCamera()->getViewport()->computeWindowMatrix(); osg::Matrix cameraMatrix = viewMatrix * projectionMatrix * windowMatrix;

        osg::Matrix inverseCameraMatrix;
        inverseCameraMatrix.invert(cameraMatrix);

        primitivePosition = primitivePosition * inverseCameraMatrix;
       ......
}

Here, the output of "primitivePosition" seems not correct. Just don't know what happened. Can you guys help me out? Thanks in advance!

Ruqin

------------------------------------------------------------------------

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

begin:vcard
fn:Rosario Leonardi
n:Leonardi;Rosario
email;internet:[EMAIL PROTECTED]
x-mozilla-html:TRUE
version:2.1
end:vcard

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to