| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 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 | |
|---|
| 29 | #include <osgGA/TrackballManipulator> |
|---|
| 30 | #include <osgGA/StateSetManipulator> |
|---|
| 31 | #include <osgViewer/Viewer> |
|---|
| 32 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 33 | |
|---|
| 34 | #include <osg/Quat> |
|---|
| 35 | #include <osg/io_utils> |
|---|
| 36 | |
|---|
| 37 | #include <iostream> |
|---|
| 38 | |
|---|
| 39 | #if defined (WIN32) && !defined(__CYGWIN__) |
|---|
| 40 | #include <winsock.h> |
|---|
| 41 | #endif |
|---|
| 42 | |
|---|
| 43 | #include "receiver.h" |
|---|
| 44 | #include "broadcaster.h" |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | const unsigned int MAX_NUM_EVENTS = 10; |
|---|
| 48 | const unsigned int SWAP_BYTES_COMPARE = 0x12345678; |
|---|
| 49 | class CameraPacket { |
|---|
| 50 | public: |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | CameraPacket():_masterKilled(false) |
|---|
| 54 | { |
|---|
| 55 | _byte_order = SWAP_BYTES_COMPARE; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | void setPacket(const osg::Matrix& matrix,const osg::FrameStamp* frameStamp) |
|---|
| 59 | { |
|---|
| 60 | _matrix = matrix; |
|---|
| 61 | if (frameStamp) |
|---|
| 62 | { |
|---|
| 63 | _frameStamp = *frameStamp; |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void getModelView(osg::Matrix& matrix,float angle_offset=0.0f) |
|---|
| 68 | { |
|---|
| 69 | |
|---|
| 70 | matrix = _matrix * osg::Matrix::rotate(osg::DegreesToRadians(angle_offset),0.0f,1.0f,0.0f); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | void readEventQueue(osgViewer::Viewer& viewer); |
|---|
| 74 | |
|---|
| 75 | void writeEventQueue(osgViewer::Viewer& viewer); |
|---|
| 76 | |
|---|
| 77 | void setMasterKilled(const bool flag) { _masterKilled = flag; } |
|---|
| 78 | const bool getMasterKilled() const { return _masterKilled; } |
|---|
| 79 | |
|---|
| 80 | unsigned int _byte_order; |
|---|
| 81 | bool _masterKilled; |
|---|
| 82 | osg::Matrix _matrix; |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | osg::FrameStamp _frameStamp; |
|---|
| 89 | |
|---|
| 90 | osgGA::EventQueue::Events _events; |
|---|
| 91 | |
|---|
| 92 | }; |
|---|
| 93 | |
|---|
| 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; |
|---|
| 115 | |
|---|
| 116 | void reset() |
|---|
| 117 | { |
|---|
| 118 | _currentPtr = _startPtr; |
|---|
| 119 | } |
|---|
| 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 | { |
|---|
| 255 | osg::notify(osg::NOTICE)<<"writeFramestamp = "<<fs.getFrameNumber()<<" "<<fs.getReferenceTime()<<std::endl; |
|---|
| 256 | |
|---|
| 257 | writeUInt(fs.getFrameNumber()); |
|---|
| 258 | writeDouble(fs.getReferenceTime()); |
|---|
| 259 | writeDouble(fs.getSimulationTime()); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | void read(osg::FrameStamp& fs) |
|---|
| 263 | { |
|---|
| 264 | fs.setFrameNumber(readUInt()); |
|---|
| 265 | fs.setReferenceTime(readDouble()); |
|---|
| 266 | fs.setSimulationTime(readDouble()); |
|---|
| 267 | |
|---|
| 268 | osg::notify(osg::NOTICE)<<"readFramestamp = "<<fs.getFrameNumber()<<" "<<fs.getReferenceTime()<<std::endl; |
|---|
| 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)); |
|---|
| 292 | |
|---|
| 293 | osg::notify(osg::NOTICE)<<"writeMatrix = "<<matrix<<std::endl; |
|---|
| 294 | |
|---|
| 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(); |
|---|
| 318 | |
|---|
| 319 | osg::notify(osg::NOTICE)<<"readMatrix = "<<matrix<<std::endl; |
|---|
| 320 | |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | void write(const osgGA::GUIEventAdapter& event) |
|---|
| 324 | { |
|---|
| 325 | writeUInt(event.getEventType()); |
|---|
| 326 | writeUInt(event.getKey()); |
|---|
| 327 | writeUInt(event.getButton()); |
|---|
| 328 | writeInt(event.getWindowX()); |
|---|
| 329 | writeInt(event.getWindowY()); |
|---|
| 330 | writeUInt(event.getWindowWidth()); |
|---|
| 331 | writeUInt(event.getWindowHeight()); |
|---|
| 332 | writeFloat(event.getXmin()); |
|---|
| 333 | writeFloat(event.getYmin()); |
|---|
| 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()); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | void read(osgGA::GUIEventAdapter& event) |
|---|
| 344 | { |
|---|
| 345 | event.setEventType((osgGA::GUIEventAdapter::EventType)readUInt()); |
|---|
| 346 | event.setKey(readUInt()); |
|---|
| 347 | event.setButton(readUInt()); |
|---|
| 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); |
|---|
| 358 | event.setX(readFloat()); |
|---|
| 359 | event.setY(readFloat()); |
|---|
| 360 | event.setButtonMask(readUInt()); |
|---|
| 361 | event.setModKeyMask(readUInt()); |
|---|
| 362 | event.setTime(readDouble()); |
|---|
| 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()); |
|---|
| 375 | for(osgGA::EventQueue::Events::iterator itr = cameraPacket._events.begin(); |
|---|
| 376 | itr != cameraPacket._events.end(); |
|---|
| 377 | ++itr) |
|---|
| 378 | { |
|---|
| 379 | write(*(*itr)); |
|---|
| 380 | } |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | void read(CameraPacket& cameraPacket) |
|---|
| 384 | { |
|---|
| 385 | cameraPacket._byte_order = readUInt(); |
|---|
| 386 | if (cameraPacket._byte_order != SWAP_BYTES_COMPARE) |
|---|
| 387 | { |
|---|
| 388 | _swapBytes = !_swapBytes; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | cameraPacket._masterKilled = readUInt()!=0; |
|---|
| 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 | { |
|---|
| 400 | osgGA::GUIEventAdapter* event = new osgGA::GUIEventAdapter; |
|---|
| 401 | read(*(event)); |
|---|
| 402 | cameraPacket._events.push_back(event); |
|---|
| 403 | } |
|---|
| 404 | } |
|---|
| 405 | }; |
|---|
| 406 | |
|---|
| 407 | void CameraPacket::readEventQueue(osgViewer::Viewer& viewer) |
|---|
| 408 | { |
|---|
| 409 | _events.clear(); |
|---|
| 410 | |
|---|
| 411 | viewer.getEventQueue()->copyEvents(_events); |
|---|
| 412 | |
|---|
| 413 | osg::notify(osg::INFO)<<"written events = "<<_events.size()<<std::endl; |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | void CameraPacket::writeEventQueue(osgViewer::Viewer& viewer) |
|---|
| 417 | { |
|---|
| 418 | osg::notify(osg::INFO)<<"received events = "<<_events.size()<<std::endl; |
|---|
| 419 | |
|---|
| 420 | viewer.getEventQueue()->appendEvents(_events); |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | enum ViewerMode |
|---|
| 426 | { |
|---|
| 427 | STAND_ALONE, |
|---|
| 428 | SLAVE, |
|---|
| 429 | MASTER |
|---|
| 430 | }; |
|---|
| 431 | |
|---|
| 432 | int main( int argc, char **argv ) |
|---|
| 433 | { |
|---|
| 434 | |
|---|
| 435 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to approach implementation of clustering."); |
|---|
| 439 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 440 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 441 | arguments.getApplicationUsage()->addCommandLineOption("-m","Set viewer to MASTER mode, sending view via packets."); |
|---|
| 442 | arguments.getApplicationUsage()->addCommandLineOption("-s","Set viewer to SLAVE mode, receiving view via packets."); |
|---|
| 443 | arguments.getApplicationUsage()->addCommandLineOption("-n <int>","Socket number to transmit packets"); |
|---|
| 444 | arguments.getApplicationUsage()->addCommandLineOption("-f <float>","Field of view of camera"); |
|---|
| 445 | arguments.getApplicationUsage()->addCommandLineOption("-o <float>","Offset angle of camera"); |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | osgViewer::Viewer viewer; |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | ViewerMode viewerMode = STAND_ALONE; |
|---|
| 453 | while (arguments.read("-m")) viewerMode = MASTER; |
|---|
| 454 | while (arguments.read("-s")) viewerMode = SLAVE; |
|---|
| 455 | |
|---|
| 456 | int socketNumber=8100; |
|---|
| 457 | while (arguments.read("-n",socketNumber)) ; |
|---|
| 458 | |
|---|
| 459 | float camera_fov=-1.0f; |
|---|
| 460 | while (arguments.read("-f",camera_fov)) |
|---|
| 461 | { |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | float camera_offset=45.0f; |
|---|
| 465 | while (arguments.read("-o",camera_offset)) ; |
|---|
| 466 | |
|---|
| 467 | |
|---|
| 468 | |
|---|
| 469 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 470 | { |
|---|
| 471 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 472 | return 1; |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | |
|---|
| 476 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 477 | |
|---|
| 478 | |
|---|
| 479 | if (arguments.errors()) |
|---|
| 480 | { |
|---|
| 481 | arguments.writeErrorMessages(std::cout); |
|---|
| 482 | return 1; |
|---|
| 483 | } |
|---|
| 484 | |
|---|
| 485 | if (arguments.argc()<=1) |
|---|
| 486 | { |
|---|
| 487 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 488 | return 1; |
|---|
| 489 | } |
|---|
| 490 | |
|---|
| 491 | |
|---|
| 492 | osg::ref_ptr<osg::Node> rootnode = osgDB::readNodeFiles(arguments); |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | viewer.setSceneData(rootnode.get()); |
|---|
| 496 | |
|---|
| 497 | if (camera_fov>0.0f) |
|---|
| 498 | { |
|---|
| 499 | double fovy, aspectRatio, zNear, zFar; |
|---|
| 500 | viewer.getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio,zNear, zFar); |
|---|
| 501 | |
|---|
| 502 | double original_fov = atan(tan(osg::DegreesToRadians(fovy)*0.5)*aspectRatio)*2.0; |
|---|
| 503 | std::cout << "setting lens perspective : original "<<original_fov<<" "<<fovy<<std::endl; |
|---|
| 504 | |
|---|
| 505 | fovy = atan(tan(osg::DegreesToRadians(camera_fov)*0.5)/aspectRatio)*2.0; |
|---|
| 506 | viewer.getCamera()->setProjectionMatrixAsPerspective(fovy, aspectRatio,zNear, zFar); |
|---|
| 507 | |
|---|
| 508 | viewer.getCamera()->getProjectionMatrixAsPerspective(fovy, aspectRatio,zNear, zFar); |
|---|
| 509 | original_fov = atan(tan(osg::DegreesToRadians(fovy)*0.5)*aspectRatio)*2.0; |
|---|
| 510 | std::cout << "setting lens perspective : new "<<original_fov<<" "<<fovy<<std::endl; |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | viewer.setCameraManipulator(new osgGA::TrackballManipulator()); |
|---|
| 514 | |
|---|
| 515 | |
|---|
| 516 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | viewer.realize(); |
|---|
| 524 | |
|---|
| 525 | |
|---|
| 526 | CameraPacket *cp = new CameraPacket; |
|---|
| 527 | |
|---|
| 528 | |
|---|
| 529 | Broadcaster bc; |
|---|
| 530 | Receiver rc; |
|---|
| 531 | |
|---|
| 532 | bc.setPort(static_cast<short int>(socketNumber)); |
|---|
| 533 | rc.setPort(static_cast<short int>(socketNumber)); |
|---|
| 534 | |
|---|
| 535 | bool masterKilled = false; |
|---|
| 536 | |
|---|
| 537 | DataConverter scratchPad(1024); |
|---|
| 538 | |
|---|
| 539 | while( !viewer.done() && !masterKilled ) |
|---|
| 540 | { |
|---|
| 541 | osg::Timer_t startTick = osg::Timer::instance()->tick(); |
|---|
| 542 | |
|---|
| 543 | viewer.advance(); |
|---|
| 544 | |
|---|
| 545 | |
|---|
| 546 | switch (viewerMode) |
|---|
| 547 | { |
|---|
| 548 | case(MASTER): |
|---|
| 549 | { |
|---|
| 550 | |
|---|
| 551 | |
|---|
| 552 | osg::Matrix modelview(viewer.getCamera()->getViewMatrix()); |
|---|
| 553 | |
|---|
| 554 | cp->setPacket(modelview,viewer.getFrameStamp()); |
|---|
| 555 | |
|---|
| 556 | cp->readEventQueue(viewer); |
|---|
| 557 | |
|---|
| 558 | scratchPad.reset(); |
|---|
| 559 | scratchPad.write(*cp); |
|---|
| 560 | |
|---|
| 561 | scratchPad.reset(); |
|---|
| 562 | scratchPad.read(*cp); |
|---|
| 563 | |
|---|
| 564 | bc.setBuffer(scratchPad._startPtr, scratchPad._numBytes); |
|---|
| 565 | |
|---|
| 566 | std::cout << "bc.sync()"<<scratchPad._numBytes<<std::endl; |
|---|
| 567 | |
|---|
| 568 | bc.sync(); |
|---|
| 569 | |
|---|
| 570 | } |
|---|
| 571 | break; |
|---|
| 572 | case(SLAVE): |
|---|
| 573 | { |
|---|
| 574 | |
|---|
| 575 | rc.setBuffer(scratchPad._startPtr, scratchPad._numBytes); |
|---|
| 576 | |
|---|
| 577 | rc.sync(); |
|---|
| 578 | |
|---|
| 579 | scratchPad.reset(); |
|---|
| 580 | scratchPad.read(*cp); |
|---|
| 581 | |
|---|
| 582 | cp->writeEventQueue(viewer); |
|---|
| 583 | |
|---|
| 584 | if (cp->getMasterKilled()) |
|---|
| 585 | { |
|---|
| 586 | std::cout << "Received master killed."<<std::endl; |
|---|
| 587 | |
|---|
| 588 | masterKilled = true; |
|---|
| 589 | } |
|---|
| 590 | } |
|---|
| 591 | break; |
|---|
| 592 | default: |
|---|
| 593 | |
|---|
| 594 | break; |
|---|
| 595 | } |
|---|
| 596 | |
|---|
| 597 | osg::Timer_t endTick = osg::Timer::instance()->tick(); |
|---|
| 598 | |
|---|
| 599 | osg::notify(osg::INFO)<<"Time to do cluster sync "<<osg::Timer::instance()->delta_m(startTick,endTick)<<std::endl; |
|---|
| 600 | |
|---|
| 601 | |
|---|
| 602 | |
|---|
| 603 | viewer.eventTraversal(); |
|---|
| 604 | viewer.updateTraversal(); |
|---|
| 605 | |
|---|
| 606 | if (viewerMode==SLAVE) |
|---|
| 607 | { |
|---|
| 608 | osg::Matrix modelview; |
|---|
| 609 | cp->getModelView(modelview,camera_offset); |
|---|
| 610 | |
|---|
| 611 | viewer.getCamera()->setViewMatrix(modelview); |
|---|
| 612 | } |
|---|
| 613 | |
|---|
| 614 | |
|---|
| 615 | if(!masterKilled) |
|---|
| 616 | viewer.renderingTraversals(); |
|---|
| 617 | |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | if (viewerMode==MASTER) |
|---|
| 622 | { |
|---|
| 623 | |
|---|
| 624 | cp->setPacket(osg::Matrix::identity(),viewer.getFrameStamp()); |
|---|
| 625 | cp->setMasterKilled(true); |
|---|
| 626 | |
|---|
| 627 | bc.setBuffer(cp, sizeof( CameraPacket )); |
|---|
| 628 | bc.sync(); |
|---|
| 629 | |
|---|
| 630 | std::cout << "Broadcasting death."<<std::endl; |
|---|
| 631 | |
|---|
| 632 | } |
|---|
| 633 | |
|---|
| 634 | return 0; |
|---|
| 635 | } |
|---|