| 1 | #if 1 |
|---|
| 2 | |
|---|
| 3 | #include <osgDB/ReadFile> |
|---|
| 4 | #include <osgDB/WriteFile> |
|---|
| 5 | #include <osgViewer/Viewer> |
|---|
| 6 | #include <osgGA/TrackballManipulator> |
|---|
| 7 | #include <osgGA/FlightManipulator> |
|---|
| 8 | #include <osgGA/AnimationPathManipulator> |
|---|
| 9 | #include <iostream> |
|---|
| 10 | |
|---|
| 11 | class ThreadingHandler : public osgGA::GUIEventHandler |
|---|
| 12 | { |
|---|
| 13 | public: |
|---|
| 14 | |
|---|
| 15 | ThreadingHandler() {} |
|---|
| 16 | |
|---|
| 17 | bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
|---|
| 18 | { |
|---|
| 19 | osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa); |
|---|
| 20 | if (!viewer) return false; |
|---|
| 21 | |
|---|
| 22 | switch(ea.getEventType()) |
|---|
| 23 | { |
|---|
| 24 | case(osgGA::GUIEventAdapter::KEYUP): |
|---|
| 25 | { |
|---|
| 26 | if (ea.getKey()=='m') |
|---|
| 27 | { |
|---|
| 28 | switch(viewer->getThreadingModel()) |
|---|
| 29 | { |
|---|
| 30 | case(osgViewer::Viewer::SingleThreaded): |
|---|
| 31 | viewer->setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext); |
|---|
| 32 | osg::notify(osg::NOTICE)<<"Threading model 'CullDrawThreadPerContext' selected."<<std::endl; |
|---|
| 33 | break; |
|---|
| 34 | case(osgViewer::Viewer::CullDrawThreadPerContext): |
|---|
| 35 | viewer->setThreadingModel(osgViewer::Viewer::DrawThreadPerContext); |
|---|
| 36 | osg::notify(osg::NOTICE)<<"Threading model 'DrawThreadPerContext' selected."<<std::endl; |
|---|
| 37 | break; |
|---|
| 38 | case(osgViewer::Viewer::DrawThreadPerContext): |
|---|
| 39 | viewer->setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext); |
|---|
| 40 | osg::notify(osg::NOTICE)<<"Threading model 'CullThreadPerCameraDrawThreadPerContext' selected."<<std::endl; |
|---|
| 41 | break; |
|---|
| 42 | case(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext): |
|---|
| 43 | viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded); |
|---|
| 44 | osg::notify(osg::NOTICE)<<"Threading model 'SingleThreaded' selected."<<std::endl; |
|---|
| 45 | break; |
|---|
| 46 | case(osgViewer::Viewer::AutomaticSelection): |
|---|
| 47 | viewer->setThreadingModel(viewer->suggestBestThreadingModel()); |
|---|
| 48 | osg::notify(osg::NOTICE)<<"Threading model 'AutomaticSelection' selected."<<std::endl; |
|---|
| 49 | break; |
|---|
| 50 | } |
|---|
| 51 | return true; |
|---|
| 52 | } |
|---|
| 53 | if (ea.getKey()=='e') |
|---|
| 54 | { |
|---|
| 55 | switch(viewer->getEndBarrierPosition()) |
|---|
| 56 | { |
|---|
| 57 | case(osgViewer::Viewer::BeforeSwapBuffers): |
|---|
| 58 | viewer->setEndBarrierPosition(osgViewer::Viewer::AfterSwapBuffers); |
|---|
| 59 | osg::notify(osg::NOTICE)<<"Threading model 'AfterSwapBuffers' selected."<<std::endl; |
|---|
| 60 | break; |
|---|
| 61 | case(osgViewer::Viewer::AfterSwapBuffers): |
|---|
| 62 | viewer->setEndBarrierPosition(osgViewer::Viewer::BeforeSwapBuffers); |
|---|
| 63 | osg::notify(osg::NOTICE)<<"Threading model 'BeforeSwapBuffers' selected."<<std::endl; |
|---|
| 64 | break; |
|---|
| 65 | } |
|---|
| 66 | return true; |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | default: break; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | return false; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | bool _done; |
|---|
| 76 | }; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | class ModelHandler : public osgGA::GUIEventHandler |
|---|
| 80 | { |
|---|
| 81 | public: |
|---|
| 82 | |
|---|
| 83 | ModelHandler(): |
|---|
| 84 | _position(0) {} |
|---|
| 85 | |
|---|
| 86 | typedef std::vector<std::string> Filenames; |
|---|
| 87 | Filenames _filenames; |
|---|
| 88 | unsigned int _position; |
|---|
| 89 | |
|---|
| 90 | void add(const std::string& filename) { _filenames.push_back(filename); } |
|---|
| 91 | |
|---|
| 92 | bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
|---|
| 93 | { |
|---|
| 94 | osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa); |
|---|
| 95 | if (!viewer) return false; |
|---|
| 96 | |
|---|
| 97 | if (_filenames.empty()) return false; |
|---|
| 98 | |
|---|
| 99 | switch(ea.getEventType()) |
|---|
| 100 | { |
|---|
| 101 | case(osgGA::GUIEventAdapter::KEYUP): |
|---|
| 102 | { |
|---|
| 103 | if (ea.getKey()=='l') |
|---|
| 104 | { |
|---|
| 105 | osg::ref_ptr<osg::Node> model = osgDB::readNodeFile( _filenames[_position] ); |
|---|
| 106 | ++_position; |
|---|
| 107 | if (_position>=_filenames.size()) _position = 0; |
|---|
| 108 | |
|---|
| 109 | if (model.valid()) |
|---|
| 110 | { |
|---|
| 111 | viewer->setSceneData(model.get()); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | return true; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | default: break; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | return false; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | bool _done; |
|---|
| 124 | }; |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | void singleWindowMultipleCameras(osgViewer::Viewer& viewer) |
|---|
| 128 | { |
|---|
| 129 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
|---|
| 130 | if (!wsi) |
|---|
| 131 | { |
|---|
| 132 | osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
|---|
| 133 | return; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | unsigned int width, height; |
|---|
| 137 | wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); |
|---|
| 138 | |
|---|
| 139 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 140 | traits->x = 0; |
|---|
| 141 | traits->y = 0; |
|---|
| 142 | traits->width = width; |
|---|
| 143 | traits->height = height; |
|---|
| 144 | traits->windowDecoration = true; |
|---|
| 145 | traits->doubleBuffer = true; |
|---|
| 146 | traits->sharedContext = 0; |
|---|
| 147 | |
|---|
| 148 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 149 | if (gc.valid()) |
|---|
| 150 | { |
|---|
| 151 | osg::notify(osg::INFO)<<" GraphicsWindow has been created successfully."<<std::endl; |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f)); |
|---|
| 156 | gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|---|
| 157 | } |
|---|
| 158 | else |
|---|
| 159 | { |
|---|
| 160 | osg::notify(osg::NOTICE)<<" GraphicsWindow has not been created successfully."<<std::endl; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | unsigned int numCameras = 2; |
|---|
| 164 | double aspectRatioScale = 1.0; |
|---|
| 165 | for(unsigned int i=0; i<numCameras;++i) |
|---|
| 166 | { |
|---|
| 167 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 168 | camera->setGraphicsContext(gc.get()); |
|---|
| 169 | camera->setViewport(new osg::Viewport((i*width)/numCameras,(i*height)/numCameras, width/numCameras, height/numCameras)); |
|---|
| 170 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 171 | camera->setDrawBuffer(buffer); |
|---|
| 172 | camera->setReadBuffer(buffer); |
|---|
| 173 | |
|---|
| 174 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::scale(aspectRatioScale,1.0,1.0)); |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | void multipleWindowMultipleCameras(osgViewer::Viewer& viewer) |
|---|
| 179 | { |
|---|
| 180 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
|---|
| 181 | if (!wsi) |
|---|
| 182 | { |
|---|
| 183 | osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
|---|
| 184 | return; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | unsigned int width, height; |
|---|
| 188 | wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | unsigned int numCameras = 6; |
|---|
| 192 | double aspectRatioScale = (double)numCameras; |
|---|
| 193 | double translate_x = double(numCameras)-1; |
|---|
| 194 | for(unsigned int i=0; i<numCameras;++i, translate_x -= 2.0) |
|---|
| 195 | { |
|---|
| 196 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 197 | traits->screenNum = i / 3; |
|---|
| 198 | traits->x = (i*width)/numCameras; |
|---|
| 199 | traits->y = 0; |
|---|
| 200 | traits->width = width/numCameras-1; |
|---|
| 201 | traits->height = height; |
|---|
| 202 | traits->windowDecoration = true; |
|---|
| 203 | traits->doubleBuffer = true; |
|---|
| 204 | traits->sharedContext = 0; |
|---|
| 205 | |
|---|
| 206 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 207 | if (gc.valid()) |
|---|
| 208 | { |
|---|
| 209 | osg::notify(osg::INFO)<<" GraphicsWindow has been created successfully."<<std::endl; |
|---|
| 210 | } |
|---|
| 211 | else |
|---|
| 212 | { |
|---|
| 213 | osg::notify(osg::NOTICE)<<" GraphicsWindow has not been created successfully."<<std::endl; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 217 | camera->setGraphicsContext(gc.get()); |
|---|
| 218 | camera->setViewport(new osg::Viewport(0,0, width/numCameras, height)); |
|---|
| 219 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 220 | camera->setDrawBuffer(buffer); |
|---|
| 221 | camera->setReadBuffer(buffer); |
|---|
| 222 | |
|---|
| 223 | viewer.addSlave(camera.get(), osg::Matrix::scale(aspectRatioScale, 1.0, 1.0)*osg::Matrix::translate(translate_x, 0.0, 0.0), osg::Matrix() ); |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | int main( int argc, char **argv ) |
|---|
| 228 | { |
|---|
| 229 | |
|---|
| 230 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 231 | |
|---|
| 232 | if (argc<2) |
|---|
| 233 | { |
|---|
| 234 | std::cout << argv[0] <<": requires filename argument." << std::endl; |
|---|
| 235 | return 1; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | std::string pathfile; |
|---|
| 240 | osg::ref_ptr<osgGA::AnimationPathManipulator> apm = 0; |
|---|
| 241 | while (arguments.read("-p",pathfile)) |
|---|
| 242 | { |
|---|
| 243 | apm = new osgGA::AnimationPathManipulator(pathfile); |
|---|
| 244 | if (!apm.valid() || !(apm->valid()) ) |
|---|
| 245 | { |
|---|
| 246 | apm = 0; |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | osgViewer::Viewer viewer; |
|---|
| 251 | |
|---|
| 252 | while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); } |
|---|
| 253 | while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext); } |
|---|
| 254 | while (arguments.read("-d")) { viewer.setThreadingModel(osgViewer::Viewer::DrawThreadPerContext); } |
|---|
| 255 | while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext); } |
|---|
| 256 | |
|---|
| 257 | bool limitNumberOfFrames = false; |
|---|
| 258 | unsigned int maxFrames = 10; |
|---|
| 259 | while (arguments.read("--run-till-frame-number",maxFrames)) { limitNumberOfFrames = true; } |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | while (arguments.read("-1")) { singleWindowMultipleCameras(viewer); } |
|---|
| 263 | while (arguments.read("-2")) { multipleWindowMultipleCameras(viewer); } |
|---|
| 264 | |
|---|
| 265 | if (apm.valid()) viewer.setCameraManipulator(apm.get()); |
|---|
| 266 | else viewer.setCameraManipulator( new osgGA::TrackballManipulator() ); |
|---|
| 267 | |
|---|
| 268 | std::string configfile; |
|---|
| 269 | while (arguments.read("--config", configfile)) |
|---|
| 270 | { |
|---|
| 271 | osg::notify(osg::NOTICE)<<"Trying to read config file "<<configfile<<std::endl; |
|---|
| 272 | osg::ref_ptr<osg::Object> object = osgDB::readObjectFile(configfile); |
|---|
| 273 | osgViewer::View* view = dynamic_cast<osgViewer::View*>(object.get()); |
|---|
| 274 | if (view) |
|---|
| 275 | { |
|---|
| 276 | osg::notify(osg::NOTICE)<<"Read config file succesfully"<<std::endl; |
|---|
| 277 | } |
|---|
| 278 | else |
|---|
| 279 | { |
|---|
| 280 | osg::notify(osg::NOTICE)<<"Failed to read config file : "<<configfile<<std::endl; |
|---|
| 281 | return 1; |
|---|
| 282 | } |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | while (arguments.read("--write-config", configfile)) { osgDB::writeObjectFile(viewer, configfile); } |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | #if 0 |
|---|
| 289 | |
|---|
| 290 | ModelHandler* modelHandler = new ModelHandler; |
|---|
| 291 | for(int i=1; i<arguments.argc();++i) |
|---|
| 292 | { |
|---|
| 293 | modelHandler->add(arguments[i]); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | viewer.addEventHandler(modelHandler); |
|---|
| 297 | |
|---|
| 298 | #else |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 302 | if (!loadedModel) |
|---|
| 303 | { |
|---|
| 304 | std::cout << argv[0] <<": No data loaded." << std::endl; |
|---|
| 305 | return 1; |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | viewer.setSceneData(loadedModel.get()); |
|---|
| 309 | #endif |
|---|
| 310 | |
|---|
| 311 | viewer.realize(); |
|---|
| 312 | |
|---|
| 313 | unsigned int numFrames = 0; |
|---|
| 314 | while(!viewer.done() && !(limitNumberOfFrames && numFrames>=maxFrames)) |
|---|
| 315 | { |
|---|
| 316 | viewer.frame(); |
|---|
| 317 | ++numFrames; |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | return 0; |
|---|
| 321 | } |
|---|
| 322 | #else |
|---|
| 323 | |
|---|
| 324 | #include <osgViewer/Viewer> |
|---|
| 325 | #include <osgDB/ReadFile> |
|---|
| 326 | #include <osgDB/WriteFile> |
|---|
| 327 | |
|---|
| 328 | int main( int, char **) |
|---|
| 329 | { |
|---|
| 330 | osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("cow.osg"); |
|---|
| 331 | |
|---|
| 332 | for(unsigned int i=0; i<5; ++i) |
|---|
| 333 | { |
|---|
| 334 | osg::notify(osg::NOTICE)<<"New frame *******************************"<<std::endl; |
|---|
| 335 | |
|---|
| 336 | osgViewer::Viewer viewer; |
|---|
| 337 | viewer.setSceneData(model.get()); |
|---|
| 338 | viewer.run(); |
|---|
| 339 | osg::notify(osg::NOTICE)<<std::endl<<std::endl; |
|---|
| 340 | } |
|---|
| 341 | return 0; |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | #endif |
|---|