OSG的几个矩阵

模型矩阵:利用glMultMatrix()来实现Transform.
视点矩阵:又叫观察矩阵,利用glulookat()来实现照相机的位置变化来实现视点的变换
投影矩阵:利用glFrustum()或者gluPerspective()来实现,生成投影矩阵或者正视矩阵
视口变换:利用setViewport来实现,实现显示窗口的大小
并附上屏幕坐标到世界坐标,世界坐标到屏幕坐标的代码:(这是从一个小姐姐的博客学到的)
http://blog.csdn.net/u011310341/article/details/70159288


osg::Vec3d screen2World(osg::ref_ptr<osgViewer::Viewer> viewer, osg::Vec3 screenPoint)//将屏幕坐标转换到世界坐标
{
    osg::Vec3d vec3;
    osg::ref_ptr<osg::Camera> camera = viewer->getCamera();
    osg::Matrix mVPW = camera->getViewMatrix() * camera->getProjectionMatrix() * camera->getViewport()->computeWindowMatrix();
    osg::Matrix invertVPW;
    invertVPW.invert(mVPW);
    vec3 = screenPoint * invertVPW;
    return vec3;
}

osg::Vec3d world2Screen(osg::ref_ptr<osgViewer::Viewer> viewer, osg::Vec3 worldPoint)//将世界坐标转换到屏幕坐标
{
    osg::Vec3d vec3;
    osg::ref_ptr<osg::Camera> camera = viewer->getCamera();
    osg::Matrix mVPW = camera->getViewMatrix() * camera->getProjectionMatrix() * camera->getViewport()->computeWindowMatrix();
    vec3 = worldPoint * mVPW;
    return vec3;
}

并且这里注意几个问题,viewer一定要是viewer->run()之后的viewer,你可以设置事件回调,也就是camera的矩阵不能没生成好,不然求的坐标是不正确的

你可能感兴趣的:(osg)