|
楼主 |
发表于 2009-7-27 11:06:23
|
显示全部楼层
本帖最后由 vincent 于 2009-7-27 11:16 编辑
再次请教版主及各位高手。
用平面与地形求交,求出的交点有问题,应该是线段所在的垂直平面求错了。
不知道怎么解决?谢谢各位。- //起点startPt,终点endPt
- //垂直地形的平面--,我选取的确定平面的第三点是不是有问题???
- osg::Plane plane(startPt, endPt,osg::Vec3(startPt.x(),startPt.y(),
- (startPt.z()+endPt.z())/2.0));
- //法线
- osg::Vec3f norm(endPt-startPt);
- norm.normalize();
- osg::Polytope polytope;
- polytope.add( osg::Plane(norm,startPt));
- polytope.add( osg::Plane(-norm, endPt));
-
- osg::ref_ptr<osgUtil::PlaneIntersector>intersector = new osgUtil::PlaneIntersector( plane, polytope );
- osgUtil::IntersectionVisitor iv( intersector.get() );
- root->accept( iv );
- osg::ref_ptr <osg::Vec3Array> linepoint = new osg::Vec3Array;
- if ( intersector->containsIntersections() )
- {
- osgUtil::PlaneIntersector::Intersections& intersections =
- intersector->getIntersections();
- osgUtil::PlaneIntersector::Intersections::iterator itr;
- for ( itr = intersections.begin(); itr != intersections.end(); ++itr )
- {
- for( unsigned int i=0; i<(*itr).polyline.size(); ++i)
- {
- fx1 = (*itr).polyline[i].x();
- fy1 = (*itr).polyline[i].y()
- fz1 = (*itr).polyline[i].z();
- linepoint->push_back(osg::Vec3(fx1,fy1,fz1));
- }
- }
- osg::Geode* pyramidGeode = new osg::Geode();
- osg::Geometry* pyramidGeometry = new osg::Geometry();
- pyramidGeometry->setVertexArray(linepoint.get());
- osg::Vec4Array* colors = new osg::Vec4Array;
- colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ); //index 0 red
- pyramidGeometry->setColorArray(colors);
- pyramidGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
- pyramidGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, 0, linepoint->size()));
- pyramidGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
- pyramidGeode->addDrawable(pyramidGeometry);
- root->addChild(pyramidGeode);
复制代码 |
|