Sunday, March 19, 2017

Rendering Basic Shapes


OpenSceneGraph provide an osg::ShapeDrawable class to render basic geometry shapes quickly with plain parameters. An osg::ShapeDrawable instance always includes an osg::Shape object to indicate the specified geometry's type and properties.

The most frequently used basic shapes defined in OSG are osg::Box, osg::Capsule, osg::Cone,  osg::Cylinder, and osg::Sphere. Their appearances can be well defined by passing parameters directly to constructors.

It is easy to create simple objects by using an osg::Shape subclass. We will take three typical shapes as examples: a box with different width, height, and depth values, a sphere with a radius value, and a cone with a radius and a height.

Please open your best IDE, If you need guidance on how to set the OSG environment, for CodeBlock instruction can be found here and for Visual Studio can be found here and for Netbean can be found here.

Please create new project and copy paste this code.

This tutorial was implemented on OpenSuse 42.2, OpenSceneGraph-3.0.1, Netbean C++.

#include <osg/ShapeDrawable>
#include <osg/Geode>
#include <osgViewer/Viewer>

using namespace std;

int main(int argc, char** argv) {

    osg::ref_ptr<osg::ShapeDrawable> shape1 = new osg::ShapeDrawable;
    shape1->setShape( new osg::Box(osg::Vec3(-3.0f, 0.0f, 0.0f), 2.0f, 2.0f, 1.0f) );
  
    osg::ref_ptr<osg::ShapeDrawable> shape2 = new osg::ShapeDrawable;
    shape2->setShape( new osg::Sphere(osg::Vec3(3.0f, 0.0f, 0.0f), 1.0f) );
    shape2->setColor( osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) );
  
    osg::ref_ptr<osg::ShapeDrawable> shape3 = new osg::ShapeDrawable;
    shape3->setShape( new osg::Cone(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f, 1.0f) );
    shape3->setColor( osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) );
  
    osg::ref_ptr<osg::Geode> root = new osg::Geode;
    root->addDrawable( shape1.get() );
    root->addDrawable( shape2.get() );
    root->addDrawable( shape3.get() );
  
    osgViewer::Viewer viewer;
    viewer.setSceneData( root.get() );
  
    return viewer.run();
}

Finally, if you are successful then you will get something like this





Other Topics:




No comments:

Post a Comment