changed: auto add updateData callback to stages so that stagedata can be updated...
[opensg.git] / Source / WindowSystem / GLUT / testSimpleSceneManager.cpp
blob9f2f63b7a14eb32babb0523b29f229b9ae9fd95b
1 // OpenSG Tutorial Example: Loading
2 //
3 // This example shows how to load a scene file using OpenSG.
4 // The supported formats right now are VRML97, OBJ, OFF and RAW, so just
5 // calling this program with a scene file as a parameter should load the scene
6 // file.
7 //
9 // Headers
10 #include "OSGGLUT.h"
11 #include "OSGConfig.h"
12 #include "OSGSimpleGeometry.h"
13 #include "OSGGLUTWindow.h"
14 #include "OSGSimpleSceneManager.h"
15 #include "OSGAction.h"
17 // New Headers
19 // the general scene file loading handler
20 #include "OSGSceneFileHandler.h"
22 #include "OSGFieldContainerUtils.h"
25 // The SimpleSceneManager to manage simple applications
26 OSG::SimpleSceneManagerRefPtr mgr;
28 // forward declaration so we can have the interesting stuff upfront
29 int setupGLUT( int *argc, char *argv[] );
31 // Initialize GLUT & OpenSG and set up the scene
32 int main(int argc, char **argv)
34 // OSG init
35 OSG::osgInit(argc,argv);
37 // GLUT init
38 int winid = setupGLUT(&argc, argv);
41 // the connection between GLUT and OpenSG
42 OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create();
43 gwin->setGlutId(winid);
44 gwin->init();
46 // load the scene
48 OSG::NodeUnrecPtr scene;
50 if(argc < 2)
52 FWARNING(("No file given!\n"));
53 FWARNING(("Supported file formats:\n"));
55 OSG::SceneFileHandler::the()->print();
56 scene = OSG::makeTorus(.5, 2, 16, 16);
58 else
61 All scene file loading is handled via the SceneFileHandler.
63 scene = OSG::SceneFileHandler::the()->read(argv[1]);
66 //scene->dump();
68 OSG::commitChanges();
70 // create the SimpleSceneManager helper
71 mgr = OSG::SimpleSceneManager::create();
72 mgr->setUseTraversalAction(true);
74 // tell the manager what to manage
75 mgr->setWindow(gwin );
76 mgr->setRoot (scene);
78 // show the whole scene
79 mgr->showAll();
80 mgr->useOpenSGLogo();
83 // GLUT main loop
84 glutMainLoop();
86 return 0;
90 // GLUT callback functions
93 // redraw the window
94 void display(void)
96 mgr->idle();
97 mgr->redraw();
98 OSG::Thread::getCurrentChangeList()->clear();
101 // react to size changes
102 void reshape(int w, int h)
104 mgr->resize(w, h);
105 glutPostRedisplay();
108 // react to mouse button presses
109 void mouse(int button, int state, int x, int y)
112 if (state)
113 mgr->mouseButtonRelease(button, x, y);
114 else
115 mgr->mouseButtonPress(button, x, y);
117 glutPostRedisplay();
120 // react to mouse motions with pressed buttons
121 void motion(int x, int y)
124 mgr->mouseMove(x, y);
125 glutPostRedisplay();
128 // react to keys
129 void keyboard(unsigned char k, int , int )
131 switch(k)
133 case 27:
134 mgr = NULL;
136 OSG::osgExit();
137 exit(0);
138 break;
139 case 'f':
140 mgr->setNavigationMode(OSG::Navigator::FLY);
141 break;
142 case 't':
143 mgr->setNavigationMode(OSG::Navigator::TRACKBALL);
144 break;
145 case 'q':
146 mgr->setStatistics(true);
147 break;
148 case 'w':
149 mgr->setStatistics(false);
150 break;
151 case 'h':
152 mgr->setHeadlight(!mgr->getHeadlightState());
153 break;
154 case 'r':
156 bool useTrav = !mgr->getUseTraversalAction();
157 mgr->setUseTraversalAction(useTrav);
158 printf("Using %s action.\n", useTrav ? "render traversal" : "render");
160 break;
162 case 'p':
164 std::cout << "Scanning memory consumption." << std::endl;
165 OSG::MemoryConsumption mc;
166 mc.scan();
167 mc.print(std::cout);
169 break;
173 // setup the GLUT library which handles the windows for us
174 int setupGLUT(int *argc, char *argv[])
176 glutInit(argc, argv);
177 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
179 int winid = glutCreateWindow("OpenSG");
181 glutReshapeFunc(reshape);
182 glutDisplayFunc(display);
183 glutIdleFunc(display);
184 glutMouseFunc(mouse);
185 glutMotionFunc(motion);
186 glutKeyboardFunc(keyboard);
188 return winid;