changed: gcc8 base update
[opensg.git] / Examples / Simple / 10loading.cpp
blobe5e4aa1dbc03e9f6486d28c4b81ae06101e01145
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 #ifdef OSG_BUILD_ACTIVE
10 // Headers
11 #include <OSGGLUT.h>
12 #include <OSGConfig.h>
13 #include <OSGSimpleGeometry.h>
14 #include <OSGGLUTWindow.h>
15 #include <OSGSimpleSceneManager.h>
16 #include <OSGAction.h>
18 // New Headers
20 // the general scene file loading handler
21 #include <OSGSceneFileHandler.h>
22 #else
23 // Headers
24 #include <OpenSG/OSGGLUT.h>
25 #include <OpenSG/OSGConfig.h>
26 #include <OpenSG/OSGSimpleGeometry.h>
27 #include <OpenSG/OSGGLUTWindow.h>
28 #include <OpenSG/OSGSimpleSceneManager.h>
29 #include <OpenSG/OSGAction.h>
31 // New Headers
33 // the general scene file loading handler
34 #include <OpenSG/OSGSceneFileHandler.h>
35 #endif
37 #include <boost/bind.hpp>
39 // The SimpleSceneManager to manage simple applications
40 OSG::SimpleSceneManagerRefPtr mgr;
42 // forward declaration so we can have the interesting stuff upfront
43 int setupGLUT( int *argc, char *argv[] );
46 #ifdef OSG_BUILD_ACTIVE
47 // helper class to find a named node
48 // names are handled as simple attachments, get the header for that
49 #include <OSGNameAttachment.h>
50 #else
51 // helper class to find a named node
52 // names are handled as simple attachments, get the header for that
53 #include <OpenSG/OSGNameAttachment.h>
54 #endif
56 // There are two convenience functions for name access: getName() and
57 // setName(). For details about general attachment handling see the
58 // attachments tutorial
60 class NamedNodeFinder
62 private:
64 public:
66 NamedNodeFinder(void) : _found(), _name() {}
68 OSG::Node *operator() (OSG::Node *root, const std::string &name)
70 _name = name;
71 _found = NULL;
73 OSG::TraverseEnterFunctor enter =
74 boost::bind(&NamedNodeFinder::checkNode, this, _1);
75 OSG::traverse(root, enter);
77 return _found;
80 static OSG::Node *find(OSG::Node *root, const std::string &name)
82 NamedNodeFinder f;
84 return f(root, name);
87 // %$#%$#% OS X trashes check symbol so we need to use checkNode
88 OSG::Action::ResultE checkNode(OSG::Node *node)
90 if(OSG::getName(node) && _name == OSG::getName(node))
92 _found = node;
93 return OSG::Action::Quit;
96 return OSG::Action::Continue;
99 OSG::Node *_found;
100 std::string _name;
102 private:
104 NamedNodeFinder(const NamedNodeFinder &other);
105 void operator =(const NamedNodeFinder &other);
108 // Initialize GLUT & OpenSG and set up the scene
109 int main(int argc, char **argv)
111 // OSG init
112 OSG::osgInit(argc,argv);
114 // GLUT init
115 int winid = setupGLUT(&argc, argv);
117 // open a new scope, because the pointers below should go out of scope
118 // before entering glutMainLoop.
119 // Otherwise OpenSG will complain about objects being alive after shutdown.
121 // the connection between GLUT and OpenSG
122 OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create();
123 gwin->setGlutId(winid);
124 gwin->init();
126 // load the scene
128 OSG::NodeRefPtr scene;
130 if(argc < 2)
132 FWARNING(("No file given!\n"));
133 FWARNING(("Supported file formats:\n"));
135 std::list<const char*> suffixes;
136 OSG::SceneFileHandler::the()->getSuffixList(suffixes);
137 //SceneFileHandler::the()->print();
139 for(std::list<const char*>::iterator it = suffixes.begin();
140 it != suffixes.end();
141 ++it)
143 FWARNING(("%s\n", *it));
146 scene = OSG::makeTorus(.5, 2, 16, 16);
148 else
151 All scene file loading is handled via the SceneFileHandler.
153 scene = OSG::SceneFileHandler::the()->read(argv[1]);
157 OSG::NodeRefPtr found;
159 NamedNodeFinder f;
161 // Try to find the Scene object. As it hasn't been named yet,
162 // it's not expected to be found.
163 found = f(scene, "Scene");
165 if(found == NULL)
167 SLOG << "Found no object named 'Scene'.\n";
169 else
171 SLOG << "Found object " << found
172 << " named 'Scene'. How did that happen?\n";
175 // Try to find the TF_DETAIL object. An object in Data/tie.wrl is called
176 // TF_DETAIL, so we might find it.
177 found = NamedNodeFinder::find(scene, "TF_DETAIL");
179 if(found == NULL)
181 SLOG << "Found no object named 'TF_DETAIL' (did you load the tie?)."
182 << OSG::endLog;
184 else
186 SLOG << "Found object " << found << " named 'TF_DETAIL'."
187 << OSG::endLog;
190 OSG::commitChanges();
192 // create the SimpleSceneManager helper
193 mgr = OSG::SimpleSceneManager::create();
195 // tell the manager what to manage
196 mgr->setWindow(gwin );
197 mgr->setRoot (scene);
199 // show the whole scene
200 mgr->showAll();
203 // GLUT main loop
204 glutMainLoop();
206 return 0;
210 // GLUT callback functions
213 // redraw the window
214 void display(void)
216 mgr->idle();
217 mgr->redraw();
220 // react to size changes
221 void reshape(int w, int h)
223 mgr->resize(w, h);
224 glutPostRedisplay();
227 // react to mouse button presses
228 void mouse(int button, int state, int x, int y)
231 if (state)
232 mgr->mouseButtonRelease(button, x, y);
233 else
234 mgr->mouseButtonPress(button, x, y);
236 glutPostRedisplay();
239 // react to mouse motions with pressed buttons
240 void motion(int x, int y)
243 mgr->mouseMove(x, y);
244 glutPostRedisplay();
247 // react to keys
248 void keyboard(unsigned char k, int , int )
250 switch(k)
252 case 27:
254 // clean up global variables
255 mgr = NULL;
257 OSG::osgExit();
258 exit(0);
260 break;
262 case 'f':
264 mgr->setNavigationMode(OSG::Navigator::FLY);
266 break;
268 case 't':
270 mgr->setNavigationMode(OSG::Navigator::TRACKBALL);
272 break;
274 case 's':
276 mgr->setStatistics(!mgr->getStatistics());
281 // setup the GLUT library which handles the windows for us
282 int setupGLUT(int *argc, char *argv[])
284 glutInit(argc, argv);
285 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
287 int winid = glutCreateWindow("OpenSG");
289 glutReshapeFunc(reshape);
290 glutDisplayFunc(display);
291 glutIdleFunc(display);
292 glutMouseFunc(mouse);
293 glutMotionFunc(motion);
294 glutKeyboardFunc(keyboard);
296 return winid;