|
这几天做的一个网格在海面上漂浮的效果,但发现海面起伏时总是有点穿透网格。代码如下。请教大家。
class DynamicGeomCallBack:public osg:rawable::UpdateCallback
{
public:
DynamicGeomCallBack(osg::ref_ptr<osgOcean::OceanScene> scene):_scene(scene){}
virtual void update(osg::NodeVisitor* nv, osg::Drawable* drawable)
{
//osg::Matrix mat = osg::computeLocalToWorld(nv->getNodePath());
osg::Geometry* geome = dynamic_cast<osg::Geometry*>(drawable);
if (!geome)
return;
osg::Vec3Array* vertics = dynamic_cast<osg::Vec3Array*>(geome->getVertexArray());
if (vertics)
{
for (osg::Vec3Array::iterator itr = vertics->begin();itr != vertics->end();++itr)
{
float z = _scene->getOceanSurfaceHeightAt((*itr).x(),(*itr).y());
(*itr).set(osg::Vec3((*itr).x(),(*itr).y(),z+0.5f));
}
vertics->dirty();
}
}
private:
osgOcean::OceanScene* _scene;
};
static osg::ref_ptr<osg::Geode> createWaveGrid(osg::Vec3 boatPos,osg::Vec3 boatToward,osgOcean::OceanScene* scene,float waveWidth = 30.0f,float waveLength = 50.0f)
{
osg:uat qt;
boatToward.normalize();
qt.makeRotate(osg::Vec3(0.0,1.0,0.0),boatToward);
osg::Matrix mt;
mt.makeRotate(qt);
int _numx = (int)(waveWidth/2.0f) ,_numy = (int)(waveLength/2.0f);
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<osg::Geometry> geome = new osg::Geometry;
osg::Vec3Array* vertices = new osg::Vec3Array(_numx*_numy);
float dx = waveWidth/(float)(_numx-1);
float dy = waveLength/(float)(_numy-1);
unsigned int vert_no = 0;
unsigned int vertx_no = 0;
unsigned int iy;
for(iy=0; iy<_numy; ++iy)
{
osg::Vec3 column = boatPos;
for(unsigned int ix=0;ix<_numx;++ix)
{
(*vertices)[vert_no++] = column*mt;
if ((ix == 0) && (iy == 0))
{
osg::Vec3 a = column;
std::cout<<a.x()<<" "<<a.y()<<" "<<a.z()<<std::endl;
}
column.x() += dx;
column.z() = 0.5f;
}
boatPos.y() += dy;
}
geome->setVertexArray(vertices);
for(iy=0; iy<_numy-1; ++iy)
{
unsigned int element_no = 0;
osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_TRIANGLE_STRIP, _numx*2);
unsigned int index = iy * _numx;
for(unsigned int ix = 0; ix<_numx; ++ix)
{
(*elements)[element_no++] = index + _numx;
(*elements)[element_no++] = index++;
}
geome->addPrimitiveSet(elements);
}
geome->setUpdateCallback(new DynamicGeomCallBack(scene));
geome->setUseDisplayList(false);
geome->setUseVertexBufferObjects(true);
geode->addDrawable(geome);
return geode.get();
} |
|