Getting Started with the osgDotNet Wrappers

Download and Setup

Download either the binary wrapper assemblies, or the code generator and build your own assemblies. Copy the assemblies to a directory on your disk.

C# Project Setup

  1. Create or open a C# project in Visual Studio 2005.
  2. Add an x86 or x64 platform configuration to your C# project via Build > Configuration Manager... and make that the active platform.
  3. In the C# project, add references to the the osgDotNet wrapper assemblies you will be using by browsing to the directory where you copied them.
  4. Copy the Release DLLs for the OpenSceneGraph libraries and plugins you will be using as well as all of their dependency DLLs to the directory containing the C# executable. (The OpenSceneGraph libraries are not provided by osgDotNet, you must supply them yourself.) Wrapper assembly DLLs will get copied automatically via the project reference configuration and do not need to be copied manually.

Viewer Setup

The following code can be used to test the setup/configuration by rendering a simple scene consisting of a lit colored quad.

Note the use of the using statement -- this ensures that the viewer is created and destroyed in the same thread which is required by Win32.

class Program
{
    static void Main(string[] args)
    {
        using (OsgViewer.Viewer viewer = new OsgViewer.Viewer())
        {
            Osg.Geode geode = new Osg.Geode();
            Osg.Geometry geometry = new Osg.Geometry();
            Osg.Vec3Array vertices = new Osg.Vec3Array();
            Osg.Vec3Array normals = new Osg.Vec3Array();
            Osg.Vec3Array colors = new Osg.Vec3Array();
            Osg.DrawArrays drawArrays =
                new Osg.DrawArrays((uint)Osg.DrawArrays.Mode.QUADS, 0, 4);

            vertices.Add(new Osg.Vec3(-1.0f, 0.0f, -1.0f));
            vertices.Add(new Osg.Vec3(1.0f, 0.0f, -1.0f));
            vertices.Add(new Osg.Vec3(1.0f, 0.0f, 1.0f));
            vertices.Add(new Osg.Vec3(-1.0f, 0.0f, 1.0f));

            normals.Add(new Osg.Vec3(0.0f, -1.0f, 0.0f));
            normals.Add(new Osg.Vec3(0.0f, -1.0f, 0.0f));
            normals.Add(new Osg.Vec3(0.0f, -1.0f, 0.0f));
            normals.Add(new Osg.Vec3(0.0f, -1.0f, 0.0f));

            colors.Add(new Osg.Vec3(1.0f, 0.0f, 0.0f));
            colors.Add(new Osg.Vec3(0.0f, 1.0f, 0.0f));
            colors.Add(new Osg.Vec3(0.0f, 0.0f, 1.0f));
            colors.Add(new Osg.Vec3(1.0f, 1.0f, 0.0f));

            geometry.addPrimitiveSet(drawArrays);
            geometry.setVertexArray(vertices);
            geometry.setNormalArray(normals);
            geometry.setNormalBinding(Osg.Geometry.AttributeBinding.BIND_PER_VERTEX);
            geometry.setColorArray(colors);
            geometry.setColorBinding(Osg.Geometry.AttributeBinding.BIND_PER_VERTEX);
            geode.addDrawable(geometry);

            viewer.setSceneData(geode);
            viewer.run();
        }
    }
}

Developing Against the osgDotNet Wrappers

For the most part the osgDotNet wrappers maintain fidelity to the OpenSceneGraph C++ API; its documentation, in conjunction with Intellisense, should be used as a guide to the interfaces. Extension of the interfaces via subclassing is also supported. However, some differences between the two were either desirable or unavoidable. The main DifferencesWithNativeAPI relate to divergence in namespace names, reference/value semantics, enum usage, protected and operator functions, public member access, and divergence to support more idiomatic CLR usage.