From 5dcea9eb218e671de20f28ce3e8990e67dbee7c4 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 17 Jun 2016 15:39:19 +0100 Subject: [PATCH] Replaced old slow path deprecated_osg::Geometry code --- .../osgocclusionquery/osgocclusionquery.cpp | 94 +++++++++++-------- 1 file changed, 56 insertions(+), 38 deletions(-) diff --git a/examples/osgocclusionquery/osgocclusionquery.cpp b/examples/osgocclusionquery/osgocclusionquery.cpp index bc558d610..2da672f68 100644 --- a/examples/osgocclusionquery/osgocclusionquery.cpp +++ b/examples/osgocclusionquery/osgocclusionquery.cpp @@ -561,15 +561,17 @@ createBox() osg::ref_ptr box = new osg::Geode; osg::StateSet* state = box->getOrCreateStateSet(); - osg::PolygonMode* pm = new osg::PolygonMode( - osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL ); - state->setAttributeAndModes( pm, - osg::StateAttribute::ON | osg::StateAttribute::PROTECTED ); - osg::ref_ptr geom = new deprecated_osg::Geometry; + osg::PolygonMode* pm = new osg::PolygonMode( osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL ); + state->setAttributeAndModes( pm, osg::StateAttribute::ON | osg::StateAttribute::PROTECTED ); + + osg::ref_ptr geom = new osg::Geometry; osg::ref_ptr v = new osg::Vec3Array; geom->setVertexArray( v.get() ); + osg::ref_ptr n = new osg::Vec3Array; + geom->setNormalArray( n.get(), osg::Array::BIND_PER_VERTEX ); + { const float x( 0.f ); const float y( 0.f ); @@ -581,40 +583,56 @@ createBox() v->push_back( osg::Vec3( x-r, y+r, z+r ) ); v->push_back( osg::Vec3( x-r, y+r, z-r ) ); + n->push_back( osg::Vec3( -1.f, 0.f, 0.f ) ); + n->push_back( osg::Vec3( -1.f, 0.f, 0.f ) ); + n->push_back( osg::Vec3( -1.f, 0.f, 0.f ) ); + n->push_back( osg::Vec3( -1.f, 0.f, 0.f ) ); + v->push_back( osg::Vec3( x+r, y-r, z+r ) ); //right +X v->push_back( osg::Vec3( x+r, y-r, z-r ) ); v->push_back( osg::Vec3( x+r, y+r, z-r ) ); v->push_back( osg::Vec3( x+r, y+r, z+r ) ); + n->push_back( osg::Vec3( 1.f, 0.f, 0.f ) ); + n->push_back( osg::Vec3( 1.f, 0.f, 0.f ) ); + n->push_back( osg::Vec3( 1.f, 0.f, 0.f ) ); + n->push_back( osg::Vec3( 1.f, 0.f, 0.f ) ); + v->push_back( osg::Vec3( x-r, y-r, z-r ) ); // bottom -Z v->push_back( osg::Vec3( x-r, y+r, z-r ) ); v->push_back( osg::Vec3( x+r, y+r, z-r ) ); v->push_back( osg::Vec3( x+r, y-r, z-r ) ); + n->push_back( osg::Vec3( 0.f, 0.f, -1.f ) ); + n->push_back( osg::Vec3( 0.f, 0.f, -1.f ) ); + n->push_back( osg::Vec3( 0.f, 0.f, -1.f ) ); + n->push_back( osg::Vec3( 0.f, 0.f, -1.f ) ); + v->push_back( osg::Vec3( x-r, y-r, z+r ) ); // top +Z v->push_back( osg::Vec3( x+r, y-r, z+r ) ); v->push_back( osg::Vec3( x+r, y+r, z+r ) ); v->push_back( osg::Vec3( x-r, y+r, z+r ) ); + n->push_back( osg::Vec3( 0.f, 0.f, 1.f ) ); + n->push_back( osg::Vec3( 0.f, 0.f, 1.f ) ); + n->push_back( osg::Vec3( 0.f, 0.f, 1.f ) ); + n->push_back( osg::Vec3( 0.f, 0.f, 1.f ) ); + v->push_back( osg::Vec3( x-r, y+r, z-r ) ); // back +Y v->push_back( osg::Vec3( x-r, y+r, z+r ) ); v->push_back( osg::Vec3( x+r, y+r, z+r ) ); v->push_back( osg::Vec3( x+r, y+r, z-r ) ); + + n->push_back( osg::Vec3( 0.f, 1.f, 0.f ) ); + n->push_back( osg::Vec3( 0.f, 1.f, 0.f ) ); + n->push_back( osg::Vec3( 0.f, 1.f, 0.f ) ); + n->push_back( osg::Vec3( 0.f, 1.f, 0.f ) ); } osg::ref_ptr c = new osg::Vec4Array; - geom->setColorArray( c.get() ); - geom->setColorBinding( deprecated_osg::Geometry::BIND_OVERALL ); - c->push_back( osg::Vec4( 0.f, 1.f, 1.f, 1.f ) ); + geom->setColorArray( c.get(), osg::Array::BIND_OVERALL ); - osg::ref_ptr n = new osg::Vec3Array; - geom->setNormalArray( n.get() ); - geom->setNormalBinding( deprecated_osg::Geometry::BIND_PER_PRIMITIVE ); - n->push_back( osg::Vec3( -1.f, 0.f, 0.f ) ); - n->push_back( osg::Vec3( 1.f, 0.f, 0.f ) ); - n->push_back( osg::Vec3( 0.f, 0.f, -1.f ) ); - n->push_back( osg::Vec3( 0.f, 0.f, 1.f ) ); - n->push_back( osg::Vec3( 0.f, 1.f, 0.f ) ); + c->push_back( osg::Vec4( 0.f, 1.f, 1.f, 1.f ) ); geom->addPrimitiveSet( new osg::DrawArrays( GL_QUADS, 0, 20 ) ); box->addDrawable( geom.get() ); @@ -630,32 +648,37 @@ createBox() // * No vertex sharing // * Draw the triangles as wireframe osg::ref_ptr -createRandomTriangles( unsigned int num ) +createRandomTriangles( unsigned int numTriangles ) { + unsigned int numVertices = numTriangles*3; + osg::ref_ptr tris = new osg::Geode; osg::StateSet* ss = tris->getOrCreateStateSet(); // Force wireframe. Many gfx cards handle this poorly. - osg::PolygonMode* pm = new osg::PolygonMode( - osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE ); - ss->setAttributeAndModes( pm, osg::StateAttribute::ON | - osg::StateAttribute::PROTECTED); - ss->setMode( GL_LIGHTING, osg::StateAttribute::OFF | - osg::StateAttribute::PROTECTED); + osg::PolygonMode* pm = new osg::PolygonMode( osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE ); + ss->setAttributeAndModes( pm, osg::StateAttribute::ON | osg::StateAttribute::PROTECTED); + ss->setMode( GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED); + + osg::ref_ptr geom = new osg::Geometry; - osg::ref_ptr geom = new deprecated_osg::Geometry; // Disable display lists to decrease performance. geom->setUseDisplayList( false ); osg::ref_ptr v = new osg::Vec3Array; geom->setVertexArray( v.get() ); - v->resize( num*3 ); + v->resize( numVertices ); + + osg::ref_ptr c = new osg::Vec4Array; + geom->setColorArray( c.get(), osg::Array::BIND_PER_VERTEX); + c->resize( numVertices ); unsigned int i; srand( 0 ); #define RAND_NEG1_TO_1 ( ((rand()%20)-10)*.1 ) - for (i=0; i c = new osg::Vec4Array; - geom->setColorArray( c.get() ); - // Bind per primitive to force slow glBegin/glEnd path. - geom->setColorBinding( deprecated_osg::Geometry::BIND_PER_PRIMITIVE ); - c->resize( num ); - -#define RAND_0_TO_1 ( (rand()%10)*.1 ) - for (i=0; iaddPrimitiveSet( new osg::DrawArrays( GL_TRIANGLES, 0, num*3 ) ); + + geom->addPrimitiveSet( new osg::DrawArrays( GL_TRIANGLES, 0, numVertices ) ); tris->addDrawable( geom.get() ); return tris.get();