|
问题是自己建立一个节点,比如由点构成的球,采用求交计算,得不到交点,为什么,以下是代码片段(用OSG的球或其他形状的可以):
(1)初始化节点片段
std::vector<std::vector<estreetPoint>>vvGlobePts;
createOriginalGlobePts(vvGlobePts);
osg::ref_ptr<osg::Group>gr=createGlobeNode(vvGlobePts);
m_Root->addChild(gr.get());
(2)
//求球的点,以构成网格
//点按照从-90,到90的纬度线,每隔1度构成
void COsgScene::createOriginalGlobePts(std::vector<std::vector<estreetPoint>>&vvGlobePts)
{
double x,y,z;
double a=6378245,e=1/298.3,n;
double cosB,sinB,e2=e*e,b,l;
for(double B=90;B>=-90;B-=1.0)
{
b=B*estreetPi/180.0;
n=a/sqrt(1-e2*sin(b)*sin(b));
cosB=cos(b);
sinB=sin(b);
//保存纬度线上的点,经线按照1度增加
std::vector<estreetPoint>vBpts;
for(double L=0;L<=360;L+=1.0)
{
l=L*estreetPi/180.0;
x=n*cosB*cos(l);
y=n*cosB*sin(l);
z=n*(1-e2)*sinB;
vBpts.push_back(estreetPoint(x,y,z));
}
vvGlobePts.push_back(vBpts);
}
return ;
}
(3)
osg::Group* COsgScene::createGlobeNode(std::vector<std::vector<estreetPoint>>&vvGlobePts)
{
osg::ref_ptr<osg::Geometry> pointGeom = new osg::Geometry();
pointGeom->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
pointGeom->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON);
int numPts=0;
osg::ref_ptr<osg::Vec3dArray> vertices = new osg::Vec3dArray();
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
//注意索引
for(int i=0;i<(int)vvGlobePts.size()-1;i++)
for(int j=0;j<(int)vvGlobePts[i].size()-1;j++)
{
numPts+=4;;
vertices->push_back(osg::Vec3d(vvGlobePts[i][j].x,vvGlobePts[i][j].y,vvGlobePts[i][j].z));
vertices->push_back(osg::Vec3d(vvGlobePts[i+1][j].x,vvGlobePts[i+1][j].y,vvGlobePts[i+1][j].z));
vertices->push_back(osg::Vec3d(vvGlobePts[i+1][j+1].x,vvGlobePts[i+1][j+1].y,vvGlobePts[i+1][j+1].z));
vertices->push_back(osg::Vec3d(vvGlobePts[i][j+1].x,vvGlobePts[i][j+1].y,vvGlobePts[i][j+1].z));
osg::Vec3d n1(vvGlobePts[i][j].x,vvGlobePts[i][j].y,vvGlobePts[i][j].z);
osg::Vec3d n2(vvGlobePts[i+1][j].x,vvGlobePts[i+1][j].y,vvGlobePts[i+1][j].z);
osg::Vec3d n3(vvGlobePts[i+1][j+1].x,vvGlobePts[i+1][j+1].y,vvGlobePts[i+1][j+1].z);
osg::Vec3d n4(vvGlobePts[i][j+1].x,vvGlobePts[i][j+1].y,vvGlobePts[i][j+1].z);
n1.normalize();
n2.normalize();
n3.normalize();
n4.normalize();
normals->push_back(n1);
normals->push_back(n2);
normals->push_back(n3);
normals->push_back(n4);
}
pointGeom->setVertexArray(vertices.get());
pointGeom->setNormalArray(normals.get());
pointGeom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
pointGeom->addPrimitiveSet(new osg:rawArrays(osg:rimitiveSet:UADS,0,numPts));
osg::ref_ptr<osg::Geode>ge=new osg::Geode();
ge->addDrawable(pointGeom.get());
osg::ref_ptr<osg::Group>gr=new osg::Group();
gr->addChild(ge.get());
return gr.release();
} |
|