|
各位大哥,国庆节时候,发现LineSegmentIntersector 有问题:
代码如下: 程序运行后,本来应该有8条线,结果只有7条线,如附件中的图:
- int main( int argc, char** argv ){
- float height = 5.f;
- osg::ref_ptr<osg::TessellationHints> hints;
- hints = new osg::TessellationHints;
- hints->setDetailRatio(0.5f);//TODO 控制精细度
- hints->setCreateBody(true);
- //hints->setCreateBackFace(true); //必须去除这句话,否则贴图闪烁。
- hints->setCreateBottom(false);
- hints->setCreateTop(false);
- osg::ref_ptr<osg::Geode> mynode = new osg::Geode;
- osg::ref_ptr<osg::ShapeDrawable> drawable = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f,0.0f,0.0f),1.0f,height),hints);
- osg::ref_ptr<osg::ShapeDrawable> drawable2 = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f,0.0f,0.0f),4.0f,height),hints);
- mynode->addDrawable(drawable.get()); // geom.get() or gemo. both are ok.
- mynode->addDrawable(drawable2.get());
- osg::ref_ptr<osg::Group> root = new osg::Group;
- root->addChild(mynode.get() );
- float angle = 0.0;
- float angleDelta = 2.0f*osg::PI/8;
- for(int i=0; i<8;i++){
- float c = cosf(angle);
- float s = sinf(angle);
- osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector( osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(10.0f*c,10.0f*s,0.0f) );
- osgUtil::IntersectionVisitor iv( intersector.get() );
- mynode->accept( iv );
- if ( intersector->containsIntersections() )
- {
- osgUtil::LineSegmentIntersector::Intersections
- &intersections = intersector->getIntersections();
- std::cout <<"intersection size "<<intersections.size()<<std::endl;
- osg::ref_ptr<osg::Geode> geode=new osg::Geode;
- osg::ref_ptr<osg::Geometry> geom=new osg::Geometry;
- osg::ref_ptr<osg::Vec3Array> v3a=new osg::Vec3Array;
- osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
- colors->push_back( osg::Vec4(1.0, 0.0, 0.0, 1.0) );
- osgUtil::LineSegmentIntersector::Intersections::iterator itr;
- for ( itr = intersections.begin(); itr != intersections.end(); ++itr ) //起始点:有四个交点,前两个交点一样,后两个交点一样(因为为了封闭)。其余点正常。
- {
- std::cout <<" X" << itr->getWorldIntersectPoint().x()
- << " Y" << itr->getWorldIntersectPoint().y()
- << " Z" << itr->getWorldIntersectPoint().z();
- if(itr == intersections.begin())
- v3a->push_back( itr->getWorldIntersectPoint());
-
- }
- itr--;
- v3a->push_back( itr->getWorldIntersectPoint());
- std::cout<<std::endl;
- geom->setVertexArray(v3a);
- geom->setColorArray(colors.get() );
- geom->setColorBinding(osg::Geometry::BIND_OVERALL);
- for(int k=0; k<v3a->size(); k++){
- std::cout<<"vectex: "<<v3a->at(k).x()<<" "<<v3a->at(k).y()<<" "<<v3a->at(k).z();
- }
- std::cout<<std::endl;
- geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,v3a->size()));
- geode->addDrawable(geom);
- root->addChild(geode);
- }else{
- std::cout<<"no intersection for i="<<i<<std::endl;
- std::cout<<"x "<<intersector->getStart().x()<<" y "<<intersector->getStart().y()<<" z "<<intersector->getStart().z()<<std::endl;
- std::cout<<"x "<<intersector->getEnd().x()<<" y "<<intersector->getEnd().y()<<" z "<<intersector->getEnd().z()<<std::endl;
- }
- angle+=angleDelta;
- }
- osgViewer::Viewer viewer;
- viewer.setSceneData(root.get());
- viewer.addEventHandler(new osgViewer::WindowSizeHandler);
- return viewer.run();
- }
复制代码 |
-
|