5 #include "OSGSimpleGeometry.h"
6 #include "OSGGLUTWindow.h"
7 #include "OSGSimpleSceneManager.h"
9 #include "OSGSimpleGeometry.h"
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
);
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
);
58 fs::path complete_file
= fs::complete(*itr
);
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
)
74 OSG::osgInit(argc
,argv
);
76 gScene
= OSG::GroupNodeRefPtr::create();
80 std::cout
<< "Specify a directory to load models from." << std::endl
;
81 gScene
= static_cast<OSG::Node
*>(NULL
);
86 findModels(std::string(argv
[1]));
88 // Start the background loader.
89 OSG::BackgroundLoader::the()->start();
92 int winid
= setupGLUT(&argc
, argv
);
94 // the connection between GLUT and OpenSG
95 OSG::GLUTWindowUnrecPtr gwin
= OSG::GLUTWindow::create();
96 gwin
->setGlutId(winid
);
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
112 OSG::BackgroundLoader::the()->stop();
118 // GLUT callback functions
124 OSG::Thread::getCurrentChangeList()->commitChangesAndClear();
126 //OSG::BackgroundLoader::the()->processOne();
127 // Sync changes from BackgroundLoader.
128 OSG::BackgroundLoader::the()->sync();
134 // react to size changes
135 void reshape(int w
, int h
)
141 // react to mouse button presses
142 void mouse(int button
, int state
, int x
, int y
)
146 mgr
->mouseButtonRelease(button
, x
, y
);
148 mgr
->mouseButtonPress(button
, x
, y
);
153 // react to mouse motions with pressed buttons
154 void motion(int x
, int y
)
157 mgr
->mouseMove(x
, y
);
162 void keyboard(unsigned char k
, int , int )
169 gScene
= static_cast<OSG::Node
*>(NULL
);
178 mgr
->setNavigationMode(OSG::Navigator::FLY
);
184 mgr
->setNavigationMode(OSG::Navigator::TRACKBALL
);
196 mgr
->setStatistics(!mgr
->getStatistics());
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
);