VR-Forces 4.3 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
exampleOsgSimple

Table of Contents

Overview

This example shows how to create an OpenSceneGraph application. This particular example demonstrates the structure of the scene-graph and the minimum amount of code to start rendering.

Above the OpenGL layer is OpenSceneGraph, an API that provides scene organization functions as well as data management, LOD management, file loaders and other much more. With OSG we organize 3D geometry into a "scene graph", and provides functions for traversing the graph and making the appropriate calls to OpenGL to render the scene.

Example details

The example creates a single piece of geometry for display; a cube. When run, the user can grab, move and spin the cube using the mouse. This tutorial uses a default viewer for rendering the scene and accepting user input. Much of this setup code is hidden from us by the viewer's default settings making this application very lean.

Of particularly interest in this tutorial is the structure of the scene-graph. There are two basic types of objects in the scene-graph; Drawables and Nodes. Drawables hold the primitive/geometry structures, such as vertices, normals, colors, texture-coordinates, etc. Nodes hold the OpenGL states, such lighting, texturing, wire-frame, etc. Derived from Nodes are two basic objects; Groups and Geodes. Groups are containers that hold Nodes. Geodes are containers that hold Drawables. Because Geodes are Nodes, Groups can contain Geodes. The structure of a scene-graph can be thought of as such: Groups make up the branches, Drawables are the leaves, and Geodes are sort-of the 'twig' between the branches and leaves.

In this application we have one Group being the root of the graph, one Geode is made a child of the Group, and one Drawable is made a child of the Geode.

osg::Group* root = new osg::Group();
osg::Drawable* draw =
new osg::ShapeDrawable(
new osg::Box(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f));
osg::Geode* geode = new osg::Geode();
geode->addDrawable(draw);
root->addChild(geode);

To display the scene we create a Viewer and use its default settings. We give the root of our graph to the Viewer and call run.

osgViewer::Viewer viewer;
viewer.setSceneData(root);
viewer.run();

Building the Example

VR-Vantage includes pre-built versions of the example application. To build it yourself, follow the instructions at Building VR-Vantage Examples, Applications, and Plug-ins.

Running the Example

This example is an application. You can run it by running ./bin/exampleOsgSimple.exe (on Windows) or ./bin/exampleOsgSimple (on Linux). For more information about running examples, please see Running Applications and Examples.

Learn More

Example Source Files


exampleOsgSimple.cxx

/******************************************************************************
** Copyright (c) 2014 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
// This particular application demonstrates the structure of the scene-graph
// and the minimum amount of code to start rendering. Of particular
// interest in this example is the structure of the scene-graph. The
// application creates a single piece of geometry for display; a cube.
#include <osg/Drawable>
#include <osg/Group>
#include <osg/Geode>
#include <osg/Light>
#include <osg/ShapeDrawable>
#include <osgViewer/Viewer>
// The main function sets up the scene-graph hierarchy, then creates a viewer
// to render the scene, modifies the global light then starts rendering.
int main()
{
// Create a root Node for the scene.
// Groups are derived from Nodes and they are containers for other Nodes.
osg::Group* root = new osg::Group();
// Create the Drawable geometry for a cube.
// Drawables hold the geometry structures such as vertices, colors,
// normals, etc. Here we're using a utility that creates basic
// shapes to build a Drawable. This is a basic unit-sized cube
// centered around the origin (0, 0, 0), its default color is white.
osg::Drawable* draw =
new osg::ShapeDrawable(
new osg::Box(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f));
// The Drawable geometry is held under a Geode.
// Geodes are Nodes which act as containers for Drawables.
// Drawables are not derived from Nodes, so Geodes are used
// to link the scene graph Node branches to Drawable leaves.
osg::Geode* geode = new osg::Geode();
// Associate the geometry with the Geode.
// Add the Geode to the root Node of the scene graph.
geode->addDrawable(draw);
root->addChild(geode);
// Create the Viewer to display the scene.
osgViewer::Viewer viewer;
viewer.setUpViewInWindow(100, 100, 640, 480);
// To make things slightly interesting, change the color of
// the global light. By default the Viewer has a single directional
// light for the scene. We'll get the light from the viewer and change
// its diffuse color to red.
osg::Light* light = viewer.getLight();
light->setDiffuse( osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) );
// The final step is to enter a simulation loop.
// Add the root of the scene to the viewer and start rendering.
viewer.setSceneData(root);
viewer.run();
return 0;
}

Document ID: Generated on Wed Mar 11 21:20:57 EDT 2015 from SVN revision 150940
Copyright © 2005-2014 VT MÄK. All Rights Reserved (www.mak.com)