1 // OpenSG Tutorial Example: Loading
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
9 #ifdef OSG_BUILD_ACTIVE
12 #include <OSGConfig.h>
13 #include <OSGSimpleGeometry.h>
14 #include <OSGGLUTWindow.h>
15 #include <OSGSimpleSceneManager.h>
16 #include <OSGAction.h>
20 // the general scene file loading handler
21 #include <OSGSceneFileHandler.h>
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>
33 // the general scene file loading handler
34 #include <OpenSG/OSGSceneFileHandler.h>
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>
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>
56 // There are two convenience functions for name access: getName() and
57 // setName(). For details about general attachment handling see the
58 // attachments tutorial
66 NamedNodeFinder(void) : _found(), _name() {}
68 OSG::Node
*operator() (OSG::Node
*root
, const std::string
&name
)
73 OSG::TraverseEnterFunctor enter
=
74 boost::bind(&NamedNodeFinder::checkNode
, this, _1
);
75 OSG::traverse(root
, enter
);
80 static OSG::Node
*find(OSG::Node
*root
, const std::string
&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
))
93 return OSG::Action::Quit
;
96 return OSG::Action::Continue
;
103 // Initialize GLUT & OpenSG and set up the scene
104 int main(int argc
, char **argv
)
107 OSG::osgInit(argc
,argv
);
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
);
123 OSG::NodeRefPtr scene
;
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();
138 FWARNING(("%s\n", *it
));
141 scene
= OSG::makeTorus(.5, 2, 16, 16);
146 All scene file loading is handled via the SceneFileHandler.
148 scene
= OSG::SceneFileHandler::the()->read(argv
[1]);
152 OSG::NodeRefPtr found
;
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");
162 SLOG
<< "Found no object named 'Scene'.\n";
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");
176 SLOG
<< "Found no object named 'TF_DETAIL' (did you load the tie?)."
181 SLOG
<< "Found object " << found
<< " named 'TF_DETAIL'."
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
205 // GLUT callback functions
215 // react to size changes
216 void reshape(int w
, int h
)
222 // react to mouse button presses
223 void mouse(int button
, int state
, int x
, int y
)
227 mgr
->mouseButtonRelease(button
, x
, y
);
229 mgr
->mouseButtonPress(button
, x
, y
);
234 // react to mouse motions with pressed buttons
235 void motion(int x
, int y
)
238 mgr
->mouseMove(x
, y
);
243 void keyboard(unsigned char k
, int , int )
249 // clean up global variables
259 mgr
->setNavigationMode(OSG::Navigator::FLY
);
265 mgr
->setNavigationMode(OSG::Navigator::TRACKBALL
);
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
);