fixed: tess shader source typos (thanks to M. Weiler for the report)
[opensg.git] / Examples / Simple / 10loading.cpp
blob55cf8985578e45ac30c2da6ae9c6f44ca460d0e2
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;
103 // Initialize GLUT & OpenSG and set up the scene
104 int main(int argc, char **argv)
106 // OSG init
107 OSG::osgInit(argc,argv);
109 // GLUT init
110 int winid = setupGLUT(&argc, argv);
112 // open a new scope, because the pointers below should go out of scope
113 // before entering glutMainLoop.
114 // Otherwise OpenSG will complain about objects being alive after shutdown.
116 // the connection between GLUT and OpenSG
117 OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create();
118 gwin->setGlutId(winid);
119 gwin->init();
121 // load the scene
123 OSG::NodeRefPtr scene;
125 if(argc < 2)
127 FWARNING(("No file given!\n"));
128 FWARNING(("Supported file formats:\n"));
130 std::list<const char*> suffixes;
131 OSG::SceneFileHandler::the()->getSuffixList(suffixes);
132 //SceneFileHandler::the()->print();
134 for(std::list<const char*>::iterator it = suffixes.begin();
135 it != suffixes.end();
136 ++it)
138 FWARNING(("%s\n", *it));
141 scene = OSG::makeTorus(.5, 2, 16, 16);
143 else
146 All scene file loading is handled via the SceneFileHandler.
148 scene = OSG::SceneFileHandler::the()->read(argv[1]);
152 OSG::NodeRefPtr found;
154 NamedNodeFinder f;
156 // Try to find the Scene object. As it hasn't been named yet,
157 // it's not expected to be found.
158 found = f(scene, "Scene");
160 if(found == NULL)
162 SLOG << "Found no object named 'Scene'.\n";
164 else
166 SLOG << "Found object " << found
167 << " named 'Scene'. How did that happen?\n";
170 // Try to find the TF_DETAIL object. An object in Data/tie.wrl is called
171 // TF_DETAIL, so we might find it.
172 found = NamedNodeFinder::find(scene, "TF_DETAIL");
174 if(found == NULL)
176 SLOG << "Found no object named 'TF_DETAIL' (did you load the tie?)."
177 << OSG::endLog;
179 else
181 SLOG << "Found object " << found << " named 'TF_DETAIL'."
182 << OSG::endLog;
185 OSG::commitChanges();
187 // create the SimpleSceneManager helper
188 mgr = OSG::SimpleSceneManager::create();
190 // tell the manager what to manage
191 mgr->setWindow(gwin );
192 mgr->setRoot (scene);
194 // show the whole scene
195 mgr->showAll();
198 // GLUT main loop
199 glutMainLoop();
201 return 0;
205 // GLUT callback functions
208 // redraw the window
209 void display(void)
211 mgr->idle();
212 mgr->redraw();
215 // react to size changes
216 void reshape(int w, int h)
218 mgr->resize(w, h);
219 glutPostRedisplay();
222 // react to mouse button presses
223 void mouse(int button, int state, int x, int y)
226 if (state)
227 mgr->mouseButtonRelease(button, x, y);
228 else
229 mgr->mouseButtonPress(button, x, y);
231 glutPostRedisplay();
234 // react to mouse motions with pressed buttons
235 void motion(int x, int y)
238 mgr->mouseMove(x, y);
239 glutPostRedisplay();
242 // react to keys
243 void keyboard(unsigned char k, int , int )
245 switch(k)
247 case 27:
249 // clean up global variables
250 mgr = NULL;
252 OSG::osgExit();
253 exit(0);
255 break;
257 case 'f':
259 mgr->setNavigationMode(OSG::Navigator::FLY);
261 break;
263 case 't':
265 mgr->setNavigationMode(OSG::Navigator::TRACKBALL);
267 break;
269 case 's':
271 mgr->setStatistics(!mgr->getStatistics());
276 // setup the GLUT library which handles the windows for us
277 int setupGLUT(int *argc, char *argv[])
279 glutInit(argc, argv);
280 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
282 int winid = glutCreateWindow("OpenSG");
284 glutReshapeFunc(reshape);
285 glutDisplayFunc(display);
286 glutIdleFunc(display);
287 glutMouseFunc(mouse);
288 glutMotionFunc(motion);
289 glutKeyboardFunc(keyboard);
291 return winid;