source: proiecte/SIMEO/Simeo/src/SimeoEngine/RemoteGui.cpp @ 167

Last change on this file since 167 was 167, checked in by (none), 14 years ago

Simeo: added final project and also older proof of concept code.

We used Git for version control, so look at the Git repo
in SIMEO/Simeo/ for more info.

File size: 9.0 KB
Line 
1#include "RemoteGui.h"
2#include <BulletCollision/CollisionShapes/btShapeHull.h>
3
4RemoteGui::RemoteGui(Environment *env)
5: Gui(env), m_addCounter(0)
6{
7        m_dynamicsWorld = env->getDynamicsWorld();
8}
9
10RemoteGui::~RemoteGui()
11{
12}
13
14void RemoteGui::init(int port)
15{
16        printf("Initializing remote gui...\n");
17
18        struct sockaddr_in serv_addr;
19
20        sockfd = socket(AF_INET, SOCK_STREAM, 0);
21
22        memset((char *) &serv_addr, 0, sizeof(serv_addr));
23        serv_addr.sin_family = AF_INET;
24        serv_addr.sin_addr.s_addr = INADDR_ANY; // foloseste adresa IP a masinii
25        serv_addr.sin_port = htons(port);
26
27        if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0) {
28                        printf("Error binding!\n");
29                        exit(0);
30         }
31}
32
33int RemoteGui::equal(guiUpdateData *e1, guiUpdateData *e2)
34{
35        for (int i = 0; i < 16; ++ i) {
36                if (e1->m[i] != e2->m[i])
37                        return 0;
38        }
39
40        if (e1->aabbMin != e2->aabbMin)
41                return 0;
42
43        if (e1->aabbMax != e2->aabbMax)
44                return 0;
45
46        for (int i = 0; i < 3; ++ i) {
47                for (int j = 0; j < 3; ++ j) {
48                        if (e1->rot[i][j] != e2->rot[i][j])
49                                return 0;
50                }
51        }
52
53//      if (e1->rot != e2->rot)
54//              return 0;
55
56        return 1;
57}
58
59//int RemoteGui::getGuiData(guiData **gd_out)
60int RemoteGui::getGuiData(map<void *, guiAddData *> &addObjects, map<void *, guiUpdateData *> &updateObjects, vector<int> &removeObjects)
61{
62        int numObjects = m_dynamicsWorld->getNumCollisionObjects();
63
64//      guiData *gd = new guiData[numObjects];
65
66        // temp map - created to test for removed objects
67        map<void *, guiUpdateData *> temp;
68
69//      getchar();
70        for (int i = 0; i < numObjects; ++ i)
71        {
72                guiUpdateData *elem = new guiUpdateData;
73
74                btCollisionObject *colObj = m_dynamicsWorld->getCollisionObjectArray()[i];
75                btRigidBody *body = btRigidBody::upcast(colObj);
76                if(body && body->getMotionState())
77                {
78                        btDefaultMotionState* myMotionState = (btDefaultMotionState*)body->getMotionState();
79                        myMotionState->m_graphicsWorldTrans.getOpenGLMatrix(elem->m);
80                        elem->rot = myMotionState->m_graphicsWorldTrans.getBasis();
81                }
82                else
83                {
84                        colObj->getWorldTransform().getOpenGLMatrix(elem->m);
85                        elem->rot = colObj->getWorldTransform().getBasis();
86                }
87
88                m_dynamicsWorld->getBroadphase()->getBroadphaseAabb(elem->aabbMin, elem->aabbMax);
89
90                map<void *, guiUpdateData *>::iterator it = m_cache.find((void *) colObj);
91
92                // check for new or updated objects
93                if (it == m_cache.end()) {
94                        // add new object
95                        guiAddData *addElem = new guiAddData;
96
97                        addElem->id = m_addCounter;
98                        elem->id = m_addCounter ++;
99
100//                      printf("[%d %d]\n", addElem->id, elem->id);
101
102                        btShapeHull *hull = new btShapeHull((btConvexShape *) colObj->getCollisionShape());
103                        hull->buildHull(colObj->getCollisionShape()->getMargin());
104                        addElem->numIndices = hull->numIndices();
105                        addElem->numVertices = hull->numVertices();
106                        addElem->indexPointer = (int *) hull->getIndexPointer();
107                        addElem->vertexPointer = (btVector3 *) hull->getVertexPointer();
108
109//                      printf("[%d %d %d %d %f]\n", addElem->id, addElem->numIndices, addElem->indexPointer[0], addElem->numIndices, addElem->vertexPointer[0]);
110//                      printf("[%d %d %d]\n", addElem->id, addElem->numIndices, addElem->numIndices);
111                        printf("[%d %d %d]\n", addElem->id, hull->numIndices(), hull->numVertices());
112
113                        addObjects[colObj] = addElem;
114
115                        m_cache[colObj] = elem;
116
117                        updateObjects[colObj] = elem;
118//                      printf("%p\n", m_dynamicsWorld->getCollisionObjectArray()[i]);
119                } else {
120                        // update existing object
121                        if (!equal(it->second, elem)) {
122                                updateObjects[colObj] = elem;
123                                delete m_cache[colObj];
124                                m_cache[colObj] = elem;
125                        }
126                }
127
128                temp[colObj] = elem;
129        }
130
131        // check for removed objects
132        map<void *, guiUpdateData *>::iterator it = m_cache.begin();
133        while (it != m_cache.end()) {
134                if (temp.find(it->first) == temp.end()) {
135                        removeObjects.push_back((*it).second->id);
136                }
137                ++ it;
138        }
139
140//      *gd_out = gd;
141
142        return numObjects;
143}
144
145void RemoteGui::sendData(int sock, const void *buffer, size_t size)
146{
147        size_t total;
148        int ret;
149
150        for (total = 0; total < size; total += ret) {
151                ret = send(sock, (char *) buffer + total, size - total, 0);
152//              printf("ret = %d\n", ret);
153                if (ret <= 0) {
154                        perror("send");
155                        exit(-1);
156                }
157        }
158}
159
160float* RemoteGui::packScalar(btScalar s, void *buffer)
161{
162        float *buff = (float *) buffer;
163
164        *buff ++ = s;
165
166        return buff;
167}
168
169float* RemoteGui::packVector3(btVector3 v, void *buffer)
170{
171        float *buff = (float *) buffer;
172
173        buff = packScalar(v.getX(), buff);
174        buff = packScalar(v.getY(), buff);
175        buff = packScalar(v.getZ(), buff);
176
177        return buff;
178}
179
180float* RemoteGui::packMatrix3x3(btMatrix3x3 m, void *buffer)
181{
182        float *buff = (float *) buffer;
183
184        for (int i = 0; i < 3; ++ i) {
185                buff = packVector3(m[i], buff);
186        }
187
188        return buff;
189}
190
191int * RemoteGui::packInt(int i, void *buffer)
192{
193        int *buff = (int *) buffer;
194
195        *buff ++ = i;
196
197        return buff;
198}
199
200void* RemoteGui::packGuiData(guiUpdateData *gd, void *buff)
201{
202        // add id
203        buff = packInt(gd->id, buff);
204
205        // add btScalar vector
206        char *p = (char *) buff;
207        for (int i = 0; i < 16; ++ i) {
208                buff = packScalar(gd->m[i], buff);
209        }
210        for (int i = 0; i < 16; i++) {
211                printf("ZZZZZZZZZZ: i=%d: %f\n", i, *(float*)(p+sizeof(float)*i));
212        }
213
214        // add rot
215        buff = packMatrix3x3(gd->rot, buff);
216
217        // add aabbMin
218        buff = packVector3(gd->aabbMin, buff);
219
220        // add aabbMax
221        buff = packVector3(gd->aabbMax, buff);
222
223        return buff;
224}
225
226int RemoteGui::packAddData(map<void *, guiAddData *> addObjects, void **buffer)
227{
228//      printf("Packing add data!\n");
229        void *temp, *buff;
230        int i;
231
232        map<void *, guiAddData *>::iterator it = addObjects.begin();
233
234        int size = 0;
235
236        for ( ; it != addObjects.end(); it ++) {
237                guiAddData *obj = (*it).second;
238
239                size += sizeof(int);
240                size += sizeof(int);
241                size += sizeof(int) * obj->numIndices;
242                size += sizeof(int);
243                size += sizeof(float) * 3 * obj->numVertices;
244        }
245
246        buff = new char [size];
247        temp = buff;
248
249        it = addObjects.begin();
250        for ( ; it != addObjects.end(); it ++) {
251                guiAddData *obj = (*it).second;
252
253                // add id
254                temp = packInt(obj->id, temp);
255//              printf("packing: obj->id = %d\n", obj->id);
256
257                // add indexPointer
258                temp = packInt(obj->numIndices, temp);
259                for (i = 0; i < obj->numIndices; ++ i) {
260                        temp = packInt(obj->indexPointer[i], temp);
261                }
262
263                // add vertexPointer
264                temp = packInt(obj->numVertices, temp);
265                for (i = 0; i < obj->numVertices; ++ i) {
266                        temp = packVector3(obj->vertexPointer[i], temp);
267                }
268        }
269
270        *buffer = buff;
271
272        return size;
273}
274
275int RemoteGui::packUpdateData(map<void *, guiUpdateData *> updateObjects, void **buffer)
276{
277        void *temp, *buff;
278
279        map<void *, guiUpdateData *>::iterator it = updateObjects.begin();
280
281        int size = 0;
282
283        for ( ; it != updateObjects.end(); it ++) {
284                size += sizeof(int) + sizeof(float) * (16 + 9 + 3 + 3);
285        }
286
287        buff = new char [size];
288        temp = buff;
289
290        it = updateObjects.begin();
291        for ( ; it != updateObjects.end(); it ++) {
292                guiUpdateData *obj = (*it).second;
293                temp = packGuiData(obj, temp);
294        }
295
296        *buffer = buff;
297
298        return size;
299}
300
301int RemoteGui::packRemoveData(vector<int> removeObjects, void **buffer)
302{
303        void *temp, *buff;
304
305        vector<int>::iterator it = removeObjects.begin();
306
307        int size = sizeof(int) * removeObjects.size();
308
309        buff = new int[size];
310        temp = buff;
311
312        for ( ; it != removeObjects.end(); it ++) {
313                temp = packInt((*it), temp);
314        }
315
316        *buffer = buff;
317
318        return size;
319}
320
321void RemoteGui::run()
322{
323        printf("Running remote gui...\n");
324
325        int n;
326//      char buffer[256];
327        map<void *, guiAddData *> addObjects;
328        map<void *, guiUpdateData *> updateObjects;
329        vector<int> removeObjects;
330
331        struct sockaddr_in cli_addr;
332
333        listen(sockfd, MAX_CLIENTS);
334
335     int clilen = sizeof(struct sockaddr_in);
336     int newsockfd = accept(sockfd,
337                 (struct sockaddr *) &cli_addr,
338                 (socklen_t *) &clilen);
339
340        if (newsockfd < 0) {
341                printf("Error accepting connection!\n");
342                exit(0);
343        }
344
345        n = send(newsockfd, SERVER_BANNER, strlen(SERVER_BANNER), 0);
346        if (n < 0) {
347                printf("Error writing to socket\n");
348                exit(0);
349        }
350
351     while (1) {
352                char q;
353
354                n = recv(newsockfd, (void*) &q, 1, 0);
355        if (n < 0) {
356                        printf("Error reading from socket\n");
357                        exit(0);
358                }
359
360        if (n == 0) {
361                        printf("Client left \n");
362            break;
363                }
364
365                addObjects.clear();
366                updateObjects.clear();
367                removeObjects.clear();
368                n = getGuiData(addObjects, updateObjects, removeObjects);
369
370                void *buff;
371
372                n = addObjects.size();
373                sendData(newsockfd, (const void *) &n, sizeof(int));
374                int sizeAddData = packAddData(addObjects, &buff);
375                sendData(newsockfd, (const void *) &sizeAddData, sizeof(int));
376                sendData(newsockfd, (const void *) buff, sizeAddData);
377                delete [] (char *) buff;
378
379                n = updateObjects.size();
380                sendData(newsockfd, (const void *) &n, sizeof(int));
381                int sizeUpdateData = packUpdateData(updateObjects, &buff);
382                sendData(newsockfd, (const void *) &sizeUpdateData, sizeof(int));
383                sendData(newsockfd, (const void *) buff, sizeUpdateData);
384                delete [] (char *) buff;
385
386                n = removeObjects.size();
387                sendData(newsockfd, (const void *) &n, sizeof(int));
388                int sizeRemoveData = packRemoveData(removeObjects, &buff);
389                sendData(newsockfd, (const void *) &sizeRemoveData, sizeof(int));
390                sendData(newsockfd, (const void *) buff, sizeRemoveData);
391                delete [] (int *) buff;
392
393                printf("Sent data!\n");
394
395                m_dynamicsWorld->stepSimulation(0.1);
396
397     }
398
399     close(sockfd);
400     close(newsockfd);
401}
Note: See TracBrowser for help on using the repository browser.