|
本帖最后由 hisong7 于 2014-8-27 22:48 编辑
Hi,大家好,请大家帮忙,困扰了好几天了:
我现在把视点切换(移动相机)到场景中,然后把屏幕上每个点计算它的世界坐标并保存起来,然后再次变换视点,然后根据之前保存的世界坐标计算它们在屏幕上的坐标,按理说如果切换视点变化很小,应该此时计算出来的屏幕坐标有很多是在屏幕内的(实际观察也应当如此),但是我在计算之后却发觉,我把视点切换之后,得到出来的全部屏幕坐标却远远不再屏幕坐标范围之内,这是怎么回事呢?
- //计算移动相机前整个屏幕的世界坐标,保存在m_worldPoints里
- void MouseEvent::calcCurWorldPoint()
- {
- m_worldPoints->clear();
- osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(mAA);
- osg::ref_ptr<osg::Camera> camera = viewer->getCamera();
- osgViewer::Renderer *render = dynamic_cast<osgViewer::Renderer*>(camera->getRenderer());
- osgUtil::SceneView *sceneView = render->getSceneView(0);
- QFile txt_file("F://worldC.txt");
- txt_file.open(QIODevice::WriteOnly | QIODevice::Text);
- QTextStream txt_file_stream(&txt_file);
- for(int i = 0; i < 640; i++){ //width
- for (int j = 0; j < 480; j++)//height
- {
- osg::Vec3 world;
- osg::Vec3 screen(i, j, 0);
- if(sceneView->projectWindowIntoObject(screen, world))//得到世界坐标
- {
- m_worldPoints->push_back(world);
- }else{
- printf("project fail");
- }
- }
- }
- printf("calcCurWorldPoint done\n");
- }
- //计算移动视点后的屏幕坐标,却发觉已经便宜了屏幕很多
- void MouseEvent::calcAfterScreenPoint()
- {
-
- m_screenPoints->clear();
- osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(mAA);
- osg::ref_ptr<osg::Camera> camera = viewer->getCamera();
- osgViewer::Renderer *render = dynamic_cast<osgViewer::Renderer*>(camera->getRenderer());
- osgUtil::SceneView *sceneView = render->getSceneView(0);
- sceneView->projectObjectIntoWindow(mObject, mWindow);
- QFile txt_file("F://screenC.txt");
- txt_file.open(QIODevice::WriteOnly | QIODevice::Text);
- QTextStream txt_file_stream(&txt_file);
- int count = 0;
- for (int i = 0; i < 640; i++)
- {
- for (int j = 0; j < 480; j++)
- {
- osg::Vec3 window, tmpWindow;
- osg::Vec3 tmpWorld = m_worldPoints->at(count);
- sceneView->projectObjectIntoWindow(tmpWorld, tmpWindow);//得到屏幕坐标
- window = osg::Vec3(tmpWindow.x(), tmpWindow.y(), 0);
- m_screenPoints->push_back(window);
- count++;
- //printf("%d: after trans: %f, %f ",count, window.x(), window.y());
- //txt_file_stream << (int)(window.x() + 0.5f) << " " << (int)(window.y() + 0.5f) << "\n";//写入结果
- }
- }
- printf("calcAfterScreenPoint done\n");
- }
复制代码
|
-
计算当前屏幕到世界坐标
-
变换视点后的截屏,计算之前保存的世界坐标在当前视上的位置
|