|
楼主 |
发表于 2011-2-18 11:28:31
|
显示全部楼层
首先,创建一个几何体:- int CreateCube()
- {
- osg::ref_ptr<osg::Geode> Cube = new osg::Geode;
- Cube->setName("CUBE");
- osg::ref_ptr<osg::Geometry> Cube0 =new osg::Geometry;
- Cube0->setName("cube0");
- osg::ref_ptr<osg::Vec3Array> v =new osg::Vec3Array;
- v->push_back(osg::Vec3(0.0f,0.0f,0.0f));
- v->push_back(osg::Vec3(1.0f,0.0f,0.0f));
- v->push_back(osg::Vec3(1.0f,1.0f,0.0f));
- v->push_back(osg::Vec3(0.0f,1.0f,0.0f));
- v->push_back(osg::Vec3(0.0f,0.0f,1.0f));
- v->push_back(osg::Vec3(1.0f,0.0f,1.0f));
- v->push_back(osg::Vec3(1.0f,1.0f,1.0f));
- v->push_back(osg::Vec3(0.0f,1.0f,1.0f));
- Cube0->setVertexArray(v.get());
- osg::ref_ptr<osg::DrawElementsUInt> bottom = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS,0);
- bottom->push_back(0);
- bottom->push_back(1);
- bottom->push_back(2);
- bottom->push_back(3);
- //。。。。。。。。。略去//
- Cube0->setUseDisplayList( false );
- Cube0->setUseVertexBufferObjects( true );
- Cube->addDrawable(Cube0.get());
- return osgDB::writeNodeFile(*Cube,"cube.ive");
- }
复制代码 然后,创建位移更新访问器。- class MoveVisitor:public osg::NodeVisitor
- {
- public:
- MoveVisitor():osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
- { }
- virtual void apply(osg::Geode& node)
- {
- osg::Drawable* drawable = node.getDrawable(0);
-
- osg::Geometry* geom = dynamic_cast<osg::Geometry*>( drawable );
- if ( !geom ) return;
- osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() );
- if ( vertices )
- {
- for (int i=0;i<vertices->getNumElements();i++)
- {
- osg::Vec3 point = vertices->at(i);
-
- (*vertices)[i].set(point.x(),point.y(),point.z()+1.0f);
- }
- osg::Vec3 point = vertices->at(0);
- if (point.z()>10.0f)
- {
- for (int i=0;i<vertices->getNumElements();i++)
- {
- osg::Vec3 point3 = vertices->at(i);
- (*vertices)[i].set(point3.x(),point3.y(),point3.z()-10.0f);
- }
- }
- vertices->dirty();
- }
- traverse(node);
- }
- };
复制代码 然后,实现主程序:- int _tmain(int argc, _TCHAR* argv[])
- {
- //读立方体文件
- osg::ref_ptr<osg::Group> root = new osg::Group;
- CreateCube();
- osg::ref_ptr<osg::Node> model = new osg::Node;
- model = osgDB::readNodeFile("cube.ive");
- MoveVisitor mv;//创建结点位移访问器
-
- //画一条线
- osg::ref_ptr<osg::Geometry> line = new osg::Geometry;
- osg::ref_ptr<osg::Vec3Array> lineVertices = new osg::Vec3Array;
- osg::Vec3 startp(0.5f,0.5f,9.0f);
- osg::Vec3 endp(0.5f,0.5f,10.0f);
- line->setVertexArray(lineVertices.get());
- lineVertices->push_back(startp);
- lineVertices->push_back(endp);
- line->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::LINES,0,2));
- osg::ref_ptr<osg::Geode> lineNode = new osg::Geode;
- lineNode->addDrawable(line.get());
- root->addChild(lineNode.get());
- root->addChild(model.get());
- //线段测试内容
- osgUtil::IntersectionVisitor iv;
- osgUtil::LineSegmentIntersector* lineTest = new osgUtil::LineSegmentIntersector(startp,endp);
- iv.setIntersector(lineTest);
- osgViewer::Viewer viewer;
- viewer.setCameraManipulator(new osgGA::TrackballManipulator);
- viewer.setSceneData(root.get());
- viewer.realize();
- //主循环
- while(!viewer.done())
- {
- viewer.frame();
- model->accept(mv);
- model->accept(iv);
- if (lineTest->containsIntersections())
- {
- cout<<"The Intersection exists!"<<endl;
- }
- //时间控制,用于降低帧速
- osg::Timer_t start = osg::Timer::instance()->tick();
- osg::Timer_t end = osg::Timer::instance()->tick();
- while (osg::Timer::instance()->delta_s(start,end)<0.1&&!viewer.done())
- {
- end = osg::Timer::instance()->tick();
- }
- }
- return 0;
- }
复制代码 这个程序中,求交判断结果为false,请各位帮忙看看是哪里的问题。
多谢! |
|