#include "osgMeshViewer.h" #include #include #include #include #include #include #include /** * Creates a series of 3D points for visualization in OpenSceneGraph. * */ osg::Node* getOSGPointsScene(std::vector points3D) { // create a Geode to be returned osg::Geode* pointsGeode = new osg::Geode; // create OSG geometry for 3D points osg::Geometry* pointsGeom = new osg::Geometry(); pointsGeode->addDrawable( pointsGeom ); // TODO set color or normal??? // create a Vec3Array for the 3D points osg::Vec3Array* osgPoints = new osg::Vec3Array(); for(int i = 0; i < points3D.size(); i++) { osgPoints->push_back( osg::Vec3(points3D[i].x, points3D[i].y, points3D[i].z) ); } // set blue color to the geometry osg::Vec4Array* osgColors = new osg::Vec4Array(); osgColors->push_back( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); // set default normal //osg::Vec3Array* osgNormals = new osg::Vec3Array(); //osgNormals->push_back( osg::Vec3(0.0f, -1.0f, 0.0f) ); // configure points geometry pointsGeom->setColorArray(osgColors); pointsGeom->setColorBinding(osg::Geometry::BIND_OVERALL); //pointsGeom->setNormalArray(osgNormals); //pointsGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); pointsGeom->setVertexArray(osgPoints); osg::PrimitiveSet *primSet = new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, osgPoints->size()); pointsGeom->addPrimitiveSet( primSet ); return pointsGeode; } osg::Node* createOSGScene(std::vector points3D) { osg::Group* scene = new osg::Group(); osg::Node* geode; /* TODO substitute when program is complete // add a bunny to the scene geode = getOSGBunny(); */ geode = getOSGPointsScene( points3D ); scene->addChild( geode ); return scene; } void viewOSGScene(osg::Node* scene) { // create a viewer and attach created scene to it osgViewer::Viewer viewer; viewer.setSceneData( scene ); // attach a trackball manipulator to user control of the view viewer.setCameraManipulator( new osgGA::TrackballManipulator ); // create the window and start the required threads viewer.realize(); // Enter the simulation loop. // viewer.done() returns false when user press the 'esc' key. // This can be changed by adding your own keyboard/mouse event // handler or by changing the settings of the default keyboard/ // mouse event handler //while ( true ) while ( !viewer.done() ) { // Dispatch the new frame. // This wraps up the following Viewer operations: // advance() to the new frame // eventTraversal() that collects events and passes them on // to the event handlers and event callbacks // updateTraversal() to call the update callbacks // renderingTraversals() that runs syncronizes all the // rendering threads (if any) and dispatch cull, draw and // swap buffers viewer.frame(); } }