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