|
本帖最后由 rubby 于 2014-9-29 15:20 编辑
又向各位大牛请教一下:
我创建了两个圆柱面,半径不同,然后利用PlaneIntersector裁剪,是否可以将这两个圆柱面中间的圆环显示出来?
代码如下。现在的结果是: 此圆所在的面获取了,但是圆环没有,见附图,请各位赐教。 或者有别的方法实现吗吗??
比如:将两个面都截取出来,然后通过求两个面的差集,osg貌似没有这种功能。
小弟实在想不出方法。跪谢。
- int main( int argc, char** argv ){
- //1. 创建两个圆柱面
- float height = 5.f;
- osg::ref_ptr<osg::TessellationHints> hints;
- hints = new osg::TessellationHints;
- hints->setDetailRatio(0.5f);// 控制精细度
- 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());
- mynode->addDrawable(drawable2.get());
-
- osg::ref_ptr<osg::Group> root = new osg::Group;
- root->addChild(mynode.get() );
- //2 PlaneIntersector 实现
- osg::Plane plane( osg::Vec3(0,0,1), osg::Vec3(0,0,0) ); //(norm point) The normal of the plane. point A point of the plane.
- osg::Polytope polytope;
- /*polytope.add( osg::Plane(osg::Vec3(1,0,0), osg::Vec3(-1,0,0)) );
- polytope.add( osg::Plane(osg::Vec3(-1,0,0), osg::Vec3(1,0,0)) );*/
- osg::ref_ptr<osgUtil::PlaneIntersector> intersector = new osgUtil::PlaneIntersector( plane, polytope );
- osgUtil::IntersectionVisitor iv( intersector.get() );
- mynode->accept( iv );
- if ( intersector->containsIntersections() )
- {
- osgUtil::PlaneIntersector::Intersections
- &intersections = intersector->getIntersections();
- std::cout <<"intersection size "<<intersections.size()<<std::endl;
- osg::Geode * geode=new osg::Geode;
- osg::Geometry * geom=new osg::Geometry;
- 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::PlaneIntersector::Intersections::iterator itr;
- for ( itr = intersections.begin(); itr != intersections.end(); ++itr )
- {
- for ( unsigned int i=0; i<(*itr).polyline.size(); ++i )
- std::cout <<" X" << (*itr).polyline[i].x()
- << " Y" << (*itr).polyline[i].y()
- << " Z" <<(*itr).polyline[i].z() << std::endl;
- //交点坐标
- std::vector<osg::Vec3d> intersectPoints = itr->polyline;
- std::cout<< intersectPoints.size()<<std::endl;
-
- for (unsigned int i=0;i<intersectPoints.size();i++)
- {
- v3a->push_back(intersectPoints.at(i));
-
- }
- }
- geom->setVertexArray(v3a);
- geom->setColorArray(colors.get() );
- geom->setColorBinding(osg::Geometry::BIND_OVERALL);
- geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,0,v3a->size()));
- //geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,v3a->size()));
- geode->addDrawable(geom);
- osg::BoundingSphere bs=geode->getBound();
- std::cout<<"radius"<<bs.radius()<<std::endl;
- root->addChild(geode);
- }
- osgViewer::Viewer viewer;
- viewer.setSceneData(root.get());
- viewer.addEventHandler(new osgViewer::WindowSizeHandler);
- return viewer.run();
- }
复制代码 |
-
|