|
cameraManipulator请教!
应用MFC框架建立一个读取地球模型的程序,程序中应用了:
//建立窗口参数
RECT rect;
mViewer->addEventHandler(new osgViewer::StatsHandler);
::GetWindowRect(m_hWnd, &rect);
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindowWin32::WindowData(m_hWnd);
traits->x = 0; traits->y = 0;
traits->width = rect.right - rect.left;traits->height = rect.bottom - rect.top;
traits->windowDecoration = false;traits->doubleBuffer = true;
traits->sharedContext = 0;traits->setInheritedWindowPixelFormat = true; traits->inheritedWindowData = windata;
//设置相机和视图
osg::GraphicsContext* gc = osg::GraphicsContext::createGraphicsContext(traits.get());
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext(gc);
camera->setViewport(new osg::Viewport(traits->x, traits->y, traits->width, traits->height));
camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
camera->setClearColor(osg::Vec4f(0.0f, 0.0f, 0.0f, 1.0f));
camera->setProjectionMatrixAsPerspective(80.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0, 1000.0);
mViewer->setCamera(camera.get());
//对视点进行控制
//设置初始
m_earthManipulator->setHomeViewpoint(Viewpoint( osg::Vec3d( 0, 0, 0 ), 0.0, -90.0, 5e6 ) );
//m_earthManipulator
mViewer->setCameraManipulator(m_earthManipulator);
//mViewer->setCameraManipulator( keyswitchManipulator );
//加入根结点
mViewer->setSceneData(mRoot.get());
mViewer->realize();
// 修正视口比率
double fovy,aspectRatio,z1,z2;
mViewer->getCamera()->getProjectionMatrixAsPerspective(fovy,aspectRatio,z1,z2);
aspectRatio=double(traits->width)/double(traits->height);
mViewer->getCamera()->setProjectionMatrixAsPerspective(fovy,aspectRatio,z1,z2);
生成结果如下:
同样的my.earth文件,应用osgearth_viewer来读取,结果如下:
现在存在一个困惑:
MFC中,太阳发生了变形。而osgearth_viewer中太阳不会发生变形。通过操作发现,鼠标滚轮操作放大或者缩小场景(前移视点或者后退视点),MFC中太阳和整个星空背景、地球都会放大或缩小。而osgearth_viewer中鼠标滚轮操作放大或者缩小场景(前移视点或者后退视点)时,只有地球会放大或缩小,而太阳与整个星空背景不会发生任何移动。
请教array等高手给解答一下,谢谢! |
|