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.");
}