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