Insert Drawable to osgWidget

29 views
Skip to first unread message

marco....@elmansrl.eu

unread,
May 3, 2021, 10:08:31 AM5/3/21
to OpenSceneGraph Users
Hi guys,

I need to insert drawables like lines, polygons to the viewer.

Then, the idea it's to create an osgWidget::Box and insert through the method  getGeode()->addDrawable a simple drawable...

#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgWidget/WindowManager>
#include <osgWidget/Box>
#include <osgWidget/Canvas>
#include <osgWidget/Label>
#include <osgWidget/ViewerEventHandlers>
#include <iostream>
#include <sstream>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Material>
#include <osg/Vec3>
#include <osg/MatrixTransform>
#include <osg/Texture2D>
#include <osg/PolygonStipple>
#include <osg/TemplatePrimitiveFunctor>
#include <osg/TemplatePrimitiveIndexFunctor>
#include <osg/io_utils>

#include <osgDB/ReadFile>
#include <osgDB/WriteFile>

#include <osgGA/TrackballManipulator>

#include <osgViewer/Viewer>

#include <osg/Math>

#include <iostream>

using namespace std;

///*Crea la label*/
osgWidget::Label* createLabel(const std::string& name, const std::string& text, float size, const osg::Vec4& color)
{
osg::ref_ptr<osgWidget::Label> label = new osgWidget::Label(name);
label->setLabel(text);
label->setFont("fonts/arial.ttf");
label->setFontSize(size);
label->setFontColor(1.0f, 1.0f, 1.0f, 1.0f);
label->setColor(color);
label->addSize(10.0f, 10.0f);
label->setCanFill(true);
return label.release();
}


osg::Geometry* createScene()
{
osg::Geometry* linesGeom = new osg::Geometry();

osg::Vec3Array* vertices = new osg::Vec3Array(8);
(*vertices)[0].set(-1.13704, -2.15188e-09, 0.40373);
(*vertices)[1].set(-0.856897, -2.15188e-09, 0.531441);
(*vertices)[2].set(-0.889855, -2.15188e-09, 0.444927);
(*vertices)[3].set(-0.568518, -2.15188e-09, 0.40373);
(*vertices)[4].set(-1.00933, -2.15188e-09, 0.370773);
(*vertices)[5].set(-0.716827, -2.15188e-09, 0.292498);
(*vertices)[6].set(-1.07936, 9.18133e-09, 0.317217);
(*vertices)[7].set(-0.700348, 9.18133e-09, 0.362533);

linesGeom->setVertexArray(vertices);

osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
linesGeom->setColorArray(colors, osg::Array::BIND_OVERALL);

osg::Vec3Array* normals = new osg::Vec3Array;
normals->push_back(osg::Vec3(0.0f, -1.0f, 0.0f));
linesGeom->setNormalArray(normals, osg::Array::BIND_OVERALL);

linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 8));

return linesGeom;
}



///*Crea il widget TABS*/
osgWidget::Window* createSimpleTabs(float winX, float winY)
{
osg::ref_ptr<osgWidget::Box> mainBox = new osgWidget::Box("main", osgWidget::Box::BoxType::VERTICAL);
mainBox->setOrigin(winX, winY);
mainBox->attachMoveCallback();

mainBox->getBackground()->setColor(1.0f, 1.0f, 1.0f, 1.0f);
mainBox->addWidget(createLabel("title", "Tabs Demo", 15.0f, osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f)));
mainBox->addWidget(createLabel("title", "TEST", 40.0f, osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f)));
if (mainBox->getGeode()!=nullptr) {

osg::Geometry* line = createScene();

mainBox->getGeode()->addDrawable(line);
cout << mainBox->getGeode()->containsDrawable(line) << endl;
}
return mainBox.release();
}

int main(int argc, char** argv)
{
osgViewer::Viewer viewer;
osg::ref_ptr<osgWidget::WindowManager> wm = new osgWidget::WindowManager(&viewer, 1024.0f, 768.0f, 0xf0000000);
osg::Camera* camera = wm->createParentOrthoCamera();

wm->addChild(createSimpleTabs(0.0f, 0.0f));
wm->resizeAllWindows();

osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(camera);

viewer.setSceneData(root.get());
viewer.setUpViewInWindow(50, 50, 1024, 768);
viewer.addEventHandler(new osgWidget::MouseHandler(wm.get()));
viewer.addEventHandler(new osgWidget::KeyboardHandler(wm.get()));
viewer.addEventHandler(new osgWidget::ResizeHandler(wm.get(), camera));
viewer.addEventHandler(new osgWidget::CameraSwitchHandler(wm.get(), camera));
return viewer.run();
}

What I get is to have the two labels in the hbox .. but I can't see the line ...

What am I doing wrong?
Reply all
Reply to author
Forward
0 new messages