1. Coin3D
  2. Coin

Wiki

Clone wiki

Coin / A simple 3D viewer with Coin and SoWin

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.

1. Set Up Visual Studio
Start Visual Studio and choose File | New | Project

msvc8_file_new_project.png

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

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.

msvc8_app_wizard2.png

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

msvc8_hello_cone_properties.png

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

msvc8_add_include_dir.png

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

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

msvc8_add_lib_dir.png

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

msvc8_add_lib_files.png

2. 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.

msvc8_new_cpp_file.png

First we need to include the necessary headerfiles:
#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;
}
First we initialize SoWin (and implicitly also 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;

3. Build and Run the Application

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

hello_cone.png

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;
}

Updated