/usr/share/openscenegraph/examples/osgocclusionquery/osgocclusionquery.cpp is in openscenegraph-examples 3.2.0~rc1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 | /* OpenSceneGraph example, osganimate.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// This exampl demonstrates use of OcclusionQueryNode.
//
// In general, you use OcclusionQueryNode by simply attaching a subgraph
// or subgraphs as children, and it performs an OpenGL oclusion query
// to determine whether to draw the subgraphs or not.
//
// You can manually insert OcclusionQueryNodes at strategic locations
// in your scene graph, or you can write a NodeVisitor to insert them
// automatically, as this example shows.
//
// Run this example with no command line arguments, and it creates
// a "stock scene" to show how OcclusionQueryNode can be used.
//
// Or, run this example with a model on the command line, and the
// example uses a NodeVisitor to try to find worthwhile locations
// for OcclusionQueryNodes in your the scene graph.
#include <osg/NodeVisitor>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/StateSet>
#include <osg/StateAttribute>
#include <osg/PolygonMode>
#include <osg/ColorMask>
#include <osg/PolygonOffset>
#include <osg/Depth>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator>
#include <osg/OcclusionQueryNode>
#include <iostream>
#include <sstream>
// NodeVisitors and utility functions for OcclusionQueryNode
// Use this visitor to insert OcclusionQueryNodes (OQNs) in the
// visited subgraph. Only one OQN will test any particular node
// (no nesting). See also OcclusionQueryNonFlatVisitor.
class OcclusionQueryVisitor : public osg::NodeVisitor
{
public:
OcclusionQueryVisitor();
virtual ~OcclusionQueryVisitor();
// Specify the vertex count threshold for performing occlusion
// query tests. Nodes in the scene graph whose total child geometry
// contains fewer vertices than the specified threshold will
// never be tested, just drawn. (In fact, they will br treated as
// potential occluders and rendered first in front-to-back order.)
void setOccluderThreshold( int vertices );
int getOccluderThreshold() const;
virtual void apply( osg::OcclusionQueryNode& oqn );
virtual void apply( osg::Group& group );
virtual void apply( osg::Geode& geode );
protected:
void addOQN( osg::Node& node );
// When an OQR creates all OQNs and each OQN shares the same OQC,
// these methods are used to uniquely name all OQNs. Handy
// for debugging.
std::string getNextOQNName();
int getNameIdx() const { return _nameIdx; }
osg::ref_ptr<osg::StateSet> _state;
osg::ref_ptr<osg::StateSet> _debugState;
unsigned int _nameIdx;
int _occluderThreshold;
};
// Find all OQNs in the visited scene graph and set their visibility threshold.
class VisibilityThresholdVisitor : public osg::NodeVisitor
{
public:
VisibilityThresholdVisitor( unsigned int threshold=500 )
: osg::NodeVisitor( osg::NodeVisitor::TRAVERSE_ALL_CHILDREN ),
_visThreshold( threshold ) {}
virtual ~VisibilityThresholdVisitor() {}
virtual void apply( osg::OcclusionQueryNode& oqn );
protected:
unsigned int _visThreshold;
};
// Find all OQNs in the visited scene graph and set the number of frames
// between queries.
class QueryFrameCountVisitor : public osg::NodeVisitor
{
public:
QueryFrameCountVisitor( int count=5 )
: osg::NodeVisitor( osg::NodeVisitor::TRAVERSE_ALL_CHILDREN ),
_count( count ) {}
virtual ~QueryFrameCountVisitor() {}
virtual void apply( osg::OcclusionQueryNode& oqn );
protected:
unsigned int _count;
};
// Find all OQNs in the visited scene graph and enable or disable queries..
class EnableQueryVisitor : public osg::NodeVisitor
{
public:
EnableQueryVisitor( bool enable=true )
: osg::NodeVisitor( osg::NodeVisitor::TRAVERSE_ALL_CHILDREN ),
_enabled( enable ) {}
virtual ~EnableQueryVisitor() {}
virtual void apply( osg::OcclusionQueryNode& oqn );
protected:
bool _enabled;
};
// Find all OQNs in the visited scene graph and enable or disable the
// debug bounding volume display.
class DebugDisplayVisitor : public osg::NodeVisitor
{
public:
DebugDisplayVisitor( bool debug=true )
: osg::NodeVisitor( osg::NodeVisitor::TRAVERSE_ALL_CHILDREN ),
_debug( debug ) {}
virtual ~DebugDisplayVisitor() {}
virtual void apply( osg::OcclusionQueryNode& oqn );
protected:
bool _debug;
};
// Remove all OQNs from the visited scene graph.
class RemoveOcclusionQueryVisitor : public osg::NodeVisitor
{
public:
RemoveOcclusionQueryVisitor();
virtual ~RemoveOcclusionQueryVisitor();
virtual void apply( osg::OcclusionQueryNode& oqn );
protected:
};
// Gather statistics about OQN performance in the visited scene graph.
class StatisticsVisitor : public osg::NodeVisitor
{
public:
StatisticsVisitor( osg::NodeVisitor::TraversalMode mode=osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN );
virtual ~StatisticsVisitor();
virtual void apply( osg::OcclusionQueryNode& oqn );
void reset();
unsigned int getNumOQNs() const;
unsigned int getNumPassed() const;
protected:
unsigned int _numOQNs;
unsigned int _numPassed;
};
unsigned int countGeometryVertices( osg::Geometry* geom )
{
if (!geom->getVertexArray())
return 0;
// TBD This will eventually iterate over the PrimitiveSets and total the
// number of vertices actually used. But for now, it just returns the
// size of the vertex array.
return geom->getVertexArray()->getNumElements();
}
class VertexCounter : public osg::NodeVisitor
{
public:
VertexCounter( int limit )
: osg::NodeVisitor( osg::NodeVisitor::TRAVERSE_ALL_CHILDREN ),
_limit( limit ),
_total( 0 ) {}
~VertexCounter() {}
int getTotal() { return _total; }
bool exceeded() const { return _total > _limit; }
void reset() { _total = 0; }
virtual void apply( osg::Node& node )
{
// Check for early abort. If out total already exceeds the
// max number of vertices, no need to traverse further.
if (exceeded())
return;
traverse( node );
}
virtual void apply( osg::Geode& geode )
{
// Possible early abort.
if (exceeded())
return;
unsigned int i;
for( i = 0; i < geode.getNumDrawables(); i++ )
{
osg::Geometry* geom = dynamic_cast<osg::Geometry *>(geode.getDrawable(i));
if( !geom )
continue;
_total += countGeometryVertices( geom );
if (_total > _limit)
break;
}
}
protected:
int _limit;
int _total;
};
OcclusionQueryVisitor::OcclusionQueryVisitor()
: osg::NodeVisitor( osg::NodeVisitor::TRAVERSE_ALL_CHILDREN ),
_nameIdx( 0 ),
_occluderThreshold( 5000 )
{
// Create a dummy OcclusionQueryNode just so we can get its state.
// We'll then share that state between all OQNs we add to the visited scene graph.
osg::ref_ptr<osg::OcclusionQueryNode> oqn = new osg::OcclusionQueryNode;
_state = oqn->getQueryStateSet();
_debugState = oqn->getDebugStateSet();
}
OcclusionQueryVisitor::~OcclusionQueryVisitor()
{
osg::notify( osg::INFO ) <<
"osgOQ: OcclusionQueryVisitor: Added " << getNameIdx() <<
" OQNodes." << std::endl;
}
void
OcclusionQueryVisitor::setOccluderThreshold( int vertices )
{
_occluderThreshold = vertices;
}
int
OcclusionQueryVisitor::getOccluderThreshold() const
{
return _occluderThreshold;
}
void
OcclusionQueryVisitor::apply( osg::OcclusionQueryNode& oqn )
{
// A subgraph is already under osgOQ control.
// Don't traverse further.
return;
}
void
OcclusionQueryVisitor::apply( osg::Group& group )
{
if (group.getNumParents() == 0)
{
// Can't add an OQN above a root node.
traverse( group );
return;
}
int preTraverseOQNCount = getNameIdx();
traverse( group );
if (getNameIdx() > preTraverseOQNCount)
// A least one OQN was added below the current node.
// Don't add one here to avoid hierarchical nesting.
return;
// There are no OQNs below this group. If the vertex
// count exceeds the threshold, add an OQN here.
addOQN( group );
}
void
OcclusionQueryVisitor::apply( osg::Geode& geode )
{
if (geode.getNumParents() == 0)
{
// Can't add an OQN above a root node.
traverse( geode );
return;
}
addOQN( geode );
}
void
OcclusionQueryVisitor::addOQN( osg::Node& node )
{
VertexCounter vc( _occluderThreshold );
node.accept( vc );
if (vc.exceeded())
{
// Insert OQN(s) above this node.
unsigned int np = node.getNumParents();
while (np--)
{
osg::Group* parent = dynamic_cast<osg::Group*>( node.getParent( np ) );
if (parent != NULL)
{
osg::ref_ptr<osg::OcclusionQueryNode> oqn = new osg::OcclusionQueryNode();
oqn->addChild( &node );
parent->replaceChild( &node, oqn.get() );
oqn->setName( getNextOQNName() );
// Set all OQNs to use the same query StateSets (instead of multiple copies
// of the same StateSet) for efficiency.
oqn->setQueryStateSet( _state.get() );
oqn->setDebugStateSet( _debugState.get() );
}
}
}
}
std::string
OcclusionQueryVisitor::getNextOQNName()
{
std::ostringstream ostr;
ostr << "OQNode_" << _nameIdx++;
return ostr.str();
}
//
void
VisibilityThresholdVisitor::apply( osg::OcclusionQueryNode& oqn )
{
oqn.setVisibilityThreshold( _visThreshold );
traverse( oqn );
}
void
QueryFrameCountVisitor::apply( osg::OcclusionQueryNode& oqn )
{
oqn.setQueryFrameCount( _count );
traverse( oqn );
}
void
EnableQueryVisitor::apply( osg::OcclusionQueryNode& oqn )
{
oqn.setQueriesEnabled( _enabled );
traverse( oqn );
}
void
DebugDisplayVisitor::apply( osg::OcclusionQueryNode& oqn )
{
oqn.setDebugDisplay( _debug );
traverse( oqn );
}
RemoveOcclusionQueryVisitor::RemoveOcclusionQueryVisitor()
: osg::NodeVisitor( osg::NodeVisitor::TRAVERSE_ALL_CHILDREN )
{
}
RemoveOcclusionQueryVisitor::~RemoveOcclusionQueryVisitor()
{
}
void
RemoveOcclusionQueryVisitor::apply( osg::OcclusionQueryNode& oqn )
{
if (oqn.getNumParents() == 0)
{
// Even if this is an OQN, can't delete it because it's the root.
traverse( oqn );
return;
}
osg::ref_ptr<osg::OcclusionQueryNode> oqnPtr = &oqn;
unsigned int np = oqn.getNumParents();
while (np--)
{
osg::Group* parent = dynamic_cast<osg::Group*>( oqn.getParent( np ) );
if (parent != NULL)
{
// Remove OQN from parent.
parent->removeChild( oqnPtr.get() );
// Add OQN's children to parent.
unsigned int nc = oqn.getNumChildren();
while (nc--)
parent->addChild( oqn.getChild( nc ) );
}
}
}
StatisticsVisitor::StatisticsVisitor( osg::NodeVisitor::TraversalMode mode )
: osg::NodeVisitor( mode ),
_numOQNs( 0 ),
_numPassed( 0 )
{
}
StatisticsVisitor::~StatisticsVisitor()
{
}
void
StatisticsVisitor::apply( osg::OcclusionQueryNode& oqn )
{
_numOQNs++;
if (oqn.getPassed())
_numPassed++;
traverse( oqn );
}
void
StatisticsVisitor::reset()
{
_numOQNs = _numPassed = 0;
}
unsigned int
StatisticsVisitor::getNumOQNs() const
{
return _numOQNs;
}
unsigned int
StatisticsVisitor::getNumPassed() const
{
return _numPassed;
}
// End NodeVisitors
// KetHandler --
// Allow user to do interesting things with an
// OcclusionQueryNode-enabled scene graph at run time.
class KeyHandler : public osgGA::GUIEventHandler
{
public:
KeyHandler( osg::Node& node )
: _node( node ),
_enable( true ),
_debug( false )
{}
bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& )
{
switch( ea.getEventType() )
{
case(osgGA::GUIEventAdapter::KEYUP):
{
if (ea.getKey()==osgGA::GUIEventAdapter::KEY_F6)
{
// F6 -- Toggle osgOQ testing.
_enable = !_enable;
EnableQueryVisitor eqv( _enable );
_node.accept( eqv );
return true;
}
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_F7)
{
// F7 -- Toggle display of OQ test bounding volumes
_debug = !_debug;
DebugDisplayVisitor ddv( _debug );
_node.accept( ddv );
return true;
}
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_F8)
{
// F8 -- Gether stats and display
StatisticsVisitor sv;
_node.accept( sv );
std::cout << "osgOQ: Stats: numOQNs " << sv.getNumOQNs() << ", numPased " << sv.getNumPassed() << std::endl;
return true;
}
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_F9)
{
// F9 -- Remove all OcclusionQueryNodes
RemoveOcclusionQueryVisitor roqv;
_node.accept( roqv );
return true;
}
else if (ea.getKey()=='o')
{
if (osgDB::writeNodeFile( _node, "saved_model.osgt" ))
osg::notify( osg::ALWAYS ) << "osgOQ: Wrote scene graph to \"saved_model.osgt\"" << std::endl;
else
osg::notify( osg::ALWAYS ) << "osgOQ: Wrote failed for \"saved_model.osgt\"" << std::endl;
return true;
}
return false;
}
default:
break;
}
return false;
}
osg::Node& _node;
bool _enable, _debug;
};
// Create a cube with one side missing. This makes a great simple occluder.
osg::ref_ptr<osg::Node>
createBox()
{
osg::ref_ptr<osg::Geode> 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<deprecated_osg::Geometry> geom = new deprecated_osg::Geometry;
osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
geom->setVertexArray( v.get() );
{
const float x( 0.f );
const float y( 0.f );
const float z( 0.f );
const float r( 1.1f );
v->push_back( osg::Vec3( x-r, y-r, z-r ) ); //left -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 ) );
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 ) );
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 ) );
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 ) );
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 ) );
}
osg::ref_ptr<osg::Vec4Array> 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 ) );
osg::ref_ptr<osg::Vec3Array> 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 ) );
geom->addPrimitiveSet( new osg::DrawArrays( GL_QUADS, 0, 20 ) );
box->addDrawable( geom.get() );
return box.get();
}
// Make a Geometry that renders slow intentionally.
// To make sure it renders slow, we do the following:
// * Disable display lists
// * Force glBegin/glEnd slow path
// * Lots of vertices and color data per vertex
// * No vertex sharing
// * Draw the triangles as wireframe
osg::ref_ptr<osg::Node>
createRandomTriangles( unsigned int num )
{
osg::ref_ptr<osg::Geode> 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::ref_ptr<deprecated_osg::Geometry> geom = new deprecated_osg::Geometry;
// Disable display lists to decrease performance.
geom->setUseDisplayList( false );
osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
geom->setVertexArray( v.get() );
v->resize( num*3 );
unsigned int i;
srand( 0 );
#define RAND_NEG1_TO_1 ( ((rand()%20)-10)*.1 )
for (i=0; i<num; i++)
{
osg::Vec3& v0 = (*v)[ i*3+0 ];
osg::Vec3& v1 = (*v)[ i*3+1 ];
osg::Vec3& v2 = (*v)[ i*3+2 ];
v0 = osg::Vec3( RAND_NEG1_TO_1, RAND_NEG1_TO_1, RAND_NEG1_TO_1 );
v1 = osg::Vec3( RAND_NEG1_TO_1, RAND_NEG1_TO_1, RAND_NEG1_TO_1 );
v2 = osg::Vec3( RAND_NEG1_TO_1, RAND_NEG1_TO_1, RAND_NEG1_TO_1 );
}
osg::ref_ptr<osg::Vec4Array> 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; i<num; i++)
{
osg::Vec4& c0 = (*c)[ i ];
c0 = osg::Vec4( RAND_0_TO_1, RAND_0_TO_1, RAND_0_TO_1, 1. );
}
geom->addPrimitiveSet( new osg::DrawArrays( GL_TRIANGLES, 0, num*3 ) );
tris->addDrawable( geom.get() );
return tris.get();
}
// Create the stock scene:
// Top level Group
// Geode (simple occluder
// OcclusionQueryNode
// Geode with complex, slow geometry.
osg::ref_ptr<osg::Node>
createStockScene()
{
// Create a simple box occluder
osg::ref_ptr<osg::Group> root = new osg::Group();
root->addChild( createBox().get() );
// Create a complex mess of triangles as a child below an
// OcclusionQueryNode. The OQN will ensure that the
// subgraph isn't rendered when it's not visible.
osg::ref_ptr<osg::OcclusionQueryNode> oqn = new osg::OcclusionQueryNode;
oqn->addChild( createRandomTriangles( 20000 ).get() );
root->addChild( oqn.get() );
return root.get();
}
int main(int argc, char** argv)
{
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc,argv);
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" demonstrates OpenGL occlusion query in OSG using the OcclusionQueryNode.");
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] [filename(s)]");
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line parameters");
// if user request help write it out to cout.
if (arguments.read("-h") || arguments.read("--help"))
{
arguments.getApplicationUsage()->write(std::cout, osg::ApplicationUsage::COMMAND_LINE_OPTION);
return 1;
}
// report any errors if they have occurred when parsing the program arguments.
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return 1;
}
osgViewer::Viewer viewer( arguments );
// add the state manipulator
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
// add the stats handler
viewer.addEventHandler(new osgViewer::StatsHandler);
// add the help handler
viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
bool optimize = arguments.read( "--opt" );
// load the specified model
osg::ref_ptr<osg::Node> root = 0;
if (arguments.argc()>1)
{
root = osgDB::readNodeFiles( arguments );
if (root.valid())
{
// Run a NodeVisitor to insert OcclusionQueryNodes in the scene graph.
OcclusionQueryVisitor oqv;
root->accept( oqv );
}
else
{
std::cout << arguments.getApplicationName() <<": unable to load specified data." << std::endl;
return 1;
}
}
else
{
root = createStockScene().get();
if (!root)
{
std::cout << arguments.getApplicationName() <<": Failed to create stock scene." << std::endl;
return 1;
}
}
// any option left unread are converted into errors to write out later.
arguments.reportRemainingOptionsAsUnrecognized();
// report any errors if they have occurred when parsing the program arguments.
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return 1;
}
// optimize the scene graph, remove redundant nodes and state etc.
if (optimize)
{
osgUtil::Optimizer optimizer;
optimizer.optimize( root.get() );
}
viewer.setSceneData( root.get() );
KeyHandler* kh = new KeyHandler( *root );
viewer.addEventHandler( kh );
return viewer.run();
}
|