| 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 <osgProducer/Viewer> |
|---|
| 12 | |
|---|
| 13 | #include <osg/Quat> |
|---|
| 14 | |
|---|
| 15 | #if defined (WIN32) |
|---|
| 16 | #include <winsock.h> |
|---|
| 17 | #endif |
|---|
| 18 | |
|---|
| 19 | #include "receiver.h" |
|---|
| 20 | #include "broadcaster.h" |
|---|
| 21 | |
|---|
| 22 | typedef unsigned char * BytePtr; |
|---|
| 23 | template <class T> |
|---|
| 24 | inline void swapBytes( T &s ) |
|---|
| 25 | { |
|---|
| 26 | if( sizeof( T ) == 1 ) return; |
|---|
| 27 | |
|---|
| 28 | T d = s; |
|---|
| 29 | BytePtr sptr = (BytePtr)&s; |
|---|
| 30 | BytePtr dptr = &(((BytePtr)&d)[sizeof(T)-1]); |
|---|
| 31 | |
|---|
| 32 | for( unsigned int i = 0; i < sizeof(T); i++ ) |
|---|
| 33 | *(sptr++) = *(dptr--); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | class CameraPacket { |
|---|
| 37 | public: |
|---|
| 38 | |
|---|
| 39 | CameraPacket():_masterKilled(false) |
|---|
| 40 | { |
|---|
| 41 | _byte_order = 0x12345678; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | void setPacket(const osg::Matrix& matrix,const osg::FrameStamp* frameStamp) |
|---|
| 45 | { |
|---|
| 46 | _matrix = matrix; |
|---|
| 47 | if (frameStamp) |
|---|
| 48 | { |
|---|
| 49 | _frameStamp = *frameStamp; |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | void getModelView(osg::Matrix& matrix,float angle_offset=0.0f) |
|---|
| 54 | { |
|---|
| 55 | |
|---|
| 56 | matrix = _matrix * osg::Matrix::rotate(angle_offset,0.0f,1.0f,1.0f); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | void checkByteOrder( void ) |
|---|
| 60 | { |
|---|
| 61 | if( _byte_order == 0x78563412 ) |
|---|
| 62 | { |
|---|
| 63 | swapBytes( _byte_order ); |
|---|
| 64 | swapBytes( _masterKilled ); |
|---|
| 65 | for( int i = 0; i < 16; i++ ) |
|---|
| 66 | swapBytes( _matrix.ptr()[i] ); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | void setMasterKilled(const bool flag) { _masterKilled = flag; } |
|---|
| 74 | const bool getMasterKilled() const { return _masterKilled; } |
|---|
| 75 | |
|---|
| 76 | unsigned long _byte_order; |
|---|
| 77 | bool _masterKilled; |
|---|
| 78 | osg::Matrix _matrix; |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | osg::FrameStamp _frameStamp; |
|---|
| 85 | |
|---|
| 86 | }; |
|---|
| 87 | |
|---|
| 88 | enum ViewerMode |
|---|
| 89 | { |
|---|
| 90 | STAND_ALONE, |
|---|
| 91 | SLAVE, |
|---|
| 92 | MASTER |
|---|
| 93 | }; |
|---|
| 94 | |
|---|
| 95 | int main( int argc, char **argv ) |
|---|
| 96 | { |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ..."); |
|---|
| 103 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 104 | arguments.getApplicationUsage()->addCommandLineOption("-m","Set viewer to MASTER mode, sending view via packets."); |
|---|
| 105 | arguments.getApplicationUsage()->addCommandLineOption("-s","Set viewer to SLAVE mode, reciving view via packets."); |
|---|
| 106 | arguments.getApplicationUsage()->addCommandLineOption("-n <int>","Socket number to transmit packets"); |
|---|
| 107 | arguments.getApplicationUsage()->addCommandLineOption("-f <float>","Field of view of camera"); |
|---|
| 108 | arguments.getApplicationUsage()->addCommandLineOption("-o <float>","Offset angle of camera"); |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | osgProducer::Viewer viewer(arguments); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | ViewerMode viewerMode = STAND_ALONE; |
|---|
| 122 | while (arguments.read("-m")) viewerMode = MASTER; |
|---|
| 123 | while (arguments.read("-s")) viewerMode = SLAVE; |
|---|
| 124 | |
|---|
| 125 | float socketNumber=8100.0f; |
|---|
| 126 | while (arguments.read("-n",socketNumber)) ; |
|---|
| 127 | |
|---|
| 128 | float camera_fov=45.0f; |
|---|
| 129 | while (arguments.read("-f",camera_fov)) ; |
|---|
| 130 | |
|---|
| 131 | float camera_offset=45.0f; |
|---|
| 132 | while (arguments.read("-o",camera_offset)) ; |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 137 | { |
|---|
| 138 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 139 | return 1; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | if (arguments.errors()) |
|---|
| 147 | { |
|---|
| 148 | arguments.writeErrorMessages(std::cout); |
|---|
| 149 | return 1; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | osg::ref_ptr<osg::Node> rootnode = osgDB::readNodeFiles(arguments); |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | viewer.setSceneData(rootnode.get()); |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | viewer.realize(Producer::CameraGroup::ThreadPerCamera); |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | Broadcaster bc; |
|---|
| 165 | Receiver rc; |
|---|
| 166 | |
|---|
| 167 | while( !viewer.done() ) |
|---|
| 168 | { |
|---|
| 169 | |
|---|
| 170 | viewer.sync(); |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | viewer.update(); |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | switch (viewerMode) |
|---|
| 179 | { |
|---|
| 180 | case(MASTER): |
|---|
| 181 | { |
|---|
| 182 | CameraPacket cp; |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | osg::Matrix modelview(viewer.getCameraConfig()->getCamera(0)->getViewMatrix()); |
|---|
| 186 | |
|---|
| 187 | cp.setPacket(modelview,viewer.getFrameStamp()); |
|---|
| 188 | |
|---|
| 189 | bc.setBuffer(&cp, sizeof( CameraPacket )); |
|---|
| 190 | bc.sync(); |
|---|
| 191 | |
|---|
| 192 | } |
|---|
| 193 | break; |
|---|
| 194 | case(SLAVE): |
|---|
| 195 | { |
|---|
| 196 | CameraPacket cp; |
|---|
| 197 | |
|---|
| 198 | rc.setBuffer(&cp, sizeof( CameraPacket )); |
|---|
| 199 | rc.sync(); |
|---|
| 200 | |
|---|
| 201 | cp.checkByteOrder(); |
|---|
| 202 | |
|---|
| 203 | osg::Matrix modelview; |
|---|
| 204 | cp.getModelView(modelview,camera_offset); |
|---|
| 205 | |
|---|
| 206 | viewer.setView(modelview); |
|---|
| 207 | |
|---|
| 208 | if (cp.getMasterKilled()) |
|---|
| 209 | { |
|---|
| 210 | std::cout << "recieved master killed"<<std::endl; |
|---|
| 211 | |
|---|
| 212 | break; |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | break; |
|---|
| 216 | default: |
|---|
| 217 | |
|---|
| 218 | break; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | viewer.frame(); |
|---|
| 223 | |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | viewer.sync(); |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | if (viewerMode==MASTER) |
|---|
| 231 | { |
|---|
| 232 | |
|---|
| 233 | CameraPacket cp; |
|---|
| 234 | cp.setPacket(osg::Matrix::identity(),viewer.getFrameStamp()); |
|---|
| 235 | cp.setMasterKilled(true); |
|---|
| 236 | |
|---|
| 237 | bc.setBuffer(&cp, sizeof( CameraPacket )); |
|---|
| 238 | bc.sync(); |
|---|
| 239 | |
|---|
| 240 | std::cout << "broadcasting death"<<std::endl; |
|---|
| 241 | |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | return 0; |
|---|
| 245 | } |
|---|