|
本帖最后由 Jetty 于 2011-7-12 11:14 编辑
大家好。我刚接触OSG不久,碰到一段范例程序,有些地方不懂,向大家请教。
程序来源: 《OpenSceneGraph三维渲染疫情设计与实践》 王锐,钱学雷 编著,清华大学出版社 P214 例8.23
原文代码如下:
#include <osgManipulator/CommandManager>
#include <osgManipulator/TranslateAxisDragger>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
class PickModelHandler:public osgGA::GUIEventHandler
{
protected:
osgManipulator::Dragger* _activeDragger;
osgManipulator::PointerInfo _pointer;
public:
PickModelHandler()
{
_activeDragger=0;
}
bool handle(const osgGA::GUIEventAdapter& ea,
osgGA::GUIActionAdapter& aa,
osg::Object*, osg::NodeVisitor*)
{
osgViewer::View* view=dynamic_cast<osgViewer::View*>(&aa);
if(!view)
return false;
switch(ea.getEventType())
{
case osgGA::GUIEventAdapter::PUSH:
{
_pointer.reset();
osgUtil::LineSegmentIntersector::Intersections hits;
if(view->computeIntersections(ea.getX(),ea.getY(),hits))
{
_pointer.setCamera(view->getCamera());
_pointer.setMousePosition(ea.getX(),ea.getY());
osgUtil::LineSegmentIntersector::Intersections ::iterator hitr;
for(hitr=hits.begin();hitr!=hits.end();++hitr)
{
_pointer.addIntersection(hitr->nodePath, hitr->getLocalIntersectPoint());
}
osg::NodePath::iterator itr;
for(itr=_pointer._hitList.front().first.begin();
itr!=_pointer._hitList.front().first.end();++itr)
{
osgManipulator::Dragger* dragger=
dynamic_cast<osgManipulator::Dragger*>(*itr);
if(dragger)
{
dragger->handle(_pointer,ea,aa);
_activeDragger=dragger;
break;
}
}
}
break;
}
case osgGA::GUIEventAdapter::DRAG:
case osgGA::GUIEventAdapter::RELEASE:
{
if(_activeDragger)
{
_pointer._hitIter=_pointer._hitList.begin();
_pointer.setCamera(view->getCamera());
_pointer.setMousePosition(ea.getX(),ea.getY());
_activeDragger->handle(_pointer,ea,aa);
}
if(ea.getEventType()==osgGA::GUIEventAdapter::RELEASE)
{
_activeDragger=NULL;
_pointer.reset();
}
break;
}
default:break;
}
return true;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
osg::Node* model=osgDB::readNodeFile("cow.osg");
osg::ref_ptr<osgManipulator::Selection>selection=new osgManipulator::Selection;
selection->addChild(model);
float scale=model->getBound().radius()*1.6;
osg::ref_ptr<osgManipulator::TranslateAxisDragger>dragger=new osgManipulator::TranslateAxisDragger;
dragger->setupDefaultGeometry();
dragger->setMatrix(osg::Matrix::scale(scale,scale,scale)*
osg::Matrix::translate(model->getBound().center()));
osg::ref_ptr<osg::Group>root=new osg::Group;
root->addChild(dragger.get());
root->addChild(selection.get());
root->addChild(osgDB::readNodeFile("axes.osg"));
osg::ref_ptr<osgManipulator::CommandManager>manager=new osgManipulator::CommandManager;
manager->connect(*dragger,*selection);
osgViewer::Viewer viewer;
viewer.addEventHandler(new PickModelHandler);
viewer.setSceneData(root.get());
return viewer.run();
}
问题如下:
1)请问是在哪句计算鼠标与坐标轴的交点,为什么是坐标轴而不是和牛的交点?
2)osgUtil::LineSegmentIntersector::Intersections ::iterator hitr;osg::NodePath::iterator itr; 作用是什么?iterator是什么含义?
3)for(itr=_pointer._hitList.front().first.begin();itr!=_pointer._hitList.front().first.end();++itr)是什么意思?
4)_pointer._hitIter=_pointer._hitList.begin();的作用是什么?
希望大家不吝赐教,先说谢谢了。 |
|