fixed: auto_ptr -> unique_ptr
[opensg.git] / Examples / Simple / 01hello.cpp
blob3fd4f2965598982754e8730445b23b0a247ab09f
1 // OpenSG Tutorial Example: Hello World
2 //
3 // Minimalistic OpenSG program
4 //
5 // This is the shortest useful OpenSG program
6 // (if you remove all the comments ;)
7 //
8 // It shows how to use OpenSG together with GLUT to create a little
9 // interactive scene viewer.
12 #ifdef OSG_BUILD_ACTIVE
13 // GLUT is used for window handling
14 #include <OSGGLUT.h>
16 // General OpenSG configuration, needed everywhere
17 #include <OSGConfig.h>
19 // Methods to create simple geometry: boxes, spheres, tori etc.
20 #include <OSGSimpleGeometry.h>
22 // The GLUT-OpenSG connection class
23 #include <OSGGLUTWindow.h>
25 // A little helper to simplify scene management and interaction
26 #include <OSGSimpleSceneManager.h>
27 #else
28 // GLUT is used for window handling
29 #include <OpenSG/OSGGLUT.h>
31 // General OpenSG configuration, needed everywhere
32 #include <OpenSG/OSGConfig.h>
34 // Methods to create simple geometry: boxes, spheres, tori etc.
35 #include <OpenSG/OSGSimpleGeometry.h>
37 // The GLUT-OpenSG connection class
38 #include <OpenSG/OSGGLUTWindow.h>
40 // A little helper to simplify scene management and interaction
41 #include <OpenSG/OSGSimpleSceneManager.h>
42 #endif
44 // The SimpleSceneManager to manage simple applications
45 OSG::SimpleSceneManagerRefPtr mgr;
47 // forward declaration so we can have the interesting stuff upfront
48 int setupGLUT( int *argc, char *argv[] );
50 // Initialize GLUT & OpenSG and set up the scene
51 int main(int argc, char **argv)
53 // OSG init
54 OSG::osgInit(argc,argv);
56 // GLUT init
57 int winid = setupGLUT(&argc, argv);
59 // open a new scope, because the pointers gwin and scene below should
60 // go out of scope before entering glutMainLoop.
61 // Otherwise OpenSG will complain about objects being alive after shutdown.
63 // the connection between GLUT and OpenSG
64 OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create();
65 gwin->setGlutId(winid);
66 gwin->init();
68 // create the scene
69 OSG::NodeRefPtr scene = OSG::makeTorus(.5, 2, 16, 16);
71 OSG::commitChanges();
73 // create the SimpleSceneManager helper
74 mgr = OSG::SimpleSceneManager::create();
76 // tell the manager what to manage
77 mgr->setWindow(gwin );
78 mgr->setRoot (scene);
80 // show the whole scene
81 mgr->showAll();
84 // GLUT main loop
85 glutMainLoop();
87 return 0;
91 // GLUT callback functions
94 // redraw the window
95 void display(void)
97 mgr->redraw();
100 // react to size changes
101 void reshape(int w, int h)
103 mgr->resize(w, h);
104 glutPostRedisplay();
107 // react to mouse button presses
108 void mouse(int button, int state, int x, int y)
110 if (state)
111 mgr->mouseButtonRelease(button, x, y);
112 else
113 mgr->mouseButtonPress(button, x, y);
115 glutPostRedisplay();
118 // react to mouse motions with pressed buttons
119 void motion(int x, int y)
121 mgr->mouseMove(x, y);
122 glutPostRedisplay();
125 // react to keys
126 void keyboard(unsigned char k, int x, int y)
128 switch(k)
130 case 27:
132 // clean up global variables
133 mgr = NULL;
135 OSG::osgExit();
136 exit(0);
138 break;
140 case 's':
142 mgr->setStatistics(!mgr->getStatistics());
144 break;
148 // setup the GLUT library which handles the windows for us
149 int setupGLUT(int *argc, char *argv[])
151 glutInit(argc, argv);
152 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
154 int winid = glutCreateWindow("OpenSG");
156 glutReshapeFunc(reshape);
157 glutDisplayFunc(display);
158 glutMouseFunc(mouse);
159 glutMotionFunc(motion);
160 glutKeyboardFunc(keyboard);
162 return winid;