This page contains useful code snippets for OpenSceneGraph and osgART. These are not full tutorials, but rather pieces of regularly-used or specialised code that can be copied into your projects.

int main( int argc, char **argv )
{
    // Use an ArgumentParser object to manage the program arguments
    osg::ArgumentParser arguments(&argc, argv);

    std::string stringArgument = "defaultValue";
    while(arguments.read("-myStringArgument", stringArgument));

    double doubleArgument = 0.0f;
    while(arguments.read("-myDoubleArgument", doubleArgument));

    // And so on for other data types...

    // Rest of main...

}

A Head Up Display, or HUD, is a 2D user interface overlaid on the main 3D view. A HUD is traditionally used of status information, icons, buttons and so on. Because a HUD is typically 2D rather than 3D, it makes sense to render it using an orthographic rather than perspective projection. In OpenSceneGraph, the easiest way to do this is to use a new osg::Camera. The camera is configured to have: [Read More]

: Documentation : Code Snippets A Node Callback is a piece of code that is run each frame. You can create a custom callback for any node you add to the scene graph, and it is the recommended place to perform updates. To create a callback, simply subclass osg::NodeCallback and override the () operator. classMyUpdateCallback : public osg::NodeCallback { virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) { // Add custom code here. [Read More]

: Documentation : Code Snippets

OpenSceneGraph has a built in mechanism for debugging, using various notification levels.

Possible levels for message notifications:

  • ALWAYS - Consume all messages (e.g. for release builds)
  • FATAL
  • WARN
  • NOTICE - Default level
  • INFO
  • DEBUG_INFO
  • DEBUG_FP
// INFO
osg::notify(osg::INFO) << "Your message here" << std::endl;

: Documentation : Code Snippets

In OpenSceneGraph, text objects are osg::Drawable objects, so they need to be added to an osg::Geode in order to be part of the scene graph. However, you can add many text objects to a single geode.

osg::Geode* textGeode = new osg::Geode();
osgText::Text* text = new osgText::Text();
text->setFont("tahoma.ttf");
text->setCharacterSize(16.0f);
text->setPosition(osg::Vec3(4, 4, 0));
text->setAlignment(osgText::Text::LEFT_BASE_LINE);
text->setColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
text->setText("Your text here");
textGeode->addDrawable(text);