| [6941] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| [1697] | 19 | #ifdef USE_MEM_CHECK |
|---|
| 20 | #include <mcheck.h> |
|---|
| 21 | #endif |
|---|
| 22 | |
|---|
| 23 | #include <osg/Group> |
|---|
| 24 | #include <osg/Notify> |
|---|
| 25 | |
|---|
| 26 | #include <osgDB/Registry> |
|---|
| 27 | #include <osgDB/ReadFile> |
|---|
| 28 | |
|---|
| [5921] | 29 | #include <osgGA/TrackballManipulator> |
|---|
| [11622] | 30 | #include <osgGA/StateSetManipulator> |
|---|
| [5920] | 31 | #include <osgViewer/Viewer> |
|---|
| [11622] | 32 | #include <osgViewer/ViewerEventHandlers> |
|---|
| [1697] | 33 | |
|---|
| 34 | #include <osg/Quat> |
|---|
| [4475] | 35 | #include <osg/io_utils> |
|---|
| [1697] | 36 | |
|---|
| [5920] | 37 | #include <iostream> |
|---|
| 38 | |
|---|
| [3230] | 39 | #if defined (WIN32) && !defined(__CYGWIN__) |
|---|
| [1697] | 40 | #include <winsock.h> |
|---|
| 41 | #endif |
|---|
| 42 | |
|---|
| 43 | #include "receiver.h" |
|---|
| 44 | #include "broadcaster.h" |
|---|
| 45 | |
|---|
| 46 | |
|---|
| [4472] | 47 | const unsigned int MAX_NUM_EVENTS = 10; |
|---|
| [4473] | 48 | const unsigned int SWAP_BYTES_COMPARE = 0x12345678; |
|---|
| [1697] | 49 | class CameraPacket { |
|---|
| 50 | public: |
|---|
| 51 | |
|---|
| [4470] | 52 | |
|---|
| [1697] | 53 | CameraPacket():_masterKilled(false) |
|---|
| [2371] | 54 | { |
|---|
| [4473] | 55 | _byte_order = SWAP_BYTES_COMPARE; |
|---|
| [2371] | 56 | } |
|---|
| [1697] | 57 | |
|---|
| [1704] | 58 | void setPacket(const osg::Matrix& matrix,const osg::FrameStamp* frameStamp) |
|---|
| [1697] | 59 | { |
|---|
| [1704] | 60 | _matrix = matrix; |
|---|
| [1697] | 61 | if (frameStamp) |
|---|
| 62 | { |
|---|
| 63 | _frameStamp = *frameStamp; |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| [1704] | 67 | void getModelView(osg::Matrix& matrix,float angle_offset=0.0f) |
|---|
| [1697] | 68 | { |
|---|
| 69 | |
|---|
| [2371] | 70 | matrix = _matrix * osg::Matrix::rotate(osg::DegreesToRadians(angle_offset),0.0f,1.0f,0.0f); |
|---|
| [1697] | 71 | } |
|---|
| 72 | |
|---|
| [5920] | 73 | void readEventQueue(osgViewer::Viewer& viewer); |
|---|
| [4470] | 74 | |
|---|
| [5920] | 75 | void writeEventQueue(osgViewer::Viewer& viewer); |
|---|
| [2371] | 76 | |
|---|
| [1697] | 77 | void setMasterKilled(const bool flag) { _masterKilled = flag; } |
|---|
| 78 | const bool getMasterKilled() const { return _masterKilled; } |
|---|
| 79 | |
|---|
| [3809] | 80 | unsigned int _byte_order; |
|---|
| [1697] | 81 | bool _masterKilled; |
|---|
| 82 | osg::Matrix _matrix; |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | osg::FrameStamp _frameStamp; |
|---|
| 89 | |
|---|
| [5032] | 90 | osgGA::EventQueue::Events _events; |
|---|
| [4470] | 91 | |
|---|
| [1697] | 92 | }; |
|---|
| 93 | |
|---|
| [4473] | 94 | class DataConverter |
|---|
| 95 | { |
|---|
| 96 | public: |
|---|
| 97 | |
|---|
| 98 | DataConverter(unsigned int numBytes): |
|---|
| 99 | _startPtr(0), |
|---|
| 100 | _endPtr(0), |
|---|
| 101 | _swapBytes(false), |
|---|
| 102 | _currentPtr(0) |
|---|
| 103 | { |
|---|
| 104 | _currentPtr = _startPtr = new char[numBytes]; |
|---|
| 105 | _endPtr = _startPtr+numBytes; |
|---|
| 106 | _numBytes = numBytes; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | char* _startPtr; |
|---|
| 110 | char* _endPtr; |
|---|
| 111 | unsigned int _numBytes; |
|---|
| 112 | bool _swapBytes; |
|---|
| 113 | |
|---|
| 114 | char* _currentPtr; |
|---|
| [4474] | 115 | |
|---|
| 116 | void reset() |
|---|
| 117 | { |
|---|
| 118 | _currentPtr = _startPtr; |
|---|
| 119 | } |
|---|
| [4473] | 120 | |
|---|
| 121 | inline void write1(char* ptr) |
|---|
| 122 | { |
|---|
| 123 | if (_currentPtr+1>=_endPtr) return; |
|---|
| 124 | |
|---|
| 125 | *(_currentPtr++) = *(ptr); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | inline void read1(char* ptr) |
|---|
| 129 | { |
|---|
| 130 | if (_currentPtr+1>=_endPtr) return; |
|---|
| 131 | |
|---|
| 132 | *(ptr) = *(_currentPtr++); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | inline void write2(char* ptr) |
|---|
| 136 | { |
|---|
| 137 | if (_currentPtr+2>=_endPtr) return; |
|---|
| 138 | |
|---|
| 139 | *(_currentPtr++) = *(ptr++); |
|---|
| 140 | *(_currentPtr++) = *(ptr); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | inline void read2(char* ptr) |
|---|
| 144 | { |
|---|
| 145 | if (_currentPtr+2>=_endPtr) return; |
|---|
| 146 | |
|---|
| 147 | if (_swapBytes) |
|---|
| 148 | { |
|---|
| 149 | *(ptr+1) = *(_currentPtr++); |
|---|
| 150 | *(ptr) = *(_currentPtr++); |
|---|
| 151 | } |
|---|
| 152 | else |
|---|
| 153 | { |
|---|
| 154 | *(ptr++) = *(_currentPtr++); |
|---|
| 155 | *(ptr) = *(_currentPtr++); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | inline void write4(char* ptr) |
|---|
| 160 | { |
|---|
| 161 | if (_currentPtr+4>=_endPtr) return; |
|---|
| 162 | |
|---|
| 163 | *(_currentPtr++) = *(ptr++); |
|---|
| 164 | *(_currentPtr++) = *(ptr++); |
|---|
| 165 | *(_currentPtr++) = *(ptr++); |
|---|
| 166 | *(_currentPtr++) = *(ptr); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | inline void read4(char* ptr) |
|---|
| 170 | { |
|---|
| 171 | if (_currentPtr+4>=_endPtr) return; |
|---|
| 172 | |
|---|
| 173 | if (_swapBytes) |
|---|
| 174 | { |
|---|
| 175 | *(ptr+3) = *(_currentPtr++); |
|---|
| 176 | *(ptr+2) = *(_currentPtr++); |
|---|
| 177 | *(ptr+1) = *(_currentPtr++); |
|---|
| 178 | *(ptr) = *(_currentPtr++); |
|---|
| 179 | } |
|---|
| 180 | else |
|---|
| 181 | { |
|---|
| 182 | *(ptr++) = *(_currentPtr++); |
|---|
| 183 | *(ptr++) = *(_currentPtr++); |
|---|
| 184 | *(ptr++) = *(_currentPtr++); |
|---|
| 185 | *(ptr) = *(_currentPtr++); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | inline void write8(char* ptr) |
|---|
| 190 | { |
|---|
| 191 | if (_currentPtr+8>=_endPtr) return; |
|---|
| 192 | |
|---|
| 193 | *(_currentPtr++) = *(ptr++); |
|---|
| 194 | *(_currentPtr++) = *(ptr++); |
|---|
| 195 | *(_currentPtr++) = *(ptr++); |
|---|
| 196 | *(_currentPtr++) = *(ptr++); |
|---|
| 197 | |
|---|
| 198 | *(_currentPtr++) = *(ptr++); |
|---|
| 199 | *(_currentPtr++) = *(ptr++); |
|---|
| 200 | *(_currentPtr++) = *(ptr++); |
|---|
| 201 | *(_currentPtr++) = *(ptr); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | inline void read8(char* ptr) |
|---|
| 205 | { |
|---|
| 206 | char* endPtr = _currentPtr+8; |
|---|
| 207 | if (endPtr>=_endPtr) return; |
|---|
| 208 | |
|---|
| 209 | if (_swapBytes) |
|---|
| 210 | { |
|---|
| 211 | *(ptr+7) = *(_currentPtr++); |
|---|
| 212 | *(ptr+6) = *(_currentPtr++); |
|---|
| 213 | *(ptr+5) = *(_currentPtr++); |
|---|
| 214 | *(ptr+4) = *(_currentPtr++); |
|---|
| 215 | |
|---|
| 216 | *(ptr+3) = *(_currentPtr++); |
|---|
| 217 | *(ptr+2) = *(_currentPtr++); |
|---|
| 218 | *(ptr+1) = *(_currentPtr++); |
|---|
| 219 | *(ptr) = *(_currentPtr++); |
|---|
| 220 | } |
|---|
| 221 | else |
|---|
| 222 | { |
|---|
| 223 | *(ptr++) = *(_currentPtr++); |
|---|
| 224 | *(ptr++) = *(_currentPtr++); |
|---|
| 225 | *(ptr++) = *(_currentPtr++); |
|---|
| 226 | *(ptr++) = *(_currentPtr++); |
|---|
| 227 | |
|---|
| 228 | *(ptr++) = *(_currentPtr++); |
|---|
| 229 | *(ptr++) = *(_currentPtr++); |
|---|
| 230 | *(ptr++) = *(_currentPtr++); |
|---|
| 231 | *(ptr) = *(_currentPtr++); |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | inline void writeChar(char c) { write1(&c); } |
|---|
| 236 | inline void writeUChar(unsigned char c) { write1((char*)&c); } |
|---|
| 237 | inline void writeShort(short c) { write2((char*)&c); } |
|---|
| 238 | inline void writeUShort(unsigned short c) { write2((char*)&c); } |
|---|
| 239 | inline void writeInt(int c) { write4((char*)&c); } |
|---|
| 240 | inline void writeUInt(unsigned int c) { write4((char*)&c); } |
|---|
| 241 | inline void writeFloat(float c) { write4((char*)&c); } |
|---|
| 242 | inline void writeDouble(double c) { write8((char*)&c); } |
|---|
| 243 | |
|---|
| 244 | inline char readChar() { char c; read1(&c); return c; } |
|---|
| 245 | inline unsigned char readUChar() { unsigned char c; read1((char*)&c); return c; } |
|---|
| 246 | inline short readShort() { short c; read2((char*)&c); return c; } |
|---|
| 247 | inline unsigned short readUShort() { unsigned short c; read2((char*)&c); return c; } |
|---|
| 248 | inline int readInt() { int c; read4((char*)&c); return c; } |
|---|
| 249 | inline unsigned int readUInt() { unsigned int c; read4((char*)&c); return c; } |
|---|
| 250 | inline float readFloat() { float c; read4((char*)&c); return c; } |
|---|
| 251 | inline double readDouble() { double c; read8((char*)&c); return c; } |
|---|
| 252 | |
|---|
| 253 | void write(const osg::FrameStamp& fs) |
|---|
| 254 | { |
|---|
| [4481] | 255 | osg::notify(osg::NOTICE)<<"writeFramestamp = "<<fs.getFrameNumber()<<" "<<fs.getReferenceTime()<<std::endl; |
|---|
| 256 | |
|---|
| [4473] | 257 | writeUInt(fs.getFrameNumber()); |
|---|
| [4736] | 258 | writeDouble(fs.getReferenceTime()); |
|---|
| [6051] | 259 | writeDouble(fs.getSimulationTime()); |
|---|
| [4473] | 260 | } |
|---|
| 261 | |
|---|
| 262 | void read(osg::FrameStamp& fs) |
|---|
| 263 | { |
|---|
| 264 | fs.setFrameNumber(readUInt()); |
|---|
| 265 | fs.setReferenceTime(readDouble()); |
|---|
| [6051] | 266 | fs.setSimulationTime(readDouble()); |
|---|
| [4481] | 267 | |
|---|
| 268 | osg::notify(osg::NOTICE)<<"readFramestamp = "<<fs.getFrameNumber()<<" "<<fs.getReferenceTime()<<std::endl; |
|---|
| [4473] | 269 | } |
|---|
| 270 | |
|---|
| 271 | void write(const osg::Matrix& matrix) |
|---|
| 272 | { |
|---|
| 273 | writeDouble(matrix(0,0)); |
|---|
| 274 | writeDouble(matrix(0,1)); |
|---|
| 275 | writeDouble(matrix(0,2)); |
|---|
| 276 | writeDouble(matrix(0,3)); |
|---|
| 277 | |
|---|
| 278 | writeDouble(matrix(1,0)); |
|---|
| 279 | writeDouble(matrix(1,1)); |
|---|
| 280 | writeDouble(matrix(1,2)); |
|---|
| 281 | writeDouble(matrix(1,3)); |
|---|
| 282 | |
|---|
| 283 | writeDouble(matrix(2,0)); |
|---|
| 284 | writeDouble(matrix(2,1)); |
|---|
| 285 | writeDouble(matrix(2,2)); |
|---|
| 286 | writeDouble(matrix(2,3)); |
|---|
| 287 | |
|---|
| 288 | writeDouble(matrix(3,0)); |
|---|
| 289 | writeDouble(matrix(3,1)); |
|---|
| 290 | writeDouble(matrix(3,2)); |
|---|
| 291 | writeDouble(matrix(3,3)); |
|---|
| [4481] | 292 | |
|---|
| 293 | osg::notify(osg::NOTICE)<<"writeMatrix = "<<matrix<<std::endl; |
|---|
| 294 | |
|---|
| [4473] | 295 | } |
|---|
| 296 | |
|---|
| 297 | void read(osg::Matrix& matrix) |
|---|
| 298 | { |
|---|
| 299 | matrix(0,0) = readDouble(); |
|---|
| 300 | matrix(0,1) = readDouble(); |
|---|
| 301 | matrix(0,2) = readDouble(); |
|---|
| 302 | matrix(0,3) = readDouble(); |
|---|
| 303 | |
|---|
| 304 | matrix(1,0) = readDouble(); |
|---|
| 305 | matrix(1,1) = readDouble(); |
|---|
| 306 | matrix(1,2) = readDouble(); |
|---|
| 307 | matrix(1,3) = readDouble(); |
|---|
| 308 | |
|---|
| 309 | matrix(2,0) = readDouble(); |
|---|
| 310 | matrix(2,1) = readDouble(); |
|---|
| 311 | matrix(2,2) = readDouble(); |
|---|
| 312 | matrix(2,3) = readDouble(); |
|---|
| 313 | |
|---|
| 314 | matrix(3,0) = readDouble(); |
|---|
| 315 | matrix(3,1) = readDouble(); |
|---|
| 316 | matrix(3,2) = readDouble(); |
|---|
| 317 | matrix(3,3) = readDouble(); |
|---|
| [4481] | 318 | |
|---|
| 319 | osg::notify(osg::NOTICE)<<"readMatrix = "<<matrix<<std::endl; |
|---|
| 320 | |
|---|
| [4473] | 321 | } |
|---|
| 322 | |
|---|
| [5032] | 323 | void write(const osgGA::GUIEventAdapter& event) |
|---|
| [4473] | 324 | { |
|---|
| [5032] | 325 | writeUInt(event.getEventType()); |
|---|
| 326 | writeUInt(event.getKey()); |
|---|
| 327 | writeUInt(event.getButton()); |
|---|
| [5592] | 328 | writeInt(event.getWindowX()); |
|---|
| 329 | writeInt(event.getWindowY()); |
|---|
| 330 | writeUInt(event.getWindowWidth()); |
|---|
| 331 | writeUInt(event.getWindowHeight()); |
|---|
| [5032] | 332 | writeFloat(event.getXmin()); |
|---|
| [5592] | 333 | writeFloat(event.getYmin()); |
|---|
| [5032] | 334 | writeFloat(event.getXmax()); |
|---|
| 335 | writeFloat(event.getYmax()); |
|---|
| 336 | writeFloat(event.getX()); |
|---|
| 337 | writeFloat(event.getY()); |
|---|
| 338 | writeUInt(event.getButtonMask()); |
|---|
| 339 | writeUInt(event.getModKeyMask()); |
|---|
| 340 | writeDouble(event.getTime()); |
|---|
| [4473] | 341 | } |
|---|
| 342 | |
|---|
| [5032] | 343 | void read(osgGA::GUIEventAdapter& event) |
|---|
| [4473] | 344 | { |
|---|
| [5032] | 345 | event.setEventType((osgGA::GUIEventAdapter::EventType)readUInt()); |
|---|
| 346 | event.setKey(readUInt()); |
|---|
| 347 | event.setButton(readUInt()); |
|---|
| [5592] | 348 | int x = readInt(); |
|---|
| 349 | int y = readInt(); |
|---|
| 350 | int width = readUInt(); |
|---|
| 351 | int height = readUInt(); |
|---|
| 352 | event.setWindowRectangle(x,y,width,height); |
|---|
| 353 | float xmin = readFloat(); |
|---|
| 354 | float ymin = readFloat(); |
|---|
| 355 | float xmax = readFloat(); |
|---|
| 356 | float ymax = readFloat(); |
|---|
| 357 | event.setInputRange(xmin,ymin,xmax,ymax); |
|---|
| [5032] | 358 | event.setX(readFloat()); |
|---|
| 359 | event.setY(readFloat()); |
|---|
| 360 | event.setButtonMask(readUInt()); |
|---|
| 361 | event.setModKeyMask(readUInt()); |
|---|
| 362 | event.setTime(readDouble()); |
|---|
| [4473] | 363 | } |
|---|
| 364 | |
|---|
| 365 | void write(CameraPacket& cameraPacket) |
|---|
| 366 | { |
|---|
| 367 | writeUInt(cameraPacket._byte_order); |
|---|
| 368 | |
|---|
| 369 | writeUInt(cameraPacket._masterKilled); |
|---|
| 370 | |
|---|
| 371 | write(cameraPacket._matrix); |
|---|
| 372 | write(cameraPacket._frameStamp); |
|---|
| 373 | |
|---|
| 374 | writeUInt(cameraPacket._events.size()); |
|---|
| [5032] | 375 | for(osgGA::EventQueue::Events::iterator itr = cameraPacket._events.begin(); |
|---|
| 376 | itr != cameraPacket._events.end(); |
|---|
| 377 | ++itr) |
|---|
| [4473] | 378 | { |
|---|
| [5032] | 379 | write(*(*itr)); |
|---|
| [4473] | 380 | } |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | void read(CameraPacket& cameraPacket) |
|---|
| 384 | { |
|---|
| 385 | cameraPacket._byte_order = readUInt(); |
|---|
| [4474] | 386 | if (cameraPacket._byte_order != SWAP_BYTES_COMPARE) |
|---|
| 387 | { |
|---|
| 388 | _swapBytes = !_swapBytes; |
|---|
| 389 | } |
|---|
| [4473] | 390 | |
|---|
| [9637] | 391 | cameraPacket._masterKilled = readUInt()!=0; |
|---|
| [4473] | 392 | |
|---|
| 393 | read(cameraPacket._matrix); |
|---|
| 394 | read(cameraPacket._frameStamp); |
|---|
| 395 | |
|---|
| 396 | cameraPacket._events.clear(); |
|---|
| 397 | unsigned int numEvents = readUInt(); |
|---|
| 398 | for(unsigned int i=0;i<numEvents;++i) |
|---|
| 399 | { |
|---|
| [5032] | 400 | osgGA::GUIEventAdapter* event = new osgGA::GUIEventAdapter; |
|---|
| [4473] | 401 | read(*(event)); |
|---|
| 402 | cameraPacket._events.push_back(event); |
|---|
| 403 | } |
|---|
| 404 | } |
|---|
| 405 | }; |
|---|
| 406 | |
|---|
| [5920] | 407 | void CameraPacket::readEventQueue(osgViewer::Viewer& viewer) |
|---|
| [4470] | 408 | { |
|---|
| [4474] | 409 | _events.clear(); |
|---|
| 410 | |
|---|
| [11623] | 411 | osgViewer::ViewerBase::Contexts contexts; |
|---|
| 412 | viewer.getContexts(contexts); |
|---|
| 413 | |
|---|
| 414 | for(osgViewer::ViewerBase::Contexts::iterator citr =contexts.begin(); citr != contexts.end(); ++citr) |
|---|
| 415 | { |
|---|
| 416 | osgGA::EventQueue::Events gw_events; |
|---|
| 417 | |
|---|
| 418 | osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(*citr); |
|---|
| 419 | if (gw) |
|---|
| 420 | { |
|---|
| 421 | gw->checkEvents(); |
|---|
| 422 | gw->getEventQueue()->copyEvents(gw_events); |
|---|
| 423 | } |
|---|
| 424 | _events.insert(_events.end(), gw_events.begin(), gw_events.end()); |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| [5920] | 427 | viewer.getEventQueue()->copyEvents(_events); |
|---|
| [4470] | 428 | |
|---|
| [4473] | 429 | osg::notify(osg::INFO)<<"written events = "<<_events.size()<<std::endl; |
|---|
| [4470] | 430 | } |
|---|
| 431 | |
|---|
| [5920] | 432 | void CameraPacket::writeEventQueue(osgViewer::Viewer& viewer) |
|---|
| [4470] | 433 | { |
|---|
| [9692] | 434 | osg::notify(osg::INFO)<<"received events = "<<_events.size()<<std::endl; |
|---|
| [4470] | 435 | |
|---|
| [5920] | 436 | viewer.getEventQueue()->appendEvents(_events); |
|---|
| [4470] | 437 | } |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | |
|---|
| [1704] | 441 | enum ViewerMode |
|---|
| 442 | { |
|---|
| 443 | STAND_ALONE, |
|---|
| 444 | SLAVE, |
|---|
| 445 | MASTER |
|---|
| [1697] | 446 | }; |
|---|
| 447 | |
|---|
| 448 | int main( int argc, char **argv ) |
|---|
| 449 | { |
|---|
| 450 | |
|---|
| 451 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 452 | |
|---|
| 453 | |
|---|
| [5962] | 454 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to approach implementation of clustering."); |
|---|
| [1815] | 455 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| [1697] | 456 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| [1704] | 457 | arguments.getApplicationUsage()->addCommandLineOption("-m","Set viewer to MASTER mode, sending view via packets."); |
|---|
| [9692] | 458 | arguments.getApplicationUsage()->addCommandLineOption("-s","Set viewer to SLAVE mode, receiving view via packets."); |
|---|
| [1704] | 459 | arguments.getApplicationUsage()->addCommandLineOption("-n <int>","Socket number to transmit packets"); |
|---|
| 460 | arguments.getApplicationUsage()->addCommandLineOption("-f <float>","Field of view of camera"); |
|---|
| 461 | arguments.getApplicationUsage()->addCommandLineOption("-o <float>","Offset angle of camera"); |
|---|
| [1697] | 462 | |
|---|
| 463 | |
|---|
| [5920] | 464 | osgViewer::Viewer viewer; |
|---|
| [1697] | 465 | |
|---|
| 466 | |
|---|
| [1704] | 467 | |
|---|
| 468 | ViewerMode viewerMode = STAND_ALONE; |
|---|
| 469 | while (arguments.read("-m")) viewerMode = MASTER; |
|---|
| 470 | while (arguments.read("-s")) viewerMode = SLAVE; |
|---|
| 471 | |
|---|
| [4470] | 472 | int socketNumber=8100; |
|---|
| [1704] | 473 | while (arguments.read("-n",socketNumber)) ; |
|---|
| 474 | |
|---|
| [3511] | 475 | float camera_fov=-1.0f; |
|---|
| [2371] | 476 | while (arguments.read("-f",camera_fov)) |
|---|
| 477 | { |
|---|
| 478 | } |
|---|
| [1704] | 479 | |
|---|
| 480 | float camera_offset=45.0f; |
|---|
| 481 | while (arguments.read("-o",camera_offset)) ; |
|---|
| 482 | |
|---|
| 483 | |
|---|
| [1697] | 484 | |
|---|
| 485 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 486 | { |
|---|
| 487 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 488 | return 1; |
|---|
| 489 | } |
|---|
| 490 | |
|---|
| 491 | |
|---|
| 492 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | if (arguments.errors()) |
|---|
| 496 | { |
|---|
| 497 | arguments.writeErrorMessages(std::cout); |
|---|
| 498 | return 1; |
|---|
| 499 | } |
|---|
| 500 | |
|---|
| [1815] | 501 | if (arguments.argc()<=1) |
|---|
| 502 | { |
|---|
| 503 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 504 | return 1; |
|---|
| 505 | } |
|---|
| [5920] | 506 | |
|---|
| [1704] | 507 | |
|---|
| 508 | osg::ref_ptr<osg::Node> rootnode = osgDB::readNodeFiles(arguments); |
|---|
| [1697] | 509 | |
|---|
| 510 | |
|---|
| [1704] | 511 | viewer.setSceneData(rootnode.get()); |
|---|
| [1697] | 512 | |
|---|
| [3511] | 513 | if (camera_fov>0.0f) |
|---|
| 514 | { |
|---|
| [5920] | 515 | double fovy, aspectRatio, zNear, zFar; |
|---|
| 516 | viewer.getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio,zNear, zFar); |
|---|
| 517 | |
|---|
| 518 | double original_fov = atan(tan(osg::DegreesToRadians(fovy)*0.5)*aspectRatio)*2.0; |
|---|
| 519 | std::cout << "setting lens perspective : original "<<original_fov<<" "<<fovy<<std::endl; |
|---|
| 520 | |
|---|
| 521 | fovy = atan(tan(osg::DegreesToRadians(camera_fov)*0.5)/aspectRatio)*2.0; |
|---|
| 522 | viewer.getCamera()->setProjectionMatrixAsPerspective(fovy, aspectRatio,zNear, zFar); |
|---|
| 523 | |
|---|
| 524 | viewer.getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio,zNear, zFar); |
|---|
| 525 | original_fov = atan(tan(osg::DegreesToRadians(fovy)*0.5)*aspectRatio)*2.0; |
|---|
| 526 | std::cout << "setting lens perspective : new "<<original_fov<<" "<<fovy<<std::endl; |
|---|
| [3511] | 527 | } |
|---|
| 528 | |
|---|
| [5921] | 529 | viewer.setCameraManipulator(new osgGA::TrackballManipulator()); |
|---|
| [3511] | 530 | |
|---|
| [11622] | 531 | |
|---|
| 532 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| [5921] | 533 | |
|---|
| [11622] | 534 | |
|---|
| 535 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 536 | |
|---|
| 537 | |
|---|
| [5920] | 538 | |
|---|
| 539 | viewer.realize(); |
|---|
| 540 | |
|---|
| 541 | |
|---|
| [3728] | 542 | CameraPacket *cp = new CameraPacket; |
|---|
| [4473] | 543 | |
|---|
| [1706] | 544 | |
|---|
| [1704] | 545 | Broadcaster bc; |
|---|
| 546 | Receiver rc; |
|---|
| 547 | |
|---|
| [1960] | 548 | bc.setPort(static_cast<short int>(socketNumber)); |
|---|
| [4472] | 549 | rc.setPort(static_cast<short int>(socketNumber)); |
|---|
| [1960] | 550 | |
|---|
| 551 | bool masterKilled = false; |
|---|
| [4473] | 552 | |
|---|
| 553 | DataConverter scratchPad(1024); |
|---|
| [1960] | 554 | |
|---|
| 555 | while( !viewer.done() && !masterKilled ) |
|---|
| [1697] | 556 | { |
|---|
| [4470] | 557 | osg::Timer_t startTick = osg::Timer::instance()->tick(); |
|---|
| 558 | |
|---|
| [5920] | 559 | viewer.advance(); |
|---|
| 560 | |
|---|
| [1704] | 561 | |
|---|
| 562 | switch (viewerMode) |
|---|
| 563 | { |
|---|
| 564 | case(MASTER): |
|---|
| 565 | { |
|---|
| 566 | |
|---|
| 567 | |
|---|
| [5920] | 568 | osg::Matrix modelview(viewer.getCamera()->getViewMatrix()); |
|---|
| [1704] | 569 | |
|---|
| [3728] | 570 | cp->setPacket(modelview,viewer.getFrameStamp()); |
|---|
| [4470] | 571 | |
|---|
| 572 | cp->readEventQueue(viewer); |
|---|
| [1704] | 573 | |
|---|
| [4474] | 574 | scratchPad.reset(); |
|---|
| [4473] | 575 | scratchPad.write(*cp); |
|---|
| 576 | |
|---|
| [4475] | 577 | scratchPad.reset(); |
|---|
| 578 | scratchPad.read(*cp); |
|---|
| 579 | |
|---|
| [4473] | 580 | bc.setBuffer(scratchPad._startPtr, scratchPad._numBytes); |
|---|
| [3067] | 581 | |
|---|
| [4473] | 582 | std::cout << "bc.sync()"<<scratchPad._numBytes<<std::endl; |
|---|
| [4471] | 583 | |
|---|
| [3067] | 584 | bc.sync(); |
|---|
| [4470] | 585 | |
|---|
| [1704] | 586 | } |
|---|
| 587 | break; |
|---|
| 588 | case(SLAVE): |
|---|
| 589 | { |
|---|
| 590 | |
|---|
| [4473] | 591 | rc.setBuffer(scratchPad._startPtr, scratchPad._numBytes); |
|---|
| [1704] | 592 | |
|---|
| [1960] | 593 | rc.sync(); |
|---|
| [4473] | 594 | |
|---|
| [4474] | 595 | scratchPad.reset(); |
|---|
| [4473] | 596 | scratchPad.read(*cp); |
|---|
| [1960] | 597 | |
|---|
| [4472] | 598 | cp->writeEventQueue(viewer); |
|---|
| [1704] | 599 | |
|---|
| [3728] | 600 | if (cp->getMasterKilled()) |
|---|
| [1704] | 601 | { |
|---|
| [4472] | 602 | std::cout << "Received master killed."<<std::endl; |
|---|
| [1706] | 603 | |
|---|
| [1960] | 604 | masterKilled = true; |
|---|
| [1704] | 605 | } |
|---|
| 606 | } |
|---|
| 607 | break; |
|---|
| 608 | default: |
|---|
| 609 | |
|---|
| 610 | break; |
|---|
| 611 | } |
|---|
| 612 | |
|---|
| [4470] | 613 | osg::Timer_t endTick = osg::Timer::instance()->tick(); |
|---|
| 614 | |
|---|
| [4472] | 615 | osg::notify(osg::INFO)<<"Time to do cluster sync "<<osg::Timer::instance()->delta_m(startTick,endTick)<<std::endl; |
|---|
| [4470] | 616 | |
|---|
| 617 | |
|---|
| 618 | |
|---|
| [5920] | 619 | viewer.eventTraversal(); |
|---|
| 620 | viewer.updateTraversal(); |
|---|
| [4470] | 621 | |
|---|
| 622 | if (viewerMode==SLAVE) |
|---|
| 623 | { |
|---|
| 624 | osg::Matrix modelview; |
|---|
| 625 | cp->getModelView(modelview,camera_offset); |
|---|
| 626 | |
|---|
| [5920] | 627 | viewer.getCamera()->setViewMatrix(modelview); |
|---|
| [4470] | 628 | } |
|---|
| 629 | |
|---|
| [1697] | 630 | |
|---|
| [1960] | 631 | if(!masterKilled) |
|---|
| [5920] | 632 | viewer.renderingTraversals(); |
|---|
| [1697] | 633 | |
|---|
| 634 | } |
|---|
| [1960] | 635 | |
|---|
| [1704] | 636 | |
|---|
| 637 | if (viewerMode==MASTER) |
|---|
| 638 | { |
|---|
| 639 | |
|---|
| [3728] | 640 | cp->setPacket(osg::Matrix::identity(),viewer.getFrameStamp()); |
|---|
| [11624] | 641 | cp->setMasterKilled(true); |
|---|
| [1704] | 642 | |
|---|
| [11624] | 643 | scratchPad.reset(); |
|---|
| 644 | scratchPad.write(*cp); |
|---|
| 645 | |
|---|
| 646 | bc.setBuffer(scratchPad._startPtr, scratchPad._numBytes); |
|---|
| [1960] | 647 | bc.sync(); |
|---|
| [1704] | 648 | |
|---|
| [4472] | 649 | std::cout << "Broadcasting death."<<std::endl; |
|---|
| [1704] | 650 | |
|---|
| 651 | } |
|---|
| 652 | |
|---|
| [1697] | 653 | return 0; |
|---|
| 654 | } |
|---|