fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / Contrib / BackgroundLoader / testBackgroundLoader.cpp
blob5fdf1359d577f5c566293ba189b50c2d017b42c9
2 // Headers
3 #include "OSGGLUT.h"
4 #include "OSGConfig.h"
5 #include "OSGSimpleGeometry.h"
6 #include "OSGGLUTWindow.h"
7 #include "OSGSimpleSceneManager.h"
8 #include "OSGAction.h"
9 #include "OSGSimpleGeometry.h"
11 // New Headers
13 // the general scene file loading handler
14 #include "OSGSceneFileHandler.h"
15 #include "OSGFieldContainerFactory.h"
17 #include <boost/bind.hpp>
18 #include <boost/filesystem/operations.hpp>
19 #include <boost/filesystem/fstream.hpp>
20 #include <boost/filesystem/convenience.hpp>
21 namespace fs = boost::filesystem;
23 #include "OSGBackgroundLoader.h"
24 #include "OSGModelRequest.h"
26 // The SimpleSceneManager to manage simple applications
27 OSG::SimpleSceneManagerRefPtr mgr;
28 OSG::GroupNodeRefPtr gScene;
30 unsigned gNextModelIdx = 0;
32 // forward declaration so we can have the interesting stuff upfront
33 int setupGLUT( int *argc, char *argv[] );
35 void findModels(std::string dirname)
37 fs::path dir_path(dirname);
39 if (!fs::exists(dir_path))
41 std::cerr << "ERROR: path does not exist: " << dirname << std::endl;
42 gScene = static_cast<OSG::Node *>(NULL);
43 OSG::osgExit();
44 exit(-1);
47 fs::directory_iterator end_itr;
48 for(fs::directory_iterator itr(dir_path); itr != end_itr; ++itr)
50 if(!fs::is_directory(*itr))
52 if (fs::extension(*itr) == std::string(".osb") ||
53 fs::extension(*itr) == std::string(".wrl"))
55 #if BOOST_FILESYSTEM_VERSION == 3
56 fs::path complete_file = fs::absolute(*itr);
57 #else
58 fs::path complete_file = fs::complete(*itr);
59 #endif
60 std::string filename(complete_file.string());
61 std::cout << "Found file: " << filename << std::endl;
62 OSG::ModelRequestPtr req = OSG::ModelRequest::create()->init(OSG::NodeRefPtr(gScene.node()), filename);
63 OSG::BackgroundLoader::the()->addRequest(req);
70 // Initialize GLUT & OpenSG and set up the scene
71 int main(int argc, char **argv)
73 // OSG init
74 OSG::osgInit(argc,argv);
76 gScene = OSG::GroupNodeRefPtr::create();
78 if (argc < 2)
80 std::cout << "Specify a directory to load models from." << std::endl;
81 gScene = static_cast<OSG::Node *>(NULL);
82 OSG::osgExit();
83 exit(-1);
86 findModels(std::string(argv[1]));
88 // Start the background loader.
89 OSG::BackgroundLoader::the()->start();
91 // GLUT init
92 int winid = setupGLUT(&argc, argv);
94 // the connection between GLUT and OpenSG
95 OSG::GLUTWindowUnrecPtr gwin= OSG::GLUTWindow::create();
96 gwin->setGlutId(winid);
97 gwin->init();
99 // create the SimpleSceneManager helper
100 mgr = OSG::SimpleSceneManager::create();
102 // tell the manager what to manage
103 mgr->setWindow(gwin );
104 mgr->setRoot (gScene.node());
106 // show the whole scene
107 mgr->showAll();
109 // GLUT main loop
110 glutMainLoop();
112 OSG::BackgroundLoader::the()->stop();
114 return 0;
118 // GLUT callback functions
121 // redraw the window
122 void display(void)
124 OSG::Thread::getCurrentChangeList()->commitChangesAndClear();
126 //OSG::BackgroundLoader::the()->processOne();
127 // Sync changes from BackgroundLoader.
128 OSG::BackgroundLoader::the()->sync();
130 mgr->idle();
131 mgr->redraw();
134 // react to size changes
135 void reshape(int w, int h)
137 mgr->resize(w, h);
138 glutPostRedisplay();
141 // react to mouse button presses
142 void mouse(int button, int state, int x, int y)
145 if (state)
146 mgr->mouseButtonRelease(button, x, y);
147 else
148 mgr->mouseButtonPress(button, x, y);
150 glutPostRedisplay();
153 // react to mouse motions with pressed buttons
154 void motion(int x, int y)
157 mgr->mouseMove(x, y);
158 glutPostRedisplay();
161 // react to keys
162 void keyboard(unsigned char k, int , int )
164 switch(k)
166 case 27:
168 mgr = NULL;
169 gScene = static_cast<OSG::Node *>(NULL);
171 OSG::osgExit();
172 exit(0);
174 break;
176 case 'f':
178 mgr->setNavigationMode(OSG::Navigator::FLY);
180 break;
182 case 't':
184 mgr->setNavigationMode(OSG::Navigator::TRACKBALL);
186 break;
188 case 'a':
190 mgr->showAll();
192 break;
194 case 's':
196 mgr->setStatistics(!mgr->getStatistics());
198 break;
202 // setup the GLUT library which handles the windows for us
203 int setupGLUT(int *argc, char *argv[])
205 glutInit(argc, argv);
206 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
208 int winid = glutCreateWindow("OpenSG");
210 glutReshapeFunc(reshape);
211 glutDisplayFunc(display);
212 glutIdleFunc(display);
213 glutMouseFunc(mouse);
214 glutMotionFunc(motion);
215 glutKeyboardFunc(keyboard);
217 return winid;