hi, i'm new to OSG so i started with reading the Quick-Start-Guide.
In the last example of the book (Picking) heap memory is allocated in a
memberfunction which is being called in case of a certain mouseevent. A normal
pointer (picker) and not a smartpointer is used to reference it - wouldn't
this yield to a memory leak, because, at least, in application code i can't see
any delete picker statement?
Code:
// Perform a pick operation.
bool pick( const double x, const double y,
osgViewer::Viewer* viewer )
{
if (!viewer-getSceneData())
// Nothing to pick.
return false;
double w( .05 ), h( .05 );
osgUtil::PolytopeIntersector* picker =
new osgUtil::PolytopeIntersector(
osgUtil::Intersector::PROJECTION,
x-w, y-h, x+w, y+h );
osgUtil::IntersectionVisitor iv( picker );
viewer-getCamera()-accept( iv );
...
Another question i have is:
Why is it even necessary to create the PolytopeIntersector at heap and not
simply at the stack of the pick memberfunction, like done with the
IntersectionVisitor? As far as i understand this example, none object inside
the memberfunction has to store any value/state/node/etc. till a possibly next
call, all work is done within the call and the return value is only TRUE/FALSE;
so the memory cleanup could be done implicit with leaving the scope of the
memberfunction.
Thanks in advance,
christian
--
Read this topic online here:
http://osgforum.tevs.eu/viewtopic.php?p=6733#6733
//
// OpenSceneGraph Quick Start Guide
// http://www.lulu.com/content/767629
//
http://www.openscenegraph.com/osgwiki/pmwiki.php/Documentation/QuickStartGuide
//
// Picking Example, Using the osgUtil Intersection classes and osgGA NodeKit
// Code derived from an OSG example. Original comment block follows.
// C++ source file - (C) 2003 Robert Osfield, released under the OSGPL.
//
// Simple example of use of osgViewer::GraphicsWindow + SimpleViewer
// example that provides the user with control over view position with basic
picking.
#include osgDB/ReadFile
#include osgViewer/Viewer
#include osgUtil/PolytopeIntersector
#include osg/Camera
#include osg/NodeCallback
#include osg/Group
#include osg/MatrixTransform
#include iostream
#include osg/Notify
osg::ref_ptrosg::Node _selectedNode;
// Derive a class from NodeCallback to manipulate a
// MatrixTransform object's matrix.
class RotateCB : public osg::NodeCallback
{
public:
RotateCB() : _angle( 0. ) {}
virtual void operator()( osg::Node* node,
osg::NodeVisitor* nv )
{
// Normally, check to make sure we have an update
// visitor, not necessary in this simple example.
osg::MatrixTransform* mt =
dynamic_castosg::MatrixTransform*( node );
osg::Matrix m;
m.makeRotate( _angle, osg::Vec3( 0., 0., 1. ) );
mt-setMatrix( m );
// Increment the angle for the next from.
_angle += 0.01;
// Continue traversing so that OSG can process
// any other nodes with callbacks.
traverse( node, nv );
}
protected:
double _angle;
};
// Create the scene graph. This is a Group root node with two
// MatrixTransform children, which multiply parent a single
// Geode loaded from the cow.osg model file.
osg::ref_ptrosg::Node
createScene()
{
// Load the cow model.
osg::ref_ptrosg::Node cow = osgDB::readNodeFile( cow.osg );
if (!cow.valid())
{
osg::notify( osg::FATAL ) Unable to load data file. Exiting.
std::endl;
return NULL;
}
// Data variance is STATIC because we won't modify it.
cow-setDataVariance( osg::Object::STATIC );
// Create a MatrixTransform to display the cow on the left.
osg::ref_ptrosg::MatrixTransform mtLeft =
new osg::MatrixTransform;
mtLeft-setName( Left Cow );
mtLeft-setDataVariance( osg::Object::STATIC );
osg::Matrix m;
m.makeTranslate( -6.f, 0.f, 0.f );
mtLeft-setMatrix( m );
osg::ref_ptrosg::MatrixTransform mt =
new osg::MatrixTransform;
mt-setName( Left Rotation );
mt-setDataVariance( osg::Object::STATIC );
m.makeIdentity();
mt-setMatrix( m );
mtLeft-addChild( mt.get() );
mt-addChild( cow.get() );
// Create a MatrixTransform to display the cow on the right.
osg::ref_ptrosg::MatrixTransform mtRight =
new osg::MatrixTransform;
mtRight-setName( Right Cow );
mtRight-setDataVariance( osg::Object::STATIC );
m.makeTranslate( 6.f, 0.f, 0.f );
mtRight-setMatrix( m );
mt = new osg::MatrixTransform;
mt-setName( Right Rotation );
mt-setDataVariance( osg::Object::STATIC );
m.makeIdentity();
mt-setMatrix( m );
mtRight-addChild( mt.get() );
mt-addChild( cow.get() );
// Create the Group root node.
osg::ref_ptrosg::Group root = new osg::Group;