root/branches/Carsten_PtrWork2/Tutorials/10loading.cpp

Revision 1039, 5.5 kB (checked in by cneumann, 11 months ago)

changed: - factory functions return a TransitPtr? that can not be implicitly

converted to C Ptr. Should help with porting.

added: - GlobalRefPtr?, needed for cases where upon return from main

a RefPtr? goes out of scope (it would attempt to access the
FCFactory which is already shutdown at that point).

status: - vrml loader does not compile (needs porting to ref ptr)

  • tutorials compile, run and exit cleanly
  • multithreading and cluster are untested, yet
  • Property svn:eol-style set to native
Line 
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 //
8
9 // Headers
10 #include <OpenSG/OSGGLUT.h>
11 #include <OpenSG/OSGConfig.h>
12 #include <OpenSG/OSGSimpleGeometry.h>
13 #include <OpenSG/OSGGLUTWindow.h>
14 #include <OpenSG/OSGSimpleSceneManager.h>
15 #include <OpenSG/OSGAction.h>
16
17 // New Headers
18
19 // the general scene file loading handler
20 #include <OpenSG/OSGSceneFileHandler.h>
21 #include <boost/bind.hpp>
22
23 // Activate the OpenSG namespace
24 OSG_USING_NAMESPACE
25
26 // The SimpleSceneManager to manage simple applications
27 SimpleSceneManager *mgr;
28
29 // forward declaration so we can have the interesting stuff upfront
30 int setupGLUT( int *argc, char *argv[] );
31
32
33 // helper class to find a named node
34 // names are handled as simple attachments, get the header for that
35 #include <OpenSG/OSGNameAttachment.h>
36
37 // There are two convenience functions for name access: getName() and
38 // setName(). For details about general attachment handling see the
39 // attachments tutorial
40
41 class NamedNodeFinder
42 {
43   public:
44
45     NamedNodeFinder(void) : _name(), _found() {}
46
47     NodePtr operator() (NodePtr root, std::string name)
48     {
49         _name=&name;
50         _found=NullFC;
51
52         TraverseEnterFunctor enter =
53             boost::bind(&NamedNodeFinder::check, this, _1);
54         traverse(root, enter);
55
56         return _found;
57     }
58
59     static NodePtr find(NodePtr root, std::string name)
60     {
61         NamedNodeFinder f;
62
63         return f(root,name);
64     }
65
66   private:
67
68     Action::ResultE check(NodePtr node)
69     {
70         if(getName(node) && *_name == getName(node))
71         {
72             _found = node;
73             return Action::Quit;
74         }
75
76         return Action::Continue;
77     }
78
79     NodePtr  _found;
80     std::string  *_name;
81 };
82
83 // Initialize GLUT & OpenSG and set up the scene
84 int main(int argc, char **argv)
85 {
86     // OSG init
87     osgInit(argc,argv);
88
89     // GLUT init
90     int winid = setupGLUT(&argc, argv);
91
92     // the connection between GLUT and OpenSG
93     GLUTWindowGlobalRefPtr gwin = GLUTWindow::create();
94     gwin->setGlutId(winid);
95     gwin->init();
96
97     // load the scene
98
99     NodeGlobalRefPtr scene;
100
101     if(argc < 2)
102     {
103         FWARNING(("No file given!\n"));
104         FWARNING(("Supported file formats:\n"));
105
106         std::list<const char*> suffixes;
107         SceneFileHandler::the()->getSuffixList(suffixes);
108         //SceneFileHandler::the()->print();
109
110         for(std::list<const char*>::iterator it  = suffixes.begin();
111                                              it != suffixes.end();
112                                            ++it)
113         {
114             FWARNING(("%s\n", *it));
115         }
116
117         scene = makeTorus(.5, 2, 16, 16);
118     }
119     else
120     {
121         /*
122             All scene file loading is handled via the SceneFileHandler.
123         */
124         scene = SceneFileHandler::the()->read(argv[1], NULL);
125     }
126
127
128     NodePtr found;
129     NamedNodeFinder f;
130
131     // Try to find the Scene object. As it hasn't been named yet,
132     // it's not expected to be found.
133     found = f(scene, "Scene");
134     if(found == NullFC)
135     {
136         SLOG << "Found no object named Scene." << endLog;
137     }
138     else
139     {
140         SLOG << "Found object " << found << " named Scene. How did that happen?"
141              << endLog;
142     }
143
144     // Try to find the TF_DETAIL object. An object in Data/tie.wrl is called
145     // TF_DETAIL, so we might find it.
146     found = NamedNodeFinder::find(scene, "TF_DETAIL");
147     if(found == NullFC)
148     {
149         SLOG << "Found no object named TF_DETAIL (did you load the tie?)."
150              << endLog;
151     }
152     else
153     {
154         SLOG << "Found object " << found << " named TF_DETAIL." << endLog;
155     }
156
157     commitChanges();
158
159     // create the SimpleSceneManager helper
160     mgr = new SimpleSceneManager;
161
162     // tell the manager what to manage
163     mgr->setWindow(gwin );
164     mgr->setRoot  (scene);
165
166     // show the whole scene
167     mgr->showAll();
168
169     // GLUT main loop
170     glutMainLoop();
171
172     return 0;
173 }
174
175 //
176 // GLUT callback functions
177 //
178
179 // redraw the window
180 void display(void)
181 {
182     mgr->idle();
183     mgr->redraw();
184 }
185
186 // react to size changes
187 void reshape(int w, int h)
188 {
189     mgr->resize(w, h);
190     glutPostRedisplay();
191 }
192
193 // react to mouse button presses
194 void mouse(int button, int state, int x, int y)
195 {
196
197     if (state)
198         mgr->mouseButtonRelease(button, x, y);
199     else
200         mgr->mouseButtonPress(button, x, y);
201
202     glutPostRedisplay();
203 }
204
205 // react to mouse motions with pressed buttons
206 void motion(int x, int y)
207 {
208
209     mgr->mouseMove(x, y);
210     glutPostRedisplay();
211 }
212
213 // react to keys
214 void keyboard(unsigned char k, int , int )
215 {
216     switch(k)
217     {
218         case 27:
219         {
220             delete mgr;
221        
222             OSG::osgExit();
223             exit(0);
224         }
225         break;
226
227         case 'f':
228         {
229             mgr->setNavigationMode(Navigator::FLY);
230         }
231         break;
232
233         case 't':
234         {
235             mgr->setNavigationMode(Navigator::TRACKBALL);
236         }
237         break;
238
239     case 's':
240        {
241           mgr->setStatistics(!mgr->getStatistics());
242        }
243     }
244 }
245
246 // setup the GLUT library which handles the windows for us
247 int setupGLUT(int *argc, char *argv[])
248 {
249     glutInit(argc, argv);
250     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
251
252     int winid = glutCreateWindow("OpenSG");
253
254     glutReshapeFunc(reshape);
255     glutDisplayFunc(display);
256     glutIdleFunc(display);
257     glutMouseFunc(mouse);
258     glutMotionFunc(motion);
259     glutKeyboardFunc(keyboard);
260
261     return winid;
262 }
Note: See TracBrowser for help on using the browser.