|
发表于 2011-12-6 21:43:37
|
显示全部楼层
garyliyong 发表于 2011-12-5 22:17
这样吧 我明天写个简单的框选示例程序传上来,实现框选,至于矩形框你自己绘制吧
我将例子改写了一下,实现了框选,高亮部分不完善,你自己改写吧- /* OpenSceneGraph example, osgkeyboardmouse.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- // Simple example of use of osgViewer::GraphicsWindow + Viewer
- // example that provides the user with control over view position with basic picking.
- #include <osg/Timer>
- #include <osg/io_utils>
- #include <osg/observer_ptr>
- #include <osgUtil/IntersectionVisitor>
- #include <osgUtil/PolytopeIntersector>
- #include <osgUtil/LineSegmentIntersector>
- #include <osgDB/ReadFile>
- #include <osgDB/WriteFile>
- #include <osgGA/TrackballManipulator>
- #include <osgGA/StateSetManipulator>
- #include <osgViewer/Viewer>
- #include <osgFX/Scribe>
- #include <iostream>
- #include <osg/MatrixTransform>
- // class to handle events with a pick
- class PickHandler : public osgGA::GUIEventHandler
- {
- public:
- PickHandler():
- _mx(0.0),_my(0.0)
- {}
- ~PickHandler() {}
- bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
- {
- osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
- if (!viewer) return false;
- ea.setHandled(true);
- switch(ea.getEventType())
- {
- case(osgGA::GUIEventAdapter::PUSH):
- {
- _mx = ea.getX();
- _my = ea.getY();
- }
- break;
- case(osgGA::GUIEventAdapter::DRAG):
- {
- _nx = ea.getX();
- _ny = ea.getY();
- }
- break;
- case(osgGA::GUIEventAdapter::RELEASE):
- {
- pick(ea,viewer);
- }
- break;
- default:
- break;
- }
- return true;
- }
- void pick(const osgGA::GUIEventAdapter& ea, osgViewer::Viewer* viewer)
- {
- osg::Node* scene = viewer->getSceneData();
- if (!scene) return;
- osg::notify(osg::NOTICE)<<std::endl;
- osg::Node* node = 0;
- osg::Group* parent = 0;
- osgUtil::PolytopeIntersector* picker;
-
- // use window coordinates
- // remap the mouse x,y into viewport coordinates.
-
- // half width, height.
- double mx = ea.getX();
- double my = ea.getY();
- double minX,minY,maxX,maxY;
- if (mx > _mx)
- {
- minX = _mx;
- maxX = mx;
- }
- else
- {
- minX = mx;
- maxX = _mx;
- }
- if (my > _my)
- {
- minY = _my;
- maxY = my;
- }
- else
- {
- minY = my;
- maxY = _my;
- }
- picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::WINDOW,
- minX,minY,maxX,maxY);
-
- osgUtil::IntersectionVisitor iv(picker);
- viewer->getCamera()->accept(iv);
- if (picker->containsIntersections())
- {
- osgUtil::PolytopeIntersector::Intersections intersections;
- osgUtil::PolytopeIntersector::Intersections::iterator iter;
- intersections = picker->getIntersections();
- for (iter = intersections.begin();iter!= intersections.end();iter++)
- {
- osg::NodePath nodePath = (*iter).nodePath;
- node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
- parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
- if (node)
- {
- toggleScribe(parent, node);
- }
-
- }
- }
- }
- void toggleScribe(osg::Group* parent, osg::Node* node)
- {
- if (!parent || !node) return;
-
- osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent);
- if (!parentAsScribe)
- {
- // node not already picked, so highlight it with an osgFX::Scribe
- osgFX::Scribe* scribe = new osgFX::Scribe();
- scribe->addChild(node);
- parent->replaceChild(node,scribe);
- }
- else
- {
- // node already picked so we want to remove scribe to unpick it.
- osg::Node::ParentList parentList = parentAsScribe->getParents();
- for(osg::Node::ParentList::iterator itr=parentList.begin();
- itr!=parentList.end();
- ++itr)
- {
- (*itr)->replaceChild(parentAsScribe,node);
- }
- }
- }
-
- protected:
- float _mx,_my;
- float _nx,_ny;
- };
- int main( int argc, char **argv )
- {
- osg::ref_ptr<osg::Group> loadedModel;
- loadedModel = new osg::Group;
- loadedModel->addChild(osgDB::readNodeFile("cow.osg"));
- osg::ref_ptr<osg::MatrixTransform> mat = new osg::MatrixTransform();
- mat->setMatrix(osg::Matrix::translate(10,0,0));
- mat->addChild(osgDB::readNodeFile("glider.osg"));
- loadedModel->addChild(mat);
- // create the window to draw to.
- osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
- traits->x = 200;
- traits->y = 200;
- traits->width = 800;
- traits->height = 600;
- traits->windowDecoration = true;
- traits->doubleBuffer = true;
- traits->sharedContext = 0;
- osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
- osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
- if (!gw)
- {
- osg::notify(osg::NOTICE)<<"Error: unable to create graphics window."<<std::endl;
- return 1;
- }
- // create the view of the scene.
- osgViewer::Viewer viewer;
- viewer.getCamera()->setGraphicsContext(gc.get());
- viewer.getCamera()->setViewport(0,0,800,600);
- viewer.setSceneData(loadedModel.get());
- // create a tracball manipulator to move the camera around in response to keyboard/mouse events
- viewer.setCameraManipulator( new osgGA::TrackballManipulator );
- osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator(viewer.getCamera()->getStateSet());
- viewer.addEventHandler(statesetManipulator.get());
- // add the pick handler
- viewer.addEventHandler(new PickHandler());
- viewer.realize();
- // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.)
- while(!viewer.done())
- {
- viewer.frame();
- }
- return 0;
- }
复制代码 |
|