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

Revision 835, 5.5 kB (checked in by vossg, 1 year ago)

ported : cluster windows (basic port still need quite some testing and verification)
added : ptr mfield copy to containers
fixed : tutorial compile problems

  • 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     GLUTWindowPtr gwin= GLUTWindow::create();
94     gwin->setGlutId(winid);
95     gwin->init();
96
97     // load the scene
98
99     NodePtr 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]);
125     }
126
127
128     NodePtr found;
129
130     NamedNodeFinder f;
131
132     // Try to find the Scene object. As it hasn't been named yet,
133     // it's not expected to be found.
134     found = f(scene, "Scene");
135     if(found == NullFC)
136     {
137         SLOG << "Found no object named Scene." << endLog;
138     }
139     else
140     {
141         SLOG << "Found object " << found << " named Scene. How did that happen?"
142              << endLog;
143     }
144
145     // Try to find the TF_DETAIL object. An object in Data/tie.wrl is called
146     // TF_DETAIL, so we might find it.
147     found = NamedNodeFinder::find(scene, "TF_DETAIL");
148     if(found == NullFC)
149     {
150         SLOG << "Found no object named TF_DETAIL (did you load the tie?)."
151              << endLog;
152     }
153     else
154     {
155         SLOG << "Found object " << found << " named TF_DETAIL." << endLog;
156     }
157
158     commitChanges();
159
160     // create the SimpleSceneManager helper
161     mgr = new SimpleSceneManager;
162
163     // tell the manager what to manage
164     mgr->setWindow(gwin );
165     mgr->setRoot  (scene);
166
167     // show the whole scene
168     mgr->showAll();
169
170     // GLUT main loop
171     glutMainLoop();
172
173     return 0;
174 }
175
176 //
177 // GLUT callback functions
178 //
179
180 // redraw the window
181 void display(void)
182 {
183     mgr->idle();
184     mgr->redraw();
185 }
186
187 // react to size changes
188 void reshape(int w, int h)
189 {
190     mgr->resize(w, h);
191     glutPostRedisplay();
192 }
193
194 // react to mouse button presses
195 void mouse(int button, int state, int x, int y)
196 {
197
198     if (state)
199         mgr->mouseButtonRelease(button, x, y);
200     else
201         mgr->mouseButtonPress(button, x, y);
202
203     glutPostRedisplay();
204 }
205
206 // react to mouse motions with pressed buttons
207 void motion(int x, int y)
208 {
209
210     mgr->mouseMove(x, y);
211     glutPostRedisplay();
212 }
213
214 // react to keys
215 void keyboard(unsigned char k, int , int )
216 {
217     switch(k)
218     {
219         case 27:
220         {
221             OSG::osgExit();
222             exit(0);
223         }
224         break;
225
226         case 'f':
227         {
228             mgr->setNavigationMode(Navigator::FLY);
229         }
230         break;
231
232         case 't':
233         {
234             mgr->setNavigationMode(Navigator::TRACKBALL);
235         }
236         break;
237
238     case 's':
239        {
240           mgr->setStatistics(!mgr->getStatistics());
241        }
242     }
243 }
244
245 // setup the GLUT library which handles the windows for us
246 int setupGLUT(int *argc, char *argv[])
247 {
248     glutInit(argc, argv);
249     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
250
251     int winid = glutCreateWindow("OpenSG");
252
253     glutReshapeFunc(reshape);
254     glutDisplayFunc(display);
255     glutIdleFunc(display);
256     glutMouseFunc(mouse);
257     glutMotionFunc(motion);
258     glutKeyboardFunc(keyboard);
259
260     return winid;
261 }
Note: See TracBrowser for help on using the browser.