1 // OpenSG Tutorial Example: Hello World
3 // Minimalistic OpenSG program
5 // This is the shortest useful OpenSG program
6 // (if you remove all the comments ;)
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
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>
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>
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
)
54 OSG::osgInit(argc
,argv
);
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
);
69 OSG::NodeRefPtr scene
= OSG::makeTorus(.5, 2, 16, 16);
73 // create the SimpleSceneManager helper
74 mgr
= OSG::SimpleSceneManager::create();
76 // tell the manager what to manage
77 mgr
->setWindow(gwin
);
80 // show the whole scene
91 // GLUT callback functions
100 // react to size changes
101 void reshape(int w
, int h
)
107 // react to mouse button presses
108 void mouse(int button
, int state
, int x
, int y
)
111 mgr
->mouseButtonRelease(button
, x
, y
);
113 mgr
->mouseButtonPress(button
, x
, y
);
118 // react to mouse motions with pressed buttons
119 void motion(int x
, int y
)
121 mgr
->mouseMove(x
, y
);
126 void keyboard(unsigned char k
, int x
, int y
)
132 // clean up global variables
142 mgr
->setStatistics(!mgr
->getStatistics());
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
);