Skip to content

A simple 3D viewer with Coin and SoWin

Volker Enderlein edited this page Dec 25, 2019 · 1 revision

You are here: HomeDocumentationWindows information pageTutorials → A simple 3D viewer with Coin and SoWin

A simple 3D viewer with Coin and SoWin

This tutorial guides you through writing a simple 3D viewer using Coin and SoWin with Microsoft Visual Studio 2003/2005. If you use Visual Studio 6, the setup is slightly different. Before you start, make sure you have a working Coin installation on your system.

Set Up Visual Studio

Start Visual Studio and choose File | New | Project

Choose Win32 as the project type, and Win32 Console Application as the Template. Name the project "hello_cone"

In the Win32 Application Wizard, click Next and check the Empty project checkbox, since we want to supply all the source files ourselves. Click Finish. You now have a new project called hello_cone.

Before we can start hacking away, we need to set the project up to use Coin and SoWin. This is done in the hello_cone properties

Choose Configuration Properties | C/C++ | General and add $(COINDIR)\include as an additional include directory

Choose Configuration Properties | C/C++ | Preprocessor and add COIN_DLL and SOWIN_DLL

Choose Configuration Properties | Linker | General and add $(COINDIR)\lib as an additional library directory

Choose Configuration Properties | Linker | Input and add coin3d.lib and sowin1d.lib as Additional Dependencies

Start Hacking

Now we're almost ready to start coding. We'll just have to add a C++ source file to the project first. Choose File | Add New Item and select C++ File (.cpp) as the template. Name it hello_cone.cpp and click Add.

First we need to include the necessary header files:

#include <Inventor/Win/SoWin.h>
#include <Inventor/Win/viewers/SoWinExaminerViewer.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoCone.h>

We are going to put all the necessary code in the main function.

int
main(int, char ** argv)
{
  //[...]
  return 0;
}

In the main function we first initialize SoWin (and implicitly Coin), this will return a top-level / shell window to use.

HWND window = SoWin::init(argv[0]);
if (window==NULL) exit(1);

Then we create a viewer:

SoWinExaminerViewer * viewer = new SoWinExaminerViewer(window);

Our scenegraph is going to consist of two nodes: the root and a geometry object. First we create the two nodes and then we add the geometry node as a child to the root node. To avoid that the root object gets deleted (because its reference counter is 0) we have to call ref() on the root node.

SoSeparator * root = new SoSeparator;
SoCone * cone = new SoCone;

root->ref();
root->addChild(cone);

Now we tell the viewer to render the scenegraph (which is represented through the root node):

viewer->setSceneGraph(root);
viewer->show();

Finally we tell SoWin to show the window and run in a loop.

SoWin::show(window);
SoWin::mainLoop();

When the application is terminated, the viewer is deleted and we have to call unref() on the root node.

root->unref();
delete viewer;

Build and Run the Application

That's it, you're all set! Now you can compile and run the hello_cone application: Choose Build | Build Solution and then run it.

This viewer provides basic interaction, click into the window and try to move the camera.

Source Code to Example

#include <Inventor/Win/SoWin.h>
#include <Inventor/Win/viewers/SoWinExaminerViewer.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoCone.h>

int
main(int, char ** argv)
{
  HWND window = SoWin::init(argv[0]);
  if (window==NULL) exit(1);

  SoWinExaminerViewer * viewer = new SoWinExaminerViewer(window);


  SoSeparator * root = new SoSeparator;
  SoCone * cone = new SoCone;

  root->ref();
  root->addChild(cone);


  viewer->setSceneGraph(root);
  viewer->show();

  SoWin::show(window);
  SoWin::mainLoop();
  delete viewer;
  root->unref();
  return 0;
}
Clone this wiki locally