Added handling of large shapes

This commit is contained in:
Robert Osfield 2017-03-28 18:29:26 +01:00
parent 9a786fd8be
commit eb59590d77
2 changed files with 61 additions and 25 deletions

View File

@ -33,7 +33,7 @@
// for the grid data..
#include "../osghangglide/terrain_coords.h"
osg::Geode* createShapes()
osg::Geode* createShapes(osg::ArgumentParser& arguments)
{
osg::Geode* geode = new osg::Geode();
@ -70,17 +70,43 @@ osg::Geode* createShapes()
geode->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(8.0f,0.0f,0.0f),radius,height),hints));
osg::HeightField* grid = new osg::HeightField;
grid->allocate(38,39);
grid->setXInterval(0.28f);
grid->setYInterval(0.28f);
for(unsigned int r=0;r<39;++r)
if (arguments.read("--large"))
{
for(unsigned int c=0;c<38;++c)
unsigned int numX = 512;
unsigned int numY = 512;
double sizeX = 10.0;
double sizeY = 10.0;
grid->allocate(numX,numY);
grid->setXInterval(sizeX/float(numX));
grid->setYInterval(sizeY/float(numY));
for(unsigned int r=0;r<numY;++r)
{
grid->setHeight(c,r,vertex[r+c*39][2]);
for(unsigned int c=0;c<numX;++c)
{
double rx = double(c)/double(numX-1);
double ry = double(r)/double(numY-1);
grid->setHeight(c, r, 2.0*sin(rx*ry*4.0*osg::PI));
}
}
}
else
{
grid->allocate(38,39);
grid->setXInterval(0.28f);
grid->setYInterval(0.28f);
for(unsigned int r=0;r<39;++r)
{
for(unsigned int c=0;c<38;++c)
{
grid->setHeight(c,r,vertex[r+c*39][2]);
}
}
}
geode->addDrawable(new osg::ShapeDrawable(grid));
osg::ConvexHull* mesh = new osg::ConvexHull;
@ -109,13 +135,15 @@ osg::Geode* createShapes()
return geode;
}
int main(int, char **)
int main(int argc, char **argv)
{
osg::ArgumentParser arguments(&argc,argv);
// construct the viewer.
osgViewer::Viewer viewer;
osgViewer::Viewer viewer(arguments);
// add model to viewer.
viewer.setSceneData( createShapes() );
viewer.setSceneData( createShapes(arguments) );
// add the state manipulator
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );

View File

@ -243,10 +243,15 @@ void BuildShapeGeometryVisitor::End()
{
if (_start_index>=_vertices->size()) return;
bool smallPrimitiveSet = _vertices->size() < 65536;
// OSG_NOTICE<<"BuildShapeGeometryVisitor::End() smallPrimitiveSet = "<<smallPrimitiveSet<<std::endl;
if (_mode==GL_QUADS)
{
osg::ref_ptr<osg::DrawElementsUShort> primitives = new osg::DrawElementsUShort(GL_TRIANGLES);
osg::ref_ptr<osg::DrawElements> primitives = smallPrimitiveSet ?
static_cast<osg::DrawElements*>(new osg::DrawElementsUShort(GL_TRIANGLES)) :
static_cast<osg::DrawElements*>(new osg::DrawElementsUInt(GL_TRIANGLES));
_geometry->addPrimitiveSet(primitives.get());
for(unsigned int i=_start_index; i<_vertices->size(); i+=4)
@ -256,18 +261,21 @@ void BuildShapeGeometryVisitor::End()
unsigned int p2 = i+2;
unsigned int p3 = i+3;
primitives->push_back(p0);
primitives->push_back(p3);
primitives->push_back(p1);
primitives->addElement(p0);
primitives->addElement(p3);
primitives->addElement(p1);
primitives->push_back(p1);
primitives->push_back(p3);
primitives->push_back(p2);
primitives->addElement(p1);
primitives->addElement(p3);
primitives->addElement(p2);
}
}
else if (_mode==GL_QUAD_STRIP)
{
osg::ref_ptr<osg::DrawElementsUShort> primitives = new osg::DrawElementsUShort(GL_TRIANGLES);
osg::ref_ptr<osg::DrawElements> primitives = smallPrimitiveSet ?
static_cast<osg::DrawElements*>(new osg::DrawElementsUShort(GL_TRIANGLES)) :
static_cast<osg::DrawElements*>(new osg::DrawElementsUInt(GL_TRIANGLES));
_geometry->addPrimitiveSet(primitives.get());
for(unsigned int i=_start_index; i<_vertices->size()-2; i+=2)
@ -277,13 +285,13 @@ void BuildShapeGeometryVisitor::End()
unsigned int p2 = i+2;
unsigned int p3 = i+3;
primitives->push_back(p0);
primitives->push_back(p2);
primitives->push_back(p1);
primitives->addElement(p0);
primitives->addElement(p2);
primitives->addElement(p1);
primitives->push_back(p1);
primitives->push_back(p2);
primitives->push_back(p3);
primitives->addElement(p1);
primitives->addElement(p2);
primitives->addElement(p3);
}
}
else