| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | #include <osg/Timer> |
|---|
| 7 | #include <osg/GraphicsContext> |
|---|
| 8 | #include <osg/GraphicsThread> |
|---|
| 9 | |
|---|
| 10 | #include <osgUtil/UpdateVisitor> |
|---|
| 11 | #include <osgUtil/CullVisitor> |
|---|
| 12 | #include <osgUtil/SceneView> |
|---|
| 13 | #include <osgUtil/GLObjectsVisitor> |
|---|
| 14 | |
|---|
| 15 | #include <osgDB/ReadFile> |
|---|
| 16 | #include <osgDB/DynamicLibrary> |
|---|
| 17 | #include <osgDB/Registry> |
|---|
| 18 | |
|---|
| 19 | #include <map> |
|---|
| 20 | #include <list> |
|---|
| 21 | #include <iostream> |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | struct CompileOperation : public osg::GraphicsThread::Operation |
|---|
| 37 | { |
|---|
| 38 | CompileOperation(osg::Node* scene): |
|---|
| 39 | osg::GraphicsThread::Operation("Compile",false), |
|---|
| 40 | _scene(scene) |
|---|
| 41 | { |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | virtual void operator () (osg::GraphicsContext* context) |
|---|
| 45 | { |
|---|
| 46 | std::cout<<"Compile"<<std::endl; |
|---|
| 47 | |
|---|
| 48 | osgUtil::GLObjectsVisitor compileVisitor; |
|---|
| 49 | compileVisitor.setState(context->getState()); |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | _scene->accept(compileVisitor); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | osg::ref_ptr<osg::Node> _scene; |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | struct CullOperation : public osg::GraphicsThread::Operation |
|---|
| 60 | { |
|---|
| 61 | CullOperation(osgUtil::SceneView* sceneView): |
|---|
| 62 | osg::GraphicsThread::Operation("Cull",true), |
|---|
| 63 | _sceneView(sceneView) |
|---|
| 64 | { |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | virtual void operator () (osg::GraphicsContext* context) |
|---|
| 68 | { |
|---|
| 69 | _sceneView->setState(context->getState()); |
|---|
| 70 | _sceneView->cull(); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | osg::ref_ptr<osgUtil::SceneView> _sceneView; |
|---|
| 74 | }; |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | struct DrawOperation : public osg::GraphicsThread::Operation |
|---|
| 78 | { |
|---|
| 79 | DrawOperation(osgUtil::SceneView* sceneView): |
|---|
| 80 | osg::GraphicsThread::Operation("Draw",true), |
|---|
| 81 | _sceneView(sceneView) |
|---|
| 82 | { |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | virtual void operator () (osg::GraphicsContext*) |
|---|
| 86 | { |
|---|
| 87 | _sceneView->draw(); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | osg::ref_ptr<osgUtil::SceneView> _sceneView; |
|---|
| 91 | }; |
|---|
| 92 | |
|---|
| 93 | struct CleanUpOperation : public osg::GraphicsThread::Operation |
|---|
| 94 | { |
|---|
| 95 | CleanUpOperation(osgUtil::SceneView* sceneView): |
|---|
| 96 | osg::GraphicsThread::Operation("CleanUp",false), |
|---|
| 97 | _sceneView(sceneView) |
|---|
| 98 | { |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | virtual void operator () (osg::GraphicsContext*) |
|---|
| 102 | { |
|---|
| 103 | _sceneView->releaseAllGLObjects(); |
|---|
| 104 | _sceneView->flushAllDeletedGLObjects(); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | osg::ref_ptr<osgUtil::SceneView> _sceneView; |
|---|
| 108 | }; |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | int main( int argc, char **argv ) |
|---|
| 132 | { |
|---|
| 133 | osg::Referenced::setThreadSafeReferenceCounting(true); |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 137 | |
|---|
| 138 | std::string windowingLibrary("osgProducer"); |
|---|
| 139 | while (arguments.read("--windowing",windowingLibrary)) {} |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | osg::ref_ptr<osgDB::DynamicLibrary> windowingLib = |
|---|
| 143 | osgDB::DynamicLibrary::loadLibrary(osgDB::Registry::instance()->createLibraryNameForNodeKit(windowingLibrary)); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | if (!windowingLib) |
|---|
| 147 | { |
|---|
| 148 | std::cout<<"Error: failed to loading windowing library: "<<windowingLibrary<<std::endl; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | unsigned int numberCameras = 3; |
|---|
| 152 | while (arguments.read("--cameras",numberCameras)) {} |
|---|
| 153 | |
|---|
| 154 | unsigned int xpos = 0; |
|---|
| 155 | unsigned int ypos = 400; |
|---|
| 156 | unsigned int width = 400; |
|---|
| 157 | unsigned int height = 400; |
|---|
| 158 | |
|---|
| 159 | while (arguments.read("--xpos",xpos)) {} |
|---|
| 160 | while (arguments.read("--ypos",ypos)) {} |
|---|
| 161 | while (arguments.read("--height",height)) {} |
|---|
| 162 | while (arguments.read("--width",width)) {} |
|---|
| 163 | |
|---|
| 164 | unsigned int maxNumFrames = 1000; |
|---|
| 165 | while (arguments.read("--max-num-frames",maxNumFrames)) {} |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 169 | if (!loadedModel) |
|---|
| 170 | { |
|---|
| 171 | std::cout << argv[0] <<": No data loaded." << std::endl; |
|---|
| 172 | return 1; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | osg::ref_ptr<osg::FrameStamp> frameStamp = new osg::FrameStamp; |
|---|
| 178 | |
|---|
| 179 | unsigned int frameNum = 0; |
|---|
| 180 | |
|---|
| 181 | osgUtil::UpdateVisitor updateVisitor; |
|---|
| 182 | updateVisitor.setFrameStamp(frameStamp.get()); |
|---|
| 183 | |
|---|
| 184 | typedef std::list< osg::ref_ptr<osg::CameraNode> > CameraList; |
|---|
| 185 | typedef std::set< osg::GraphicsContext* > GraphicsContextSet; |
|---|
| 186 | |
|---|
| 187 | CameraList cameraList; |
|---|
| 188 | GraphicsContextSet graphicsContextSet; |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | bool shareContexts = false; |
|---|
| 192 | osg::GraphicsContext* previousContext = 0; |
|---|
| 193 | for(unsigned int i=0; i< numberCameras; ++i) |
|---|
| 194 | { |
|---|
| 195 | osg::ref_ptr<osg::CameraNode> camera = new osg::CameraNode; |
|---|
| 196 | camera->addChild(loadedModel.get()); |
|---|
| 197 | |
|---|
| 198 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 199 | traits->_windowName = "osgcamera"; |
|---|
| 200 | traits->_x = xpos; |
|---|
| 201 | traits->_y = ypos; |
|---|
| 202 | traits->_width = width; |
|---|
| 203 | traits->_height = height; |
|---|
| 204 | traits->_windowDecoration = true; |
|---|
| 205 | traits->_doubleBuffer = true; |
|---|
| 206 | traits->_sharedContext = shareContexts ? previousContext : 0; |
|---|
| 207 | |
|---|
| 208 | xpos += width; |
|---|
| 209 | |
|---|
| 210 | osg::ref_ptr<osg::GraphicsContext> gfxc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 211 | |
|---|
| 212 | if (!gfxc) |
|---|
| 213 | { |
|---|
| 214 | std::cout<<"Unable to create window."<<std::endl; |
|---|
| 215 | return 1; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | camera->setGraphicsContext(gfxc.get()); |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | const osg::BoundingSphere& bs = loadedModel->getBound(); |
|---|
| 222 | osg::Matrix viewMatrix; |
|---|
| 223 | viewMatrix.makeLookAt(bs.center()-osg::Vec3(0.0,2.0f*bs.radius(),0.0),bs.center(),osg::Vec3(0.0f,0.0f,1.0f)); |
|---|
| 224 | |
|---|
| 225 | camera->setViewport(0,0,traits->_width,traits->_height); |
|---|
| 226 | camera->setProjectionMatrixAsPerspective(50.0f,1.4f,1.0f,10000.0f); |
|---|
| 227 | camera->setViewMatrix(viewMatrix); |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | gfxc->createGraphicsThread(); |
|---|
| 231 | |
|---|
| 232 | cameraList.push_back(camera); |
|---|
| 233 | |
|---|
| 234 | previousContext = gfxc.get(); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | CameraList::iterator citr; |
|---|
| 240 | for(citr = cameraList.begin(); |
|---|
| 241 | citr != cameraList.end(); |
|---|
| 242 | ++citr) |
|---|
| 243 | { |
|---|
| 244 | graphicsContextSet.insert(const_cast<osg::GraphicsContext*>((*citr)->getGraphicsContext())); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | std::cout<<"Number of cameras = "<<cameraList.size()<<std::endl; |
|---|
| 249 | std::cout<<"Number of graphics contexts = "<<graphicsContextSet.size()<<std::endl; |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | GraphicsContextSet::iterator gitr; |
|---|
| 254 | osg::ref_ptr<CompileOperation> compileOp = new CompileOperation(loadedModel.get()); |
|---|
| 255 | for(gitr = graphicsContextSet.begin(); |
|---|
| 256 | gitr != graphicsContextSet.end(); |
|---|
| 257 | ++gitr) |
|---|
| 258 | { |
|---|
| 259 | osg::GraphicsContext* context = *gitr; |
|---|
| 260 | context->getGraphicsThread()->add(compileOp.get(), false); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | osg::ref_ptr<osg::BarrierOperation> frameBeginBarrierOp = new osg::BarrierOperation(graphicsContextSet.size()+1, osg::BarrierOperation::NO_OPERATION); |
|---|
| 266 | for(gitr = graphicsContextSet.begin(); |
|---|
| 267 | gitr != graphicsContextSet.end(); |
|---|
| 268 | ++gitr) |
|---|
| 269 | { |
|---|
| 270 | osg::GraphicsContext* context = *gitr; |
|---|
| 271 | context->getGraphicsThread()->add(frameBeginBarrierOp.get(), false); |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | osg::ref_ptr<osg::BarrierOperation> glFinishBarrierOp = new osg::BarrierOperation(graphicsContextSet.size(), osg::BarrierOperation::GL_FINISH); |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | bool doFinishBeforeNewDraw = false; |
|---|
| 280 | bool doFinishAfterSwap = false; |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | for(citr = cameraList.begin(); |
|---|
| 284 | citr != cameraList.end(); |
|---|
| 285 | ++citr) |
|---|
| 286 | { |
|---|
| 287 | osg::CameraNode* camera = citr->get(); |
|---|
| 288 | osg::GraphicsThread* graphicsThread = camera->getGraphicsContext()->getGraphicsThread(); |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | osgUtil::SceneView* sceneView = new osgUtil::SceneView; |
|---|
| 292 | sceneView->setDefaults(); |
|---|
| 293 | sceneView->setFrameStamp(frameStamp.get()); |
|---|
| 294 | sceneView->setCamera(camera); |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | graphicsThread->add( new CullOperation(sceneView), false); |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | if (doFinishBeforeNewDraw) graphicsThread->add( glFinishBarrierOp.get(), false); |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | graphicsThread->add( new DrawOperation(sceneView), false); |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | osg::ref_ptr<osg::BarrierOperation> frameEndBarrierOp = new osg::BarrierOperation(graphicsContextSet.size()+1, osg::BarrierOperation::NO_OPERATION); |
|---|
| 312 | osg::ref_ptr<osg::BarrierOperation> preSwapBarrierOp = new osg::BarrierOperation(graphicsContextSet.size(), osg::BarrierOperation::GL_FLUSH); |
|---|
| 313 | osg::ref_ptr<osg::SwapBuffersOperation> swapOp = new osg::SwapBuffersOperation(); |
|---|
| 314 | for(gitr = graphicsContextSet.begin(); |
|---|
| 315 | gitr != graphicsContextSet.end(); |
|---|
| 316 | ++gitr) |
|---|
| 317 | { |
|---|
| 318 | osg::GraphicsContext* context = *gitr; |
|---|
| 319 | |
|---|
| 320 | context->getGraphicsThread()->add(frameEndBarrierOp.get(), false); |
|---|
| 321 | |
|---|
| 322 | context->getGraphicsThread()->add(swapOp.get(), false); |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | if (doFinishAfterSwap) context->getGraphicsThread()->add(glFinishBarrierOp.get(), false); |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | osg::Timer_t start_tick = osg::Timer::instance()->tick(); |
|---|
| 331 | osg::Timer_t previous_tick = start_tick; |
|---|
| 332 | |
|---|
| 333 | bool done = false; |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | while( !done && frameNum<maxNumFrames) |
|---|
| 337 | { |
|---|
| 338 | |
|---|
| 339 | osg::Timer_t current_tick = osg::Timer::instance()->tick(); |
|---|
| 340 | |
|---|
| 341 | frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,current_tick)); |
|---|
| 342 | frameStamp->setFrameNumber(frameNum++); |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | previous_tick = current_tick; |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | loadedModel->accept(updateVisitor); |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | frameBeginBarrierOp->block(); |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | frameEndBarrierOp->block(); |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | for(gitr = graphicsContextSet.begin(); |
|---|
| 359 | gitr != graphicsContextSet.end(); |
|---|
| 360 | ++gitr) |
|---|
| 361 | { |
|---|
| 362 | osg::GraphicsContext* context = *gitr; |
|---|
| 363 | if (!context->isRealized()) done = true; |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | std::cout<<"Exiting application"<<std::endl; |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | for(gitr = graphicsContextSet.begin(); |
|---|
| 373 | gitr != graphicsContextSet.end(); |
|---|
| 374 | ++gitr) |
|---|
| 375 | { |
|---|
| 376 | osg::GraphicsContext* context = *gitr; |
|---|
| 377 | osg::GraphicsThread* thread = context->getGraphicsThread(); |
|---|
| 378 | if (thread) |
|---|
| 379 | { |
|---|
| 380 | thread->removeAllOperations(); |
|---|
| 381 | thread->setDone(true); |
|---|
| 382 | } |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | std::cout<<"Removed all operations"<<std::endl; |
|---|
| 386 | |
|---|
| 387 | return 0; |
|---|
| 388 | } |
|---|