00001 /********************************************************************
00002 Description: The Visualization Thread
00003 part of the 3Dsia project
00004 created: xandi, 130400
00005
00006 History:
00007 date, name, changes, in funtion
00008 080500 StonedBones added mouse support
00009 070500 xandi added doxygen comments
00010 050500 beef&xandi added light, many bugfixes
00011 280400 xandi added support for commandThread
00012 240400 xandi added objectlists
00013 230400 StonedBones added devices, chatfunctionality
00014 220400 xandi added movement-capability
00015 180400 StonedBones added dynamic packet length
00016 160400 xandi added object parser
00017 ********************************************************************/
00018
00019 #include <GL/glut.h>
00020 #include <stdio.h>
00021 #include <map>
00022 #include <list>
00023 #include <sys/time.h>
00024
00025
00026 //JUST TEMPORARILY *********************
00027 //#include "prototypegraphics.h"
00028 #include "outputOpengl.h"
00029 //**************************************
00030
00031 #include "misc.h"
00032 #include "quartz.h"
00033 #include "devices.h"
00034
00035 #define DEBUG_3D
00036
00037 GLfloat m_ambient[] = { 0.5, 0.5, 0.5, 1.0};
00038 GLfloat m_diffuse[] = { 1.0, 1.0, 1.0, 1.0};
00039 GLfloat m_specular[] = { 0.0, 0.0, 0.0, 1.0};
00040 GLfloat m_shininess[] = { 50.0 };
00041 GLfloat m_emission[] = { 0.0, 0.0, 0.0, 1.0};
00042
00043 GLfloat m_lamp[] = { 1.0, 0.5, 0.0, 1.0 };
00044 GLfloat m_green[] = { 0.0, 1.0, 0.0, 1.0 };
00045
00046 GLfloat l_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
00047 GLfloat l_ambient[] = { 0.6, 0.6, 0.6, 1.0 };
00048 GLfloat l_specular[] = { 0.0, 0.0, 0.0, 1.0 };
00049
00050 GLfloat l_position[] = {10.0, 300.0, 10.0, 0.0};
00051
00052 Buffer* myInBuffer = 0;
00053
00054 QuartzClass* qObj = 0;
00055
00056 extern Buffer* coreBufferIn;
00057
00058 string chatmsg;
00059 string dismsg;
00060
00062
00066 struct vertex
00067 {
00068 float x;
00069 float y;
00070 float z;
00071 };
00072
00073
00075
00076 struct color
00077 {
00078 float R;
00079 float G;
00080 float B;
00081 float A;
00082 };
00083
00085
00088 struct poly
00089 {
00090 int number;
00091 char type;
00092 struct color col;
00093 list<int>* verteces;
00094 };
00095
00096
00098
00101 struct rawObject
00102 {
00103 string name;
00104
00105 int numberOfVertices;
00106 map<int,vertex> verteces;
00107
00108
00109 int numberOfPolies;
00110 map<int,poly> polies;
00111 };
00112
00114
00117 struct hullInfo
00118 {
00119 vertex a,
00120 b;
00121 };
00122
00123
00124 int myObjectID;
00125 bool showme = false;
00126
00127
00129
00132 struct Object
00133 {
00134 string name;
00135 double pos[3];
00136 // string visualData;
00137 bool havedata;
00138 GLuint visual;
00139 hullInfo* hull;
00140 string chat_msg;
00141 };
00142
00143
00144 map<int,Object> Objects;
00145
00146 int colObject;
00147
00148 struct timeval t1,t2;
00149 struct timeval devfreq;
00150 int frametime;
00151 int devasks;
00152
00153 Devices devices;
00154 int deviceX, deviceY;
00155
00156 GLuint Scene;
00157 GLuint environment;
00158 bool haveenvironment = false;
00159
00160 /*
00161
00162 parse the binary description and display it
00163 xandi, 190400
00164
00165 */
00166 #define ROTATESTEP 0.5
00167 GLfloat myRotate = 0.0;
00168
00169 char* TheOneObject = 0;
00170
00172
00180 hullInfo* parseObject ( string str )
00181 {
00182 #define SIZEOFNAME 64
00183
00184 #ifdef DEBUG
00185 fprintf(stderr,"[parser] 1\n");
00186 #endif
00187
00188 GLfloat m_diff[4] = { 1.0, 1.0, 1.0, 1.0 };
00189
00190 hullInfo* hull = new hullInfo;
00191
00192 hull->a.x = 0.0;
00193 hull->a.y = 0.0;
00194 hull->a.y = 0.0;
00195 hull->b.x = 0.0;
00196 hull->b.y = 0.0;
00197 hull->b.y = 0.0;
00198
00199 if ( str.empty () ) return hull;
00200
00201 struct rawObject* o = new rawObject;
00202
00203 int pos;
00204
00205 o->name.assign ( str.c_str(), SIZEOFNAME );
00206 o->name[63] = '\0';
00207 pos = 64;
00208
00209 #ifdef DEBUG
00210 fprintf(stderr,"Name: %s\n", o->name.c_str ());
00211 fprintf(stderr,"%s\n",str.c_str() + pos );
00212 #endif
00213
00214 memcpy ( &o->numberOfVertices, str.data() + pos, sizeof ( int ) );
00215 pos += sizeof ( int );
00216
00217 int tempNumber;
00218
00219 #ifdef DEBUG
00220 fprintf(stderr,"%d\n",o->numberOfVertices);
00221 #endif
00222 for ( int i = 0; i < o->numberOfVertices; i++ )
00223 {
00224 #ifdef DEBUG
00225 fprintf(stderr,"vertices\n");
00226 #endif
00227
00228 memcpy ( &tempNumber, str.data() + pos, sizeof(int) );
00229 pos += sizeof(int);
00230 memcpy ( &o->verteces[tempNumber], str.data() + pos, sizeof(struct vertex) );
00231 pos += sizeof(vertex);
00232
00233 if (hull->a.x > o->verteces[tempNumber].x )
00234 hull->a.x = o->verteces[tempNumber].x;
00235 if (hull->a.y > o->verteces[tempNumber].y )
00236 hull->a.y = o->verteces[tempNumber].y;
00237 if (hull->a.z > o->verteces[tempNumber].z )
00238 hull->a.z = o->verteces[tempNumber].z;
00239
00240 if (hull->b.x < o->verteces[tempNumber].x )
00241 hull->b.x = o->verteces[tempNumber].x;
00242 if (hull->b.y < o->verteces[tempNumber].y )
00243 hull->b.y = o->verteces[tempNumber].y;
00244 if (hull->b.z < o->verteces[tempNumber].z )
00245 hull->b.z = o->verteces[tempNumber].z;
00246 }
00247
00248 memcpy ( &o->numberOfPolies, str.data() + pos, sizeof(int) );
00249 pos += sizeof(int);
00250
00251 poly tempPoly;
00252
00253 for ( int i = 0; i < o->numberOfPolies; i++ )
00254 {
00255 #ifdef DEBUG
00256 fprintf(stderr,"polygons\n");
00257 #endif
00258
00259 memcpy ( &tempPoly.number, str.data() + pos, sizeof (int) );
00260 pos += sizeof (int);
00261 memcpy ( &tempPoly.type, str.data() + pos, sizeof (char) );
00262 pos += sizeof (char);
00263 memcpy ( &tempPoly.col, str.data() + pos, sizeof (color) );
00264 pos += sizeof (color);
00265
00266 memcpy ( &tempNumber, str.data() + pos, sizeof (int) );
00267 pos += sizeof (int);
00268
00269 int tempVertexNr;
00270
00271 m_diff[0] = tempPoly.col.R;
00272 m_diff[1] = tempPoly.col.G;
00273 m_diff[2] = tempPoly.col.B;
00274 m_diff[3] = tempPoly.col.A;
00275
00276 glBegin(GL_TRIANGLE_STRIP);
00277
00278 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, m_diff );
00279
00280 for ( int j = 0; j < tempNumber; j++ )
00281 {
00282 memcpy ( &tempVertexNr, str.data() + pos, sizeof (int) );
00283 pos += sizeof (int);
00284
00285 if ( !(j % 2) )
00286 {
00287 switch ( j % 6 )
00288 {
00289 case 0:
00290 glNormal3f(1.0,0.0,0.0);
00291 break;
00292 case 2:
00293 glNormal3f(0.0,1.0,0.0);
00294 break;
00295 case 4:
00296 glNormal3f(0.0,0.0,1.0);
00297 break;
00298 }
00299 }
00300
00301 glVertex3f (o->verteces[tempVertexNr].x,o->verteces[tempVertexNr].y,o->verteces[tempVertexNr].z);
00302 }
00303 glEnd();
00304
00305 o->polies[tempPoly.number] = tempPoly;
00306 }
00307
00308 #ifdef DEBUG3
00309 fprintf(stderr,"[%f][%f][%f]\n",o->verteces[1].x
00310 ,o->verteces[1].y
00311 ,o->verteces[1].z);
00312 #endif
00313
00314
00315 #ifdef DEBUG
00316 fprintf(stderr,"[parser] finished\n");
00317 #endif
00318
00319 #ifdef DEBUG5
00320 fprintf(stderr,"[parser] HULL = a %f %f %f\n",hull->a.x,hull->a.y,hull->a.z);
00321 fprintf(stderr," b %f %f %f\n",hull->b.x,hull->b.y,hull->b.z);
00322 #endif
00323
00324 delete o;
00325 return hull;
00326 }
00327
00328
00329
00331
00338 int collisionDetection ( vertex* position )
00339 {
00340 int cobject = -1;
00341
00342 #ifdef DEBUG5
00343 fprintf(stderr,"[vT.collisionDetection] checking [%f][%f][%f]\n",position->x,position->y,position->z);
00344 #endif
00345
00346 typedef map<int,Object>::const_iterator objectIt;
00347 for ( objectIt o = Objects.begin(); o != Objects.end(); ++o)
00348 {
00349 const hullInfo *hull = o->second.hull;
00350 const double *pos = o->second.pos;
00351
00352 #ifdef DEBUG5
00353 fprintf(stderr,"[collision] HULL = a %f %f %f\n",pos[X] + hull->a.x, pos[Y] + hull->a.y, pos[Z] + hull->a.z);
00354 fprintf(stderr," b %f %f %f\n",pos[X] + hull->b.x, pos[Y] + hull->b.y, pos[Z] + hull->b.z);
00355 #endif
00356 if ( o->first == myObjectID )
00357 continue;
00358
00359 if ( ( ( ( pos[X] + hull->a.x ) < position->x + 1 ) && ( ( pos[X] + hull->b.x ) > position->x -1 ) )
00360 && ( ( ( pos[Y] + hull->a.y ) < position->y + 1 ) && ( ( pos[Y] + hull->b.y ) > position->y -1 ) )
00361 && ( ( ( pos[Z] + hull->a.z ) < position->z + 1 ) && ( ( pos[Z] + hull->b.z ) > position->z -1 ) ) )
00362 {
00363 cobject = o->first;
00364 #ifdef DEBUG5
00365 fprintf(stderr,"[vT] Collision Detection POSITIVE!! %d\n",cobject);
00366 #endif
00367 break;
00368 }
00369
00370 }
00371
00372 return cobject;
00373 }
00374
00375
00376 void DrawCursor()
00377 {
00378 glTranslatef( (GLfloat) campos[X]+lookdir[X]*1.1,
00379 (GLfloat) campos[Y]+lookdir[Y]*1.1,
00380 (GLfloat) campos[Z]+lookdir[Z]*1.1);
00381
00382 DrawHand ();
00383 }
00384
00385
00387
00392 void display (void)
00393 {
00394 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00395
00396 glShadeModel( GL_SMOOTH);
00397
00398 glMaterialfv( GL_FRONT, GL_SPECULAR, m_specular );
00399 glMaterialfv( GL_FRONT, GL_SHININESS, m_shininess );
00400 glMaterialfv( GL_FRONT, GL_EMISSION, m_emission );
00401
00402 glLightfv ( GL_LIGHT0, GL_AMBIENT, l_ambient );
00403 glLightfv ( GL_LIGHT0, GL_DIFFUSE, l_diffuse );
00404 glLightfv ( GL_LIGHT0, GL_POSITION, l_position );
00405 glLightfv ( GL_LIGHT0, GL_SPECULAR, l_specular );
00406
00407 glPushMatrix ();
00408
00409 glDisable(GL_BLEND);
00410
00411 #ifdef DEBUG
00412 fprintf(stderr,"[vT] - %f %f %f\n",campos[X],campos[Y],campos[Z]);
00413 fprintf(stderr,"[vT] - %f %f %f\n",lookto[X],lookto[Y],lookto[Z]);
00414 fprintf(stderr,"[vT] - %f %f %f\n",upvect[X],upvect[Y],upvect[Z]);
00415 #endif
00416
00417 gluLookAt ( campos[X], campos[Y], campos[Z],
00418 lookto[X], lookto[Y], lookto[Z],
00419 upvect[X], upvect[Y], upvect[Z]);
00420
00421
00422 glScalef (1.0, 1.0, 1.0);
00423
00424 glCallList(Scene);
00425
00426 glMaterialfv( GL_FRONT, GL_DIFFUSE, m_green );
00427
00428 glBegin(GL_TRIANGLE_STRIP);
00429 glNormal3f(1.0,0.0,0.0);
00430 glVertex3f(-50.0,0.0,-50.0);
00431 glVertex3f(50.0,0.0,-50.0);
00432 glNormal3f(0.0,1.0,0.0);
00433 glVertex3f(0.0,50.0,0.0);
00434 glVertex3f(0.0,0.0,50.0);
00435 glNormal3f(0.0,0.0,1.0);
00436 glVertex3f(-50.0,0.0,-50.0);
00437 glEnd();
00438
00439 if ( !haveenvironment )
00440 glCallList ( environment );
00441
00442 typedef map<int,Object>::const_iterator objectIt;
00443
00444 for ( objectIt o = Objects.begin(); o != Objects.end(); ++o)
00445 {
00446 const double *pos = o->second.pos;
00447
00448 #ifdef DEBUG3
00449 fprintf(stderr,"[%d]-[%f][%f][%f]--[%d]\n",o->first,pos[0],pos[1],pos[2],o->second.visualData.length());
00450 #endif
00451
00452
00453 if ( !o->second.havedata ) continue;
00454
00455 if ( o->first == myObjectID )
00456 {
00457 if ( !showme )
00458 continue;
00459 }
00460
00461 glPushMatrix();
00462
00463 glTranslated(pos[0],pos[1],pos[2]);
00464 glCallList ( o->second.visual);
00465
00466 glRasterPos2i (0,0);
00467 printstring (GLUT_BITMAP_TIMES_ROMAN_24, const_cast<char*>(o->second.chat_msg.c_str()));
00468
00469 glPopMatrix ();
00470 }
00471
00472
00473 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, m_green );
00474
00475 // SHOWS LIGHTSOURCES:
00476 #ifdef DEBUG_3D
00477 glPushMatrix();
00478 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, m_lamp );
00479
00480 glTranslatef(l_position[0],l_position[1],l_position[2]);
00481 glRotatef(myRotate,1.0,1.1,1.4);
00482
00483 glutWireSphere ( 20.0, 10, 10 );
00484 glPopMatrix();
00485 #endif
00486
00487 DrawCursor();
00488
00489 glPopMatrix();
00490
00491 glShadeModel(GL_FLAT);
00492 glMatrixMode(GL_PROJECTION);
00493
00494 glPushMatrix();
00495
00496 glLoadIdentity();
00497 glOrtho(-0.5,799.5,-0.5,579.5,-1.0,1.0);
00498 glMatrixMode(GL_MODELVIEW);
00499 glLoadIdentity();
00500
00501
00502 DrawStat();
00503
00504 glPushMatrix ();
00505 glColor3f (1.0, 1.0, 1.0);
00506 glRasterPos2i (20,100);
00507 printstring (GLUT_BITMAP_TIMES_ROMAN_24, const_cast<char*>(chatmsg.c_str()));
00508 glPopMatrix ();
00509
00510 glMatrixMode(GL_PROJECTION);
00511
00512 glPopMatrix();
00513
00514 glMatrixMode(GL_MODELVIEW);
00515
00516 glutSwapBuffers();
00517 }
00518
00519
00521
00525 void ChangeCamPos ( char direction )
00526 {
00527 switch (direction)
00528 {
00529 case 'f':
00530 {
00531 campos[X] = campos[X] + lookdir[X]/speed;
00532 campos[Y] = campos[Y] + lookdir[Y]/speed;
00533 campos[Z] = campos[Z] + lookdir[Z]/speed;
00534 break;
00535 }
00536
00537 case 'b':
00538 {
00539 campos[X] = campos[X] - lookdir[X]/speed;
00540 campos[Y] = campos[Y] - lookdir[Y]/speed;
00541 campos[Z] = campos[Z] - lookdir[Z]/speed;
00542 break;
00543 }
00544 }
00545
00546 vertex temp;
00547 temp.x = lookto[X];
00548 temp.y = lookto[Y];
00549 temp.z = lookto[Z];
00550
00551 colObject = collisionDetection ( &temp );
00552 }
00553
00554
00556
00562 void ChangeCamLookTo(int x, int y)
00563 {
00564 float radiusSegX = 2*PI / WINDOWX;
00565 float radiusSegY = PI / WINDOWY;
00566
00567 int split[2];
00568 #ifdef DEBUG
00569 fprintf(stderr,"chCamLook2 x=%d y=%d \n",x,y);
00570 #endif
00571 float hyp_hor;
00572
00573 x = WINDOWX - x;
00574 split[X] = (WINDOWX/2) - x;
00575 split[Y] = (WINDOWY/2) - y;
00576
00577 float winkel[2];
00578 winkel[X] = radiusSegX * ( (float) split[X] );
00579 winkel[Y] = radiusSegY * ( (float) split[Y] );
00580
00581 lookdir[Y] = sin ( winkel[Y] );
00582 hyp_hor = ( cos ( winkel[Y] ) * -1.0);
00583
00584 lookdir[X] = cos ( winkel[X] ) * hyp_hor;
00585 lookdir[Z] = sin ( winkel[X] ) * hyp_hor;
00586
00587 vertex temp;
00588 temp.x = lookto[X];
00589 temp.y = lookto[Y];
00590 temp.z = lookto[Z];
00591
00592 colObject = collisionDetection ( &temp );
00593 }
00594
00596
00600 void reshape (int x, int y)
00601 {
00602 glMatrixMode(GL_PROJECTION);
00603 glLoadIdentity();
00604 gluPerspective(70.0,1.0*x/y,1.0,5000.0);
00605 glMatrixMode(GL_MODELVIEW);
00606 glLoadIdentity();
00607 glViewport(0,0,x,y);
00608
00609 float percX = (float)x / (float)WINDOWX;
00610 float percY = (float)y / (float)WINDOWY;
00611
00612 //fix me pleeease!!
00613 WINDOWX = x;
00614 WINDOWY = y;
00615
00616 deviceX = (int)( (float)deviceX * percX);
00617 deviceY = (int)( (float)deviceY * percY);
00618 ChangeCamLookTo(deviceX,deviceY);
00619 }
00620
00621
00623
00627 void Init (void)
00628 {
00629 glClearColor (0.0, 0.0, 0.0, 0.0);
00630 glutWarpPointer(WINDOWX/2,WINDOWY/2);
00631
00632 glMatrixMode(GL_PROJECTION);
00633 glLoadIdentity();
00634 gluPerspective(70.0,1.0*WINDOWX/WINDOWY,1.0,5000.0);
00635
00636 glMatrixMode(GL_MODELVIEW);
00637
00638 glShadeModel (GL_SMOOTH);
00639
00640 glEnable(GL_DEPTH_TEST);
00641
00642 Scene = glGenLists (1);
00643
00644 glNewList(Scene,GL_COMPILE);
00645 #define MATRIXSECTORS 6
00646 DrawMatrix((MATRIXSECTORS*200/2), MATRIXSECTORS);
00647 glEndList();
00648
00649 glEnable(GL_LIGHTING);
00650 glEnable(GL_LIGHT0);
00651
00652 deviceX = WINDOWX * 3 / 4;
00653 deviceY = WINDOWY / 2;
00654
00655 ChangeCamLookTo(deviceX,deviceY);
00656 }
00657
00658
00660
00664 void idlefunc()
00665 {
00666 gettimeofday(&t1,NULL);
00667
00668 if ( t1.tv_sec - t2.tv_sec > 0 )
00669 {
00670 frametime = framecounter;
00671 framecounter = 0;
00672 t2.tv_sec = t1.tv_sec;
00673 devasks = 0;
00674
00675 if ( frametime < 25 )
00676 qObj->setFrequency ( frametime ); //WHY DOES THERE COME A FLOATING POINT EXCEPTION WHEN I USE qObj->setFrequency ( frametime )??
00677 else
00678 qObj->setFrequency ( 25 );
00679
00680 }
00681 framecounter++;
00682
00683 devices.update ();
00684
00685 if ( ( devfreq.tv_usec * devasks ) < t1.tv_usec )
00686 {
00687 if (devices.eventStatus (MOVE_FORWARD)) {
00688 #ifdef DEBUG
00689 cerr << "MOVE_FORWARD\n";
00690 #endif
00691
00692 if ( colObject < 0 )
00693 {
00694 packet* pak = new packet;
00695 pak->h.header[0] = PROT_OUTPUT_VISUAL;
00696 pak->h.header[1] = VISUAL_CAMERA_MOVE;
00697 pak->h.header[2] = INPUT_FORWARD;
00698
00699 coreBufferIn->write (pak);
00700 }
00701 };
00702
00703 if (devices.eventStatus (MOVE_BACKWARD)) {
00704 #ifdef DEBUG
00705 cerr << "MOVE_BACKWARD\n";
00706 #endif
00707
00708 packet* pak = new packet;
00709 pak->h.header[0] = PROT_OUTPUT_VISUAL;
00710 pak->h.header[1] = VISUAL_CAMERA_MOVE;
00711 pak->h.header[2] = INPUT_BACKWARD;
00712
00713 coreBufferIn->write (pak);
00714 };
00715
00716 if (devices.eventStatus (ROTATE_UP)) {
00717 #ifdef DEBUG
00718 cerr << "ROTATE_UP\n";
00719 #endif
00720
00721 deviceY -= (int) (devices.eventStatus (ROTATE_UP) * 5.0);
00722
00723 if ( deviceY <= 0 )
00724 deviceY = (WINDOWY - deviceY) % WINDOWY;
00725
00726 ChangeCamLookTo(deviceX,deviceY);
00727 };
00728
00729 if (devices.eventStatus (ROTATE_DOWN)) {
00730 #ifdef DEBUG
00731 cerr << "ROTATE_DOWN\n";
00732 #endif
00733
00734 deviceY += (int) (devices.eventStatus (ROTATE_DOWN) * 5.0);
00735
00736 if ( deviceY >= WINDOWY )
00737 deviceY = deviceY % WINDOWY;
00738
00739 ChangeCamLookTo(deviceX,deviceY);
00740 };
00741
00742 if (devices.eventStatus (ROTATE_LEFT)) {
00743 #ifdef DEBUG
00744 cerr << "ROTATE_LEFT\n";
00745 #endif
00746
00747 deviceX -= (int) (devices.eventStatus (ROTATE_LEFT) * 5.0);
00748
00749 if ( deviceX <= 0)
00750 deviceX = (WINDOWX + deviceX) % WINDOWX;
00751
00752 ChangeCamLookTo(deviceX,deviceY);
00753 };
00754
00755 if (devices.eventStatus (ROTATE_RIGHT)) {
00756 #ifdef DEBUG
00757 cerr << "ROTATE_RIGHT\n";
00758 #endif
00759
00760 deviceX += (int) (devices.eventStatus (ROTATE_RIGHT) * 5.0);
00761
00762 if ( deviceX >= WINDOWX)
00763 deviceX = deviceX % WINDOWX;
00764
00765 ChangeCamLookTo(deviceX,deviceY);
00766 };
00767
00768 devasks++;
00769 }
00770
00771
00772 if (devices.eventStatus (CHAT_MODE)) {
00773 char c = (char) (devices.eventStatus (CHAT_MODE) * 32767);
00774 c++;
00775 #ifdef DEBUG
00776 cerr << "CHAT_MODE: " << c << " " << (int) c << "\n";
00777 #endif
00778
00779 if (c == 13) {
00780 packet* pak = new packet;
00781 pak->h.header[0] = PROT_TEXT_MSG;
00782 pak->data = chatmsg;
00783
00784 coreBufferIn->write (pak);
00785
00786 chatmsg = "";
00787 } else if ((c == 8) && (chatmsg.length () > 0)) {
00788 chatmsg = chatmsg.substr (0, chatmsg.length () - 1);
00789 } else {
00790 chatmsg += c;
00791 };
00792
00793 };
00794
00795 if (devices.eventStatus (EXIT_CLIENT)) {
00796 cerr << "EXIT_CLIENT\n";
00797
00798 packet* pak = new packet;
00799 pak->h.header[0] = PROT_KILL_CLIENT;
00800
00801 coreBufferIn->write (pak);
00802 exit (0);
00803 };
00804
00805
00806 myRotate += ROTATESTEP;
00807 if ( myRotate >= 360.0 ) myRotate = 0.0;
00808
00809 if ( !myInBuffer->isEmpty() )
00810 {
00811 #ifdef DEBUG
00812 fprintf(stderr,"[vT] got a packet!\n");
00813 #endif
00814 packet* pak;
00815 pak = myInBuffer->read ();
00816
00817 switch ( pak->h.header[0] )
00818 {
00819 case VISUAL_CAMERA_MOVE:
00820 {
00821 #ifdef DEBUG
00822 fprintf(stderr,"[vT] VISUAL_CAMERA_MOVE\n");
00823 #endif
00824 if ( pak->h.header[1] == INPUT_FORWARD )
00825 {
00826 ChangeCamPos( 'f' );
00827 }
00828 else
00829 {
00830 ChangeCamPos( 'b' );
00831 }
00832
00833 pak = new packet;
00834
00835 char* temp;
00836 double tempPos[3];
00837
00838 tempPos[0] = campos[0];
00839 tempPos[1] = campos[1];
00840 tempPos[2] = campos[2];
00841
00842 temp = new char[sizeof(double)*3];
00843 memcpy ( temp, &tempPos, sizeof(double)*3);
00844
00845
00846 pak->h.header[0] = PROT_CHANGE_POSITION;
00847 pak->data.assign (temp, sizeof(double)*3);
00848
00849 coreBufferIn->write (pak);
00850
00851 NeedRedraw = 1;
00852 break;
00853 }
00854
00855 case VISUAL_CAMERA_LOOKTO:
00856 {
00857 #ifdef DEBUG
00858 fprintf(stderr,"[vT] VISUAL_CAMERA_LOOKTO\n");
00859 #endif
00860 ChangeCamLookTo(pak->h.header[2],pak->h.header[3]);
00861 NeedRedraw = 1;
00862
00863 delete pak;
00864 break;
00865 }
00866
00867 case VISUAL_NEW_OBJECT:
00868 {
00869 /************************************
00870 h.header[1] ObjectID
00871 data name
00872 ************************************/
00873 #ifdef DEBUG2
00874 fprintf(stderr,"[vT] getting a new object - %d - %d\n", pak->h.header[1],pak->data.length());
00875 #endif
00876
00877 Objects[pak->h.header[1]].name = pak->data;
00878
00879 #ifdef DEBUG2
00880 cerr << "Thats the object: \n" << pak->data;
00881 #endif
00882
00883 delete pak;
00884 break;
00885 }
00886
00887 case VISUAL_MY_OBJECT:
00888 {
00889 myObjectID = pak->h.header[2];
00890 break;
00891 }
00892
00893 case VISUAL_TURN_AVATAR_OFF:
00894 {
00895 showme = !showme;
00896 break;
00897 }
00898
00899 case VISUAL_NEW_ATOM:
00900 {
00901 /**************************************
00902 h.header[1] ObjectID
00903 h.header[2] atomType
00904 h.header[3] atomID
00905 h.header[4] valueType
00906 data The atom data
00907 **************************************/
00908
00909 #ifdef DEBUG4
00910 fprintf(stderr,"[vT] New Atom (%d) type %d - for object %d - %d\n",pak->h.header[3],pak->h.header[2],pak->h.header[1],pak->data.length());
00911 #endif
00912
00913 switch ( pak->h.header[2] )
00914 {
00915 case AT_CHATMSG:
00916 {
00917 dismsg = pak->data;
00918
00919 cerr << dismsg << " ...\n";
00920 Objects[pak->h.header[1]].chat_msg = pak->data;
00921
00922 delete pak;
00923 break;
00924 }
00925
00926 case AT_POSITION:
00927 {
00928
00929 double *pos = Objects[pak->h.header[1]].pos;
00930 memcpy ( pos, pak->data.data(), sizeof(double)*3 );
00931
00932 #ifdef DEBUG2
00933 fprintf(stderr,"pos ausgabe:\n");
00934 fprintf(stderr,"%d %f,%f,%f\n",pak->h.header[1]
00935 ,Objects[pak->h.header[1]].pos[0]
00936 ,Objects[pak->h.header[1]].pos[1]
00937 ,Objects[pak->h.header[1]].pos[2]);
00938 #endif
00939
00940 delete pak;
00941 break;
00942 }
00943
00944 case AT_3DSIAGRAPH:
00945 {
00946 if ( pak->h.header[1] < 0 )
00947 {
00948 environment = glGenLists (1);
00949
00950 glNewList(environment,GL_COMPILE);
00951 parseObject ( pak->data );
00952 glEndList();
00953
00954 haveenvironment = true;
00955 }
00956 else
00957 {
00958 Objects[pak->h.header[1]].visual = glGenLists (1);
00959
00960 glNewList(Objects[pak->h.header[1]].visual,GL_COMPILE);
00961
00962 Objects[pak->h.header[1]].hull = parseObject ( pak->data );
00963
00964 glEndList();
00965
00966 Objects[pak->h.header[1]].havedata = true;
00967 }
00968
00969 delete pak;
00970 break;
00971 }
00972
00973 default:
00974 {
00975 fprintf(stderr,"[vT] THIS CANNOT BE!! IT CAN'T!!\n");
00976 delete pak;
00977 exit(1);
00978 break;
00979 }
00980 }
00981 break;
00982 }
00983
00984 default:
00985 {
00986 #ifdef DEBUG
00987 fprintf(stderr,"[vT] heey, DEFAULT IS BORING!!!!\n");
00988 #endif
00989 break;
00990 }
00991
00992 }
00993 }
00994
00995
00996 lookto[X] = lookdir[X] + campos[X];
00997 lookto[Y] = lookdir[Y] + campos[Y];
00998 lookto[Z] = lookdir[Z] + campos[Z];
00999
01000 glutPostRedisplay();
01001 }
01002
01003 /*...................................................................
01004 Description: The Visualization Thread
01005 Args:
01006 Returns:
01007 Created: xandi, 130400
01008 [ToDo:]
01009 [Comments:]
01010 Changes:
01011 -------------------------------------------------------------------*/
01012
01014
01019 void *VisualizationThread ( void* targz )
01020 {
01021 ThreadArgs* targs = (ThreadArgs*) targz;
01022 myInBuffer = targs->in;
01023
01024 fprintf(stderr,"[vT] Visualization Thread started... %p\n",targs->in);
01025
01026 /* i sometimes forget to export the correct DISPLAY.*/
01027 if (getenv("DISPLAY") == NULL)
01028 {
01029 cerr << "[display-environment-check] DISPLAY not set...\tcontinuing\n"<<endl;
01030
01031 if (putenv("DISPLAY=0:0"))
01032 cerr << "[display-environment-check]\tsetting DISPLAY environement-var to 0:0 failed\n";
01033 cerr << "[display-environment-check]\tsetting DISPLAY=0:0\n";
01034 }
01035
01036 devfreq.tv_usec = 1000000 / 26;
01037
01038
01039 while ( !myInBuffer ) usleep (10000); //useless actually
01040 while ( !coreBufferIn ) usleep (10000);
01041
01042 extern Buffer* outputBufferIn;
01043 qObj = new QuartzClass ( outputBufferIn, PROT_GET_ALL_CHANGED, 25 );
01044 pthread_t qThread;
01045 pthread_create (&qThread, NULL, QuartzThread, qObj);
01046
01047 glutInit(&targs->argc,targs->argv);
01048 WINDOWX = 640;
01049 WINDOWY = 480;
01050
01051 glutInitDisplayMode (GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
01052 glutInitWindowSize ( WINDOWX, WINDOWY );
01053 glutInitWindowPosition ( 0, 0);
01054 glutCreateWindow ( "[3Dsia] Reference Client" );
01055
01056 Init ();
01057
01058 cerr << devices.addDevicePlugIn ("client/plugins/deviceMouse.so") << "\n";
01059 devices.connect (0, 0, MOVE_FORWARD);
01060 devices.connect (0, 1, MOVE_BACKWARD);
01061 devices.connect (0, devices.getDeviceButtons (0), ROTATE_UP);
01062 devices.connect (0, devices.getDeviceButtons (0) + 1, ROTATE_DOWN);
01063 devices.connect (0, devices.getDeviceButtons (0) + 2, ROTATE_LEFT);
01064 devices.connect (0, devices.getDeviceButtons (0) + 3, ROTATE_RIGHT);
01065
01066 cerr << devices.addDevicePlugIn ("client/plugins/deviceKeyboard.so") << "\n";
01067 devices.connect (1, 0, MOVE_FORWARD);
01068 devices.connect (1, 1, MOVE_BACKWARD);
01069 devices.connect (1, 2, EXIT_CLIENT);
01070 devices.connect (1, 3, INC_SPEED);
01071 devices.connect (1, 4, DEC_SPEED);
01072 devices.connect (1, devices.getDeviceButtons (1), ROTATE_UP);
01073 devices.connect (1, devices.getDeviceButtons (1) + 1, ROTATE_DOWN);
01074 devices.connect (1, devices.getDeviceButtons (1) + 2, ROTATE_LEFT);
01075 devices.connect (1, devices.getDeviceButtons (1) + 3, ROTATE_RIGHT);
01076 devices.connect (1, devices.getDeviceButtons (1) + 4, CHAT_MODE);
01077
01078 glutDisplayFunc (display);
01079 glutReshapeFunc (reshape);
01080 glutIdleFunc (idlefunc);
01081 glutMainLoop ();
01082
01083 return targz;
01084 }