|
osg::Vec2Array *LinearConstraint::getRoadTexcoords(const osg::Vec3Array *points) {
// need to create a vec2 array from the coordinates that fits the road
osg::Vec3Array::const_iterator tritr;//常迭代器,这样就不能修改points里的内容了
osg::ref_ptr<osg::Vec2Array> tcoords= new osg::Vec2Array ;
for (tritr=points->begin(); tritr!=points->end();tritr++) {
osg::Vec2 tci(-1.,-1.);
int ib=0;
// osg::Vec3Array *varr=dynamic_cast<osg::Vec3Array*>(getVertexArray());
bool ptfound=false;
for (osg::Vec3Array::iterator vit=_edgecoords->begin(); vit!= _edgecoords->end() && !ptfound; vit++) {
if ((*vit)==(*tritr)) {
tci=_tcoords->at(ib);
ptfound=true;
}
ib++;
}///找到特定顶点的纹理坐标
if (!ptfound) { // search for surrounding points and interpolate
ib=0;
osg::Vec3 pminus=(_edgecoords->back()); // need pminus for interpolation
int ibm1=_edgecoords->size()-1;
for (osg::Vec3Array::iterator vit=_edgecoords->begin(); vit!= _edgecoords->end() /*&& !ptfound*/; vit++) {
osg::Vec3 pplus=(*vit)-(*tritr);
osg::Vec3 dpm=pminus-(*tritr);
pplus.set (pplus.x(),pplus.y(),0);
dpm.set (dpm.x(),dpm.y(),0);
float dprod=pplus*dpm/(pplus.length() * dpm.length());
if (dprod<-0.9999) { // *tritr lies between....
osg::Vec2 tminus=_tcoords->at(ibm1);
osg::Vec2 tplus=_tcoords->at(ib);
float frac=(dpm.length()/(dpm.length()+pplus.length()));
tci=tminus+((tplus-tminus)*frac);
ptfound=true;
}
ibm1=ib;
ib++;
pminus=(*vit);
}
}
tcoords->push_back(tci);
}
// some extra points are not interpolated as they lie between 2 interpolated vertices
for (tritr=points->begin(); tritr!=points->end();tritr++) {
int ib=tritr-points->begin();
osg::Vec2 tci=tcoords->at(ib);
if (tci.x()<-.99 && tci.y()<-.99) {
// search through each of the primitivesets
osg::Vec3Array::const_iterator ptitr;
// osg::notify(osg::WARN) << "Not calculated " << (*tritr).x() <<"," << (*tritr).y() << std::endl;
for (ptitr=points->begin(); ptitr!=points->end();ptitr++) {
}
}
}
return tcoords.release();
}
这个函数里面有些地方不是很好懂,特别是 if (!ptfound) 循环里面的这段代码,如果说它是想得到道路周围的点的纹理坐标,那为什么在里面的
if (dprod<-0.9999) { // *tritr lies between....
osg::Vec2 tminus=_tcoords->at(ibm1);
osg::Vec2 tplus=_tcoords->at(ib);
float frac=(dpm.length()/(dpm.length()+pplus.length()));
tci=tminus+((tplus-tminus)*frac);
ptfound=true;
}
里面没有push_back?请研究过这段代码的前辈给我点指导,谢谢了。 |
|