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









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


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


