root/OpenSceneGraph/trunk/applications/present3D/deprecated/Cluster.cpp @ 13890

Revision 13890, 12.8 kB (checked in by robert, 10 days ago)

Added shaders to support experimental shader based displacement mapping technique osgTerrain::ShaderTerrain?.

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- Present3D - Copyright (C) 1999-2006 Robert Osfield
2 *
3 * This software is open source and may be redistributed and/or modified under
4 * the terms of the GNU General Public License (GPL) version 2.0.
5 * The full license is in LICENSE.txt file included with this distribution,.
6 *
7 * This software is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * include LICENSE.txt for more details.
11*/
12
13#include "Cluster.h"
14
15#include <stdio.h>
16#include <fcntl.h>
17#include <sys/types.h>
18
19#if !defined (WIN32) || defined(__CYGWIN__)
20#include <sys/ioctl.h>
21#include <sys/uio.h>
22#include <sys/socket.h>
23#include <netinet/in.h>
24#include <netdb.h>
25#include <arpa/inet.h>
26#include <sys/time.h>
27#include <net/if.h>
28#include <netdb.h>
29#endif
30
31#include <string.h>
32
33#if defined(__linux)
34    #include <unistd.h>
35    #include <linux/sockios.h>
36#elif defined(__FreeBSD__) || defined(__DragonFly__)
37    #include <unistd.h>
38    #include <sys/sockio.h>
39#elif defined(__sgi)
40    #include <unistd.h>
41    #include <net/soioctl.h>
42#elif defined(__CYGWIN__)
43    #include <unistd.h>
44#elif defined(__sun)
45    #include <unistd.h>
46    #include <sys/sockio.h>
47#elif defined (__APPLE__)
48    #include <unistd.h>
49    #include <sys/sockio.h>
50#elif defined (WIN32)
51    #include <winsock.h>
52    #include <stdio.h>
53#elif defined (__hpux)
54    #include <unistd.h>
55#else
56    #error Teach me how to build on this system
57#endif
58
59#include <stdio.h>
60#include <fcntl.h>
61#include <sys/types.h>
62#if defined (WIN32) && !defined(__CYGWIN__)
63#include <winsock.h>
64#else
65#include <unistd.h>
66#include <sys/uio.h>
67#include <sys/socket.h>
68#include <netinet/in.h>
69#include <netdb.h>
70#include <arpa/inet.h>
71#include <sys/time.h>
72#endif
73#include <string.h>
74
75#include <osg/io_utils>
76#include <iostream>
77
78const unsigned int CameraPacket::MAX_NUM_EVENTS = 10;
79const unsigned int CameraPacket::SWAP_BYTES_COMPARE = 0x12345678;
80
81void DataConverter::write(const osg::FrameStamp& fs)
82{
83    osg::notify(osg::NOTICE)<<"writeFramestamp = "<<fs.getFrameNumber()<<" "<<fs.getReferenceTime()<<std::endl;
84
85    writeUInt(fs.getFrameNumber());
86    writeDouble(fs.getReferenceTime());
87}
88
89void DataConverter::read(osg::FrameStamp& fs)
90{
91    fs.setFrameNumber(readUInt());
92    fs.setReferenceTime(readDouble());
93
94    osg::notify(osg::NOTICE)<<"readFramestamp = "<<fs.getFrameNumber()<<" "<<fs.getReferenceTime()<<std::endl;
95}
96
97void DataConverter::write(const osg::Matrix& matrix)
98{
99    writeDouble(matrix(0,0));
100    writeDouble(matrix(0,1));
101    writeDouble(matrix(0,2));
102    writeDouble(matrix(0,3));
103
104    writeDouble(matrix(1,0));
105    writeDouble(matrix(1,1));
106    writeDouble(matrix(1,2));
107    writeDouble(matrix(1,3));
108
109    writeDouble(matrix(2,0));
110    writeDouble(matrix(2,1));
111    writeDouble(matrix(2,2));
112    writeDouble(matrix(2,3));
113
114    writeDouble(matrix(3,0));
115    writeDouble(matrix(3,1));
116    writeDouble(matrix(3,2));
117    writeDouble(matrix(3,3));
118
119    osg::notify(osg::NOTICE)<<"writeMatrix = "<<matrix<<std::endl;
120
121}
122
123void DataConverter::read(osg::Matrix& matrix)
124{
125    matrix(0,0) = readDouble();
126    matrix(0,1) = readDouble();
127    matrix(0,2) = readDouble();
128    matrix(0,3) = readDouble();
129
130    matrix(1,0) = readDouble();
131    matrix(1,1) = readDouble();
132    matrix(1,2) = readDouble();
133    matrix(1,3) = readDouble();
134
135    matrix(2,0) = readDouble();
136    matrix(2,1) = readDouble();
137    matrix(2,2) = readDouble();
138    matrix(2,3) = readDouble();
139
140    matrix(3,0) = readDouble();
141    matrix(3,1) = readDouble();
142    matrix(3,2) = readDouble();
143    matrix(3,3) = readDouble();
144
145    osg::notify(osg::NOTICE)<<"readMatrix = "<<matrix<<std::endl;
146
147}
148
149void DataConverter::write(const osgGA::GUIEventAdapter& event)
150{
151    writeUInt(event.getEventType());
152    writeUInt(event.getKey());
153    writeUInt(event.getButton());
154    writeInt(event.getWindowX());
155    writeInt(event.getWindowY());
156    writeUInt(event.getWindowWidth());
157    writeUInt(event.getWindowHeight());
158    writeFloat(event.getXmin());
159    writeFloat(event.getYmin());
160    writeFloat(event.getXmax());
161    writeFloat(event.getYmax());
162    writeFloat(event.getX());
163    writeFloat(event.getY());
164    writeUInt(event.getButtonMask());
165    writeUInt(event.getModKeyMask());
166    writeDouble(event.getTime());
167}
168
169void DataConverter::read(osgGA::GUIEventAdapter& event)
170{
171    event.setEventType((osgGA::GUIEventAdapter::EventType)readUInt());
172    event.setKey(readUInt());
173    event.setButton(readUInt());
174    int x = readInt();
175    int y = readInt();
176    int width = readUInt();
177    int height = readUInt();
178    event.setWindowRectangle(x,y,width,height);
179    float xmin = readFloat();
180    float ymin = readFloat();
181    float xmax = readFloat();
182    float ymax = readFloat();
183    event.setInputRange(xmin,ymin,xmax,ymax);
184    event.setX(readFloat());
185    event.setY(readFloat());
186    event.setButtonMask(readUInt());
187    event.setModKeyMask(readUInt());
188    event.setTime(readDouble());
189}
190
191void DataConverter::write(CameraPacket& cameraPacket)
192{
193    writeUInt(cameraPacket._byte_order);
194
195    writeUInt(cameraPacket._masterKilled);
196
197    write(cameraPacket._matrix);
198    write(cameraPacket._frameStamp);
199
200    writeUInt(cameraPacket._events.size());
201    for(osgGA::EventQueue::Events::iterator itr = cameraPacket._events.begin();
202        itr != cameraPacket._events.end();
203        ++itr)
204    {
205        osgGA::GUIEventAdapter* event = (*itr)->asGUIEventAdapter();
206        if (event) write(*(event));
207    }
208}
209
210void DataConverter::read(CameraPacket& cameraPacket)
211{
212    cameraPacket._byte_order = readUInt();
213    if (cameraPacket._byte_order != CameraPacket::SWAP_BYTES_COMPARE)
214    {
215        _swapBytes = !_swapBytes;
216    }
217
218    cameraPacket._masterKilled = readUInt()!=0;
219
220    read(cameraPacket._matrix);
221    read(cameraPacket._frameStamp);
222
223    cameraPacket._events.clear();
224    unsigned int numEvents = readUInt();
225    for(unsigned int i=0;i<numEvents;++i)
226    {
227        osgGA::GUIEventAdapter* event = new osgGA::GUIEventAdapter;
228        read(*(event));
229        cameraPacket._events.push_back(event);
230    }
231}
232
233void CameraPacket::readEventQueue(osgViewer::Viewer& viewer)
234{
235    _events.clear();
236
237    viewer.getEventQueue()->copyEvents(_events);
238
239    osg::notify(osg::INFO)<<"written events = "<<_events.size()<<std::endl;
240}
241
242void CameraPacket::writeEventQueue(osgViewer::Viewer& viewer)
243{
244    osg::notify(osg::INFO)<<"received events = "<<_events.size()<<std::endl;
245
246    // copy the events to osgProducer style events.
247    viewer.getEventQueue()->appendEvents(_events);
248}
249
250
251//////////////////////////////////////////////////////////////////////////////
252//
253//  Reciever
254//
255Receiver::Receiver( void )
256{
257    _port = 0;
258    _initialized = false;
259    _buffer = 0L;
260}
261
262Receiver::~Receiver( void )
263{
264#if defined (WIN32) && !defined(__CYGWIN__)
265    closesocket( _so);
266#else
267    close( _so );
268#endif
269}
270
271bool Receiver::init( void )
272{
273#if defined(WIN32) && !defined(__CYGWIN__)
274    WORD version = MAKEWORD(1,1);
275    WSADATA wsaData;
276    // First, we start up Winsock
277    WSAStartup(version, &wsaData);
278#endif
279
280    if( _port == 0 )
281    {
282    fprintf( stderr, "Receiver::init() - port not defined\n" );
283    return false;
284    }
285
286    if( (_so = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
287    {
288        perror( "Socket" );
289    return false;
290    }
291#if defined (WIN32) && !defined(__CYGWIN__)
292//    const BOOL on = TRUE;
293//    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(int));
294#else
295    int on = 1;
296    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
297#endif
298
299//    struct sockaddr_in saddr;
300    saddr.sin_family = AF_INET;
301    saddr.sin_port   = htons( _port );
302#if defined (WIN32) && !defined(__CYGWIN__)
303    saddr.sin_addr.s_addr =  htonl(INADDR_ANY);
304#else
305    saddr.sin_addr.s_addr =  0;
306#endif
307
308    if( bind( _so, (struct sockaddr *)&saddr, sizeof( saddr )) < 0 )
309    {
310        perror( "bind" );
311        return false;
312    }
313
314    _initialized = true;
315    return _initialized;
316}
317
318
319void Receiver::setPort( const short port )
320{
321    _port = port;
322}
323
324void Receiver::setBuffer( void *buffer, const unsigned int size )
325{
326    _buffer = buffer;
327    _buffer_size = size;
328}
329
330void Receiver::sync( void )
331{
332    if(!_initialized) init();
333
334    if( _buffer == 0L )
335    {
336        fprintf( stderr, "Receiver::sync() - No buffer\n" );
337        return;
338    }
339
340#if defined(__linux) || defined(__FreeBSD__) || defined( __APPLE__ ) || \
341    defined(__DragonFly__)
342    socklen_t
343#else
344    int
345#endif
346        size = sizeof( struct sockaddr_in );
347
348    fd_set fdset;
349    FD_ZERO( &fdset );
350    FD_SET( _so, &fdset );
351
352    struct timeval tv;
353    tv.tv_sec = 0;
354    tv.tv_usec = 0;
355
356#if defined (WIN32) && !defined(__CYGWIN__)
357//    saddr.sin_port   = htons( _port );
358    recvfrom( _so, (char *)_buffer, _buffer_size, 0, (sockaddr*)&saddr, &size );
359//    recvfrom(sock_Receive, szMessage, 256, 0, (sockaddr*)&addr_Cli, &clilen)
360    //int err = WSAGetLastError ();
361    //int *dum = (int*) _buffer;
362
363    while( select( _so+1, &fdset, 0L, 0L, &tv ) )
364    {
365        if( FD_ISSET( _so, &fdset ) )
366        {
367            recvfrom( _so, (char *)_buffer, _buffer_size, 0, (sockaddr*)&saddr, &size );
368        }
369    }
370#else
371    recvfrom( _so, (caddr_t)_buffer, _buffer_size, 0, 0, &size );
372    while( select( _so+1, &fdset, 0L, 0L, &tv ) )
373    {
374        if( FD_ISSET( _so, &fdset ) )
375        {
376            recvfrom( _so, (caddr_t)_buffer, _buffer_size, 0, 0, &size );
377        }
378    }
379#endif
380}
381
382
383//////////////////////////////////////////////////////////////////////////////
384//
385//  Broadcaster
386//
387Broadcaster::Broadcaster( void )
388{
389    _port = 0;
390    _initialized = false;
391    _buffer = 0L;
392    _address = 0;
393}
394
395Broadcaster::~Broadcaster( void )
396{
397#if defined (WIN32) && !defined(__CYGWIN__)
398    closesocket( _so);
399#else
400    close( _so );
401#endif
402}
403
404bool Broadcaster::init( void )
405{
406#if defined (WIN32) && !defined(__CYGWIN__)
407    WORD version = MAKEWORD(1,1);
408    WSADATA wsaData;
409    // First, we start up Winsock
410    WSAStartup(version, &wsaData);
411#endif
412
413    if( _port == 0 )
414    {
415        fprintf( stderr, "Broadcaster::init() - port not defined\n" );
416        return false;
417    }
418
419    if( (_so = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
420    {
421        perror( "Socket" );
422        return false;
423    }
424#if defined (WIN32) && !defined(__CYGWIN__)
425    const BOOL on = TRUE;
426#else
427    int on = 1;
428#endif
429
430#if defined (WIN32) && !defined(__CYGWIN__)
431    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(int));
432#else
433    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
434#endif
435
436    saddr.sin_family = AF_INET;
437    saddr.sin_port   = htons( _port );
438    if( _address == 0 )
439    {
440#if defined (WIN32) && !defined(__CYGWIN__)
441        setsockopt( _so, SOL_SOCKET, SO_BROADCAST, (const char *) &on, sizeof(int));
442#else
443        setsockopt( _so, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
444#endif
445
446#if !defined (WIN32) || defined(__CYGWIN__)
447        struct ifreq ifr;
448#endif
449#if defined (__linux) || defined(__CYGWIN__)
450        strcpy( ifr.ifr_name, "eth0" );
451#elif defined(__sun)
452        strcpy( ifr.ifr_name, "hme0" );
453#elif !defined (WIN32)
454        strcpy( ifr.ifr_name, "ef0" );
455#endif
456#if defined (WIN32) // get the server address
457        saddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
458    }
459#else
460        if( (ioctl( _so, SIOCGIFBRDADDR, &ifr)) < 0 )
461        {
462            perror( "Broadcaster::init() Cannot get Broadcast Address" );
463            return false;
464        }
465            saddr.sin_addr.s_addr = (((sockaddr_in *)&ifr.ifr_broadaddr)->sin_addr.s_addr);
466        }
467        else
468        {
469            saddr.sin_addr.s_addr = _address;
470        }
471#endif
472#define _VERBOSE 1
473#ifdef _VERBOSE
474    unsigned char *ptr = (unsigned char *)&saddr.sin_addr.s_addr;
475    printf( "Broadcast address : %u.%u.%u.%u\n", ptr[0], ptr[1], ptr[2], ptr[3] );
476#endif
477
478    _initialized = true;
479    return _initialized;
480}
481
482void Broadcaster::setHost( const char *hostname )
483{
484    struct hostent *h;
485    if( (h = gethostbyname( hostname )) == 0L )
486    {
487        fprintf( stderr, "Broadcaster::setHost() - Cannot resolv an address for \"%s\".\n", hostname );
488        _address = 0;
489    }
490    else
491        _address = *(( unsigned long  *)h->h_addr);
492}
493
494void Broadcaster::setPort( const short port )
495{
496    _port = port;
497}
498
499void Broadcaster::setBuffer( void *buffer, const unsigned int size )
500{
501    _buffer = buffer;
502    _buffer_size = size;
503}
504
505void Broadcaster::sync( void )
506{
507    if(!_initialized) init();
508
509    if( _buffer == 0L )
510    {
511        fprintf( stderr, "Broadcaster::sync() - No buffer\n" );
512        return;
513    }
514
515#if defined (WIN32) && !defined(__CYGWIN__)
516    unsigned int size = sizeof( SOCKADDR_IN );
517    sendto( _so, (const char *)_buffer, _buffer_size, 0, (struct sockaddr *)&saddr, size );
518    // int err = WSAGetLastError ();
519    // int *dum = (int*) _buffer;
520#else
521    unsigned int size = sizeof( struct sockaddr_in );
522    sendto( _so, (const void *)_buffer, _buffer_size, 0, (struct sockaddr *)&saddr, size );
523#endif
524
525}
Note: See TracBrowser for help on using the browser.