Craig, Joel S. (SED/TMI) schrieb:
Classification: UNCLASSIFIED
Caveats: NONE
Greetings...
In my simulation, I have a moving object that I would like to show its path
as it moves through the scene. The original programmer created an osg::Node
each time the object moved using the previous and current locations as the
points of the geometry. This approach worked fairly well, but the LOD
culling made parts of the line disappear at various zoom levels.
It struck me that it would be better to have one "movementTrail" osg::Node
that contained the list of points where the object moved. Each time the
object changed position, the geometry of the "movementTrail" could be
extended by the current position of the object.
I researched the e-mail archives and other documentation and came up with
the code shown below. With this code, the "movementTrail" is not shown at
all. Can someone tell me what I've omitted?
Thanks for your help,
Joel Craig
When we have two positions (current and previous) of the moving object, I
call...
osg::Geode* drawLine(osg::Vec3& start, osg::Vec3& end, const osg::Vec4&
color)
{
osg::ref_ptr<osg::Vec3Array> verts = new osg::Vec3Array;
verts->push_back(start);
verts->push_back(end);
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
colors->push_back(color);
osg::Geometry* lineNode = new osg::Geometry();
lineNode->setName("linesNode");
lineNode->setVertexArray(verts.get());
lineNode->setColorArray(colors.get());
lineNode->setColorBinding(osg::Geometry::BIND_OVERALL);
lineNode->addPrimitiveSet(new
osg::DrawArrays(osg::PrimitiveSet::LINES,0,verts->size());
lineNode->setUseDisplayList(false); // added this call when I added
extendLine function(below)
osg::StateSet* stateSet = new osg::StateSet();
stateSet->setMode(GL_LIGHTING,osg::StateAttribute::OFF|osg::StateAttribute::
OVERRIDE);
stateSet->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OFF|osg::State
Attribute::OVERRIDE);
osg::Geode* geode = new osg::Geode;
geode->addDrawable(lineNode);
geode->setStateSet(stateSet);
return geode;
}
The calling function adds this geode to the scenegraph with
"rootGroup->addChild(geode)" and saves it in an array to be later retrieved.
If I call drawLine each time the object moves, then I get a "movementTrail"
made of many very short lines. The trail is displayed, but parts of it will
appear or disappear depending on the zoom level.
I added the extendLine function, below. When there are two positions, the
drawLine function is called and the Geode is saved. When the object moves
again, we extend the line with a call to extendLine passing the saved Geode
and the new position.
void extendLine(osg::Node** nodePtr, float x, float y, float z)
{
osg::Geode* nodeToExtend = (osg::Geode*) *nodePtr;
osg::Vec3 newEnd = osg::Vec3(x,y,z);
osg::ref_ptr<osg::Vec3Array> verts;
// We've only added one drawable on this node as it represents the
movementTrail
osg::Geometry* lineNode = dynamic_cast<osg::Geometry*>
(nodeToExtend->getDrawable(0));
verts = (osg::Vec3Array*) lineNode->getVertexArray();
verts->push_back(newEnd);
verts->dirty(); // do I need to do this or is dirtyBound sufficient?
lineNode->setVertexArray(verts.get());
lineNode->dirtyBound(); // do I need this? It seems to be called from
setVertexArray.
}
The number of vertices in the vertex array grows with each movement, but the
trail is not displayed at any zoom level. Should I use a different function
to add a coordinate to the end of the vertex array?
Hi,
in a case like this I just created a new drawable and replaced the old
one in the Geometry with the new one.
However, maybe this :
computeCorrectBindingsAndArraySizes (
)
called on your drawable would help, because obviously the drawable isn´t
updated.
You don´t use ref_ptr´s , and at least the first version of your code
will produce tremendous memory-leaks when running for some time.
Regards,
Andreas
_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/