[osg-users] Create BoundingBoxes dynamically, by creating global Geode and attaching children later

Martin Steinborn bmmartint.stb at web.de
Wed Jan 15 00:34:21 PST 2020


Hello,

I want to display my own BoundingBoxes (BBs) using osg. The way I am trying 
to do it is the following:
1) create a Geode and set it as Child of the overall scene. Create a global 
pointer to be able to add children to this Geode.
2) for every BoundingBox I find, create a new Geometry with a LINE_LOOP 
PrimitiveSet and add the draw-able as Child to the geode
3) after every iteration, multiple BBs could be generated and need to be 
deleted, because the objects in the BBs or the camera may have moved

I managed to show one BB in my scene by directly creating the Geode and the 
Box as child, but I cant get it working, using the above described method. 
I think that the Problem is with rerendering the previously created Geode, 
but I may be wrong. The other think, that may prevent stuff from rendering, 
is that I delete the BBs in the next "iteration".

Scene creation and Vertex positions are "correct", I've used the same code 
in my working example where I created Geode and DrawArray at once.

Here is a part of my Code, ill try to add some explanation:

Visual.h
...
/** Create scene object for all the Bounding Boxes to be drawn into **/
osg::ref_ptr<osg::Geode> myGeode;
...

--------------------------------------------------
Visual.cpp

// called on plug-in load
void Visual::load()
{
    ...
    createBoundingBoxScene();
}

// called with 60fps
void Visual::update()
{
// remove old BBs and create new one
myGeode->removeDrawables(0, myGeode->getNumDrawables());
     createBoundingBox();
}

/*
* \brief   Visual create BoundingBox as Child to Geode
* creates a BoundingBox via DrawArray and lines over (later given) 
vertices, and adds them as child to the global Geode
*/
void Visual::createBoundingBox()
{
osg::Geometry* geo = new osg::Geometry;
osg::ref_ptr<osg::Vec3dArray> myBBPoints = new osg::Vec3dArray; // array of 
BB cordinates
osg::ref_ptr<osg::Vec4dArray> color = new osg::Vec4dArray;
color->push_back(osg::Vec4d(1.0, 0.0, 0.0, 1.0)); // create color: red
osg::DrawArrays* da = new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, 0, 
myBBPoints->size()); // LINE_LOOP, because BBs are a loop for sure

myBBPoints->push_back(osg::Vec3d(754.0, 3860.0, 11.0)); // 4 corner 
vertices, TODO: if working, pass Global Points as parameters in function
myBBPoints->push_back(osg::Vec3d(760.0, 3860.0, 11.0));
myBBPoints->push_back(osg::Vec3d(760.0, 3860.0, 13.0));
myBBPoints->push_back(osg::Vec3d(754.0, 3860.0, 13.0));

geo->setVertexArray(myBBPoints);
geo->addPrimitiveSet(da);
geo->setColorArray(color);
geo->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE);
geo->setDataVariance(osg::Object::DYNAMIC);

        // add geometry to geode to be drawn
myGeode->addDrawable(geo);

        // force scene / Geode to be redrawn ? all necessary, all useless?
geo->dirtyDisplayList();
geo->dirtyBound();
da->dirty();
myGeode->dirtyBound();
}

/*
* \brief   Visual create global Geode as child to scene
* get 3D scene of environment, create a (global) geode and add it as child 
to scene.
*/

void Visual::createBoundingBoxScene()
{
osg::Group * scene = 
findScene3D(dynamic_cast<osg::Group*>(myGraphicalContext->getScene())); 
//works
if (scene)
{
myGeode = new osg::Geode;
myGeode->setDataVariance(osg::Object::DYNAMIC);
myGeode->setName("Geode");
osg::MatrixTransform* mat = new osg::MatrixTransform;
mat->setReferenceFrame(osg::Transform::ABSOLUTE_RF); // (?) use global 
coordinate system, not local
osg::StateSet *stateset = myGeode->getOrCreateStateSet();

stateset->setMode(GL_LIGHTING, osg::StateAttribute::ON);
           stateset->setRenderBinDetails(INT_MAX, "RenderBin"); // make 
sure to print at last possible time

                // add global Geode as child to scene
scene->addChild(mat);
mat->addChild(myGeode);

std::cout << "Scene created successfully, name: " << scene->getName() << " 
with " << scene->getNumChildren() << " children." << std::endl;
}
else mySimulationContext->printErrorMessage("Failed to get scene node.");
}

I hope the code is not too long, but because I'm new to osg, I dont know 
what is important and what not.

Have a nice Day and thanks for your responses

-- 
You received this message because you are subscribed to the Google Groups "OpenSceneGraph Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to osg-users+unsubscribe at googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/osg-users/b2866ce9-f4c2-4395-bf2a-06db06d84ed5%40googlegroups.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20200115/4e8422d0/attachment.html>


More information about the osg-users mailing list