Beaming Scene Service  2.0
RakNet wrapper for managing data communications between multiple Beaming clients
sampleXVRclient/client.s3d
Go to the documentation of this file.
00001 /* Include s3d symbols */
00002 #include <Script3d.h>
00003 //#define DQUAT_TRANSF_XVRSHADER
00004 //#define VA_TRANS
00005 #define TM_TRANSF_XVRSHADER
00006 #include "Common.s3d"
00007 #include "avatars.s3d"
00008 #include "dictxvr.h.s3d"
00009 
00010 // Function declarations
00011 extern function NewLight;
00012 //extern function GetTrackerPos;
00013 //extern function GetTrackerRot;
00014 //extern function GetTrackerJoy;
00015 //extern function GetTrackerButtons;
00016 function CameraMouseMove();
00017 function CameraMoveWand();
00018 function KeyStatus(keycode);
00019 function KeyBoardManager();
00020 function AxisAngleToQuat(a1,q1);
00021 function matrixToAngles(m, a);
00022 function DrawBoneAxes(avId,boneId);
00023 function CheckAndGetData();
00024 function updateRemoteClients();
00025 function avatarLookAt(av_id,bone_id,target);
00026 function shortestArc(from, to);
00027 function slerp(qa, qb, t);
00028 
00029 // Scene parameters (Field of view, near clipping plane, far clipping plane)
00030 SET SCENE_FOV = 60;
00031 SET SCENE_NEAR = 0.1;
00032 SET SCENE_FAR = 500;
00033 
00034 #define IBMCLIENT
00035 #define DEGTORAD (3.141596/180)
00036 #define RADTODEG (180/3.141596)
00037 #define CLIENT ("UCL") //define your client name here e.g. UCL, UB, etc
00038 #define CLIENTTYPE ("AVATAR") //define your client type here e.g. AVATAR, ROOM, etc
00039 var viewer_mode = 0; //change to 1 if running in viewer-only mode - no avatar will be created
00040 //#define CAVEMODE      //comment this line if running on desktop with just mouse control
00041 
00042 static var cave_half_width = 1.5;
00043 static var cave_half_height = 1.1;
00044 // Variables to manage camera
00045 var CamPos = [0,6.4,2];                 //camera position
00046 // Variables to manage light
00047 var Luce1;
00048 var PosL1 = [0.0,10.0,0.0]      ;       //default light position  
00049 // Variables for 3d geometry
00050 var m_mesh1;    // Variables for meshes 
00051 var m_obj1;     // Variables for objects
00052 var m_mesh2;    // Variables for meshes 
00053 var m_obj2;     // Variables for objects
00054 var m_avatar;   // Variables for objects
00055 var m_cam; //variable for camera
00056 var m_mesh;
00057 var index = 0;
00058 var avatarArray = {};
00059 var angolo = 0.0;
00060 var sens = 0.01;
00061 var key_offset = 0.1;
00062 var prev_key = "";
00063 var morphval=vector(10);
00064 var normalinfl=0.0;
00065 var translation=vector(2);
00066 var clientdll;
00067 var my_avatarId;
00068 var my_avatar = "m016";
00069 var y_pos = 0;
00070 var rightarm_quat = [0,0,0,0];
00071 var leftarm_quat = [0,0,0,0];
00072 var rightleg_quat = [0,0,0,0];
00073 var leftleg_quat = [0,0,0,0];
00074 var root_quat = [0,0,0,0];
00075 var rotSwitch = true;
00076 var angleX = 0;
00077 var avatarmap;
00078 var animationPlaying = false;
00079 var resetleg = false;
00080 var changemodel = false;
00081 
00082 function OnDownload(params)
00083 {
00084 }
00085 
00086 function OnInit(params)
00087 {
00088         //Font settings
00089         ConsoleFont("Lucida Console",20);
00090         ConsoleColor(0,1,0);
00091 
00092         SetLocalDir();
00093     clientdll = CVmExternDll("..\\netClient\\Release\\netClient.dll");
00094     clientdll.__AddFunction(C_VOID, "startclient", C_PCHAR, C_INT, C_PCHAR, C_PCHAR, C_PCHAR, C_INT, C_INT, C_INT, C_INT);
00095     clientdll.__AddFunction(C_VOID, "check");
00096     clientdll.__AddFunction(C_VOID, "addRocketBoxAvatar", C_PCHAR,C_PSTR);
00097      clientdll.__AddFunction(C_VOID, "deleteRocketBoxAvatar", C_PCHAR);
00098    clientdll.__AddFunction(C_VOID, "setAvatarName", C_PCHAR,C_PCHAR,C_PCHAR);
00099     clientdll.__AddFunction(C_VOID, "updateRocketBoxAvatar", C_PCHAR, C_PCHAR, C_FLOAT, C_FLOAT, C_FLOAT, C_FLOAT, C_FLOAT, C_FLOAT, C_FLOAT);
00100     clientdll.__AddFunction(C_VOID, "removeAllNodes");
00101     clientdll.__AddFunction(C_VOID, "stop");
00102     clientdll.__AddFunction(C_VOID, "exitlibrary");
00103     clientdll.__AddFunction(C_INT, "getAvatarSpecificData", C_PSTR, C_PSTR, C_PSTR, C_PFLOAT);
00104     clientdll.__AddFunction(C_INT, "getAvatarData", C_PSTR, C_PSTR, C_PSTR, C_PFLOAT);
00105     clientdll.__AddFunction(C_INT, "getPeersID", C_PSTR);
00106     clientdll.__AddFunction(C_INT, "getNodesInfo", C_PSTR, C_PSTR);
00107     clientdll.__AddFunction(C_VOID, "getAvatarName", C_PCHAR,C_PCHAR,C_PCHAR,C_PCHAR);
00108     clientdll.startclient("127.0.0.1"/*"128.16.7.66"*/, 12050, CLIENT, CLIENTTYPE, my_avatar+".cfg", viewer_mode, 2, 1, 30); 
00109     
00110         //initialise avatars
00111         SetLocalDir();
00112         initAvatars();
00113         
00114     // Setup Camera
00115         //CameraSetPosition(CamPos);    
00116 
00117         // Setup First Light
00118         Luce1 = CVmLight();
00119         Luce1.SetDiffuse(1,1,1);
00120         Luce1.Enable();         
00121         //Setup Initial Position
00122         Luce1.SetPosition(PosL1);
00123 
00124     SetLocalDir(".\\misc");
00125     
00126         m_mesh1 = CVmNewMesh("uclquad.AAM");
00127         m_mesh1.Normalize(500);
00128         m_mesh1.CenterBoundingBox();
00129         var mesh_bounds = m_mesh1.GetBoundingBox();
00130         m_obj1 = CVmObj();
00131         m_obj1.LinkToMesh(m_mesh1);     
00132         m_obj1.SetPosition(0,mesh_bounds[4]-2.5,0);
00133         m_obj1.Rotate(45,0,1,0);
00134         //m_obj1.SetScale(10,1,10);
00135 
00136         m_mesh2 = CVmNewMesh("leaningtowerpisa.AAM");
00137         m_mesh2.Normalize(500);
00138         m_mesh2.CenterBoundingBox();
00139         mesh_bounds = m_mesh2.GetBoundingBox();
00140         m_obj2 = CVmObj();
00141         m_obj2.LinkToMesh(m_mesh2);     
00142         m_obj2.SetPosition(0,mesh_bounds[4]-2.5,0);
00143         m_obj2.Rotate(45,0,1,0);
00144         //m_obj2.SetScale(10,1,10);
00145         
00146         m_mesh = CVmNewMesh("dummy.AAM");
00147         m_mesh.Normalize(0.1);
00148         //m_mesh.HideAll();
00149         m_cam = CVmObj();
00150         m_cam.LinkToMesh(m_mesh);
00151         m_cam.SetPosition(0,0,0);
00152 
00153     SetLocalDir("");
00154 
00155         if (viewer_mode == 0)
00156         {
00157                 //initialise a dummy/invisible avatar for my client locally
00158                 my_avatarId = Avatars.addCharacter(my_avatar,my_avatar+".cfg");
00159                 Avatars.IdleOne(my_avatarId);
00160                 clientdll.addRocketBoxAvatar("ucl_avatar","m016.cfg");
00161                 clientdll.setAvatarName("ucl_avatar","John","Doe");
00162                 Avatars.getRotation(my_avatarId,66,&leftarm_quat);
00163                 Avatars.getRotation(my_avatarId,90,&rightarm_quat);
00164                 Avatars.getRotation(my_avatarId,113,&leftleg_quat);
00165                 Avatars.getRotation(my_avatarId,118,&rightleg_quat);
00166         }
00167         avatarmap = Hashtable();
00168         //CameraSetBounds(m_mesh1.GetBoundingBox());
00169         
00170                                 
00171 #ifdef CAVEMODE
00172         SceneSetParam(VR_HEADTRACKER,1);
00173 #endif
00174 }
00175 
00176 function OnFrame()
00177 {
00178         #ifdef CAVEMODE
00179                 CameraMoveWand();       // Manage camera movement with wand
00180         #else
00181                 CameraMouseMove();      // Manage camera movement with mouse
00182                 CameraSetPosition(CamPos);      
00183         #endif
00184         KeyboardManager();
00185         Luce1.SetPosition(CameraGetPosition(0).x,CameraGetPosition(0).y,CameraGetPosition(0).z);
00186         //get camera rotation matrix
00187         var rot_mat4 = CameraGetRotationMatrix();
00188         //extract rotation matrix as 3x3 matrix
00189         var rot_mat3 = [rot_mat4[0], rot_mat4[1], rot_mat4[2], rot_mat4[4], rot_mat4[5], rot_mat4[6], rot_mat4[8], rot_mat4[9], rot_mat4[10]];
00190         var rot_euler = [0,0,0];
00191         matrixToAngles(rot_mat3, &rot_euler);
00192         //output yaw, taking both head orientation and world orientation into account
00193 #ifdef CAVEMODE //cave w is doubled - issue with glRotate rotating by 360 when w=180
00194         root_quat = AxisAngleToQuaternion([0,1,0,DEGTORAD*((2*(RADTODEG*rot_euler[1]))-GetTrackerRot(0).y)]);
00195         //AxisAngleToQuat([0,1,0,DEGTORAD*(GetTrackerRot(0).y-(2*(RADTODEG*rot_euler[1])))],&rot_quat);
00196 #else
00197         root_quat = AxisAngleToQuaternion([0,1,0,DEGTORAD*((RADTODEG*rot_euler[1])-GetTrackerRot(0).y)]);
00198         //AxisAngleToQuat([0,1,0,DEGTORAD*(GetTrackerRot(0).y-(RADTODEG*rot_euler[1]))],&rot_quat);
00199 #endif
00200         //ConsoleText(0.04,0.50, str(GetTrackerRot(0).y+(/*2**/(RADTODEG*rot_euler[1]))));
00201         
00202         Avatars.Idle();
00203         if (viewer_mode == 0)
00204         {
00205                 //update root's position and orientation locally
00206                 Avatars.setRotation(my_avatarId,0,root_quat); //set local-coord orientation of local avatar
00207                 Avatars.setTranslation(my_avatarId,0,[CameraGetPosition(0).x, y_pos, CameraGetPosition(0).z]);//set local-coord position of local avatar
00208                 var tmpquat = [0,0,0,0];
00209                 AxisAngleToQuat([0,1,0,DEGTORAD*-90],&tmpquat);
00210                 Avatars.addRotation(my_avatarId,0,tmpquat);
00211                 //ConsoleText(0.04,0.20,str(CameraGetPosition(0)));
00212                 
00213                 /*
00214                 //To-do: set angleX based on change in translation rather than mouse movement 
00215                 //do this also for remote avatars in CheckAndGetData()
00216                 Avatars.setRotation(my_avatarId,66,leftarm_quat);
00217                 Avatars.addRotation(my_avatarId,66,AxisAngleToQuaternion([0,0,1,DEGTORAD*85]));
00218                 Avatars.addRotation(my_avatarId,66,AxisAngleToQuaternion([0,1,0,DEGTORAD*-angleX]));
00219                 Avatars.setRotation(my_avatarId,90,rightarm_quat);
00220                 Avatars.addRotation(my_avatarId,90,AxisAngleToQuaternion([0,0,1,DEGTORAD*-85]));
00221                 Avatars.addRotation(my_avatarId,90,AxisAngleToQuaternion([0,1,0,DEGTORAD*angleX]));
00222                 Avatars.setRotation(my_avatarId,113,leftleg_quat);
00223                 Avatars.addRotation(my_avatarId,113,AxisAngleToQuaternion([0,0,1,DEGTORAD*-2.5]));
00224                 Avatars.addRotation(my_avatarId,113,AxisAngleToQuaternion([0,1,0,DEGTORAD*-angleX]));
00225                 Avatars.setRotation(my_avatarId,118,rightleg_quat);
00226                 Avatars.addRotation(my_avatarId,118,AxisAngleToQuaternion([0,0,1,DEGTORAD*2.5]));
00227                 Avatars.addRotation(my_avatarId,118,AxisAngleToQuaternion([0,1,0,DEGTORAD*angleX]));
00228                 //output(angleX," ");
00229                 */
00230                 //remote updates - reads local position and orientation and updates remote clients 
00231                 updateRemoteClients();
00232         }
00233 
00234         clientdll.check(); //checks for incoming connections
00235         
00236     SceneBegin(VR_STEREO_LEFT);
00237         glPushMatrix();
00238         glDisable(GL_CULL_FACE);
00239 
00240         DrawGrid([0.5, 0.5, 0.5], 100);
00241 
00242         CheckAndGetData(); //check for updates to remote avatars/objects
00243 
00244         // put drawing code here
00245         //m_cam.Draw();
00246 
00247 #ifdef CAVEMODE
00248         //var direzione=GetTrackerPos(0);
00249         var posizione = CameraGetPosition();
00250 /*      glTranslate(posizione + [0, 0, 1.5]);
00251         glRotate(angolo, 0, 1, 0);
00252         glTranslate(-posizione - [0, 0, 1.5]);*/
00253         glRotate(angolo, 0, 1, 0);
00254         glTranslate(-posizione - [cave_half_width, cave_half_height,0]);
00255 #endif  
00256         //m_cam.Draw();
00257         if (changemodel)
00258                 m_obj2.Draw();
00259         else
00260                 m_obj1.Draw();
00261         glPopMatrix();
00262     SceneEnd();
00263         
00264 }
00265 
00266 function updateRemoteClients()
00267 {
00268         var tmpquat = [0,0,0,0];
00269         var translate[3], rotate[4];
00270         for (var i=0; i<=122; i++)
00271         {
00272                 //get current position/orientation
00273                 Avatars.getTranslation(my_avatarId,i,&translate); //local-coord position
00274                 Avatars.getRotation(my_avatarId,i,&rotate); //local-coord orientation
00275                 //inform remote clients 
00276                 clientdll.updateRocketBoxAvatar("ucl_avatar", str(i), translate[0], translate[1], translate[2],rotate[0],rotate[1],rotate[2],rotate[3]); 
00277                 #ifdef IBMCLIENT
00278                 if (i==0)
00279                 {
00280                         clientdll.updateRocketBoxAvatar("ucl_avatar", str(i), -translate[0], translate[1], translate[2],rotate[0],rotate[1],rotate[2],rotate[3]); 
00281                 }
00282                 #endif
00283         }
00284 }
00285 
00286 
00287 function CheckAndGetData()
00288 {
00289     var new_clients = space(1024);
00290     var allnodes = space(1024);
00291     var new_avatar = space(128);
00292     var client_type = space(128);
00293     var client_name = space(128);
00294     var avatar_name = space(128);
00295     //get newly added client avatars
00296         var avatarArray = {};
00297     var client_count = clientdll.getPeersID(&new_clients);//,&client_type, &client_name, &avatar_name
00298     var guids = split(new_clients,";");
00299         if (client_count>0) //if there are connected clients
00300         {
00301             foreach(var id in guids)
00302                 {
00303                         var node_count = clientdll.getNodesInfo(id,&allnodes);
00304                         var nodes_info = split(allnodes,";");
00305                     foreach(var node in nodes_info)
00306                     {
00307                                 var node_details = split(node,",");
00308                                 if ( len(node_details) > 2 )
00309                                 {
00310                                         if (/*(x==3) &&*/ (node_details[2] == "AVATAR"))
00311                                         {
00312                                                 var avatar_id=space(64);
00313                                                 var avatar_cfg=space(32);
00314                                             var fname = space(32);
00315                                             var lname = space(32);
00316                                                 avatar_id = node_details[0];
00317                                                 //avatar_type = details[2];
00318                                                 avatar_cfg = node_details[3];
00319                                                 //output("\t\tAvatar:");
00320                                                 var avatar_data = Vector(7*123);
00321                                                 var joints = space(3*123);
00322                                                 //print out root position
00323                                                 //clientdll.getAvatarSpecificData(id,avatar_id,"0",&avatar_data);
00324                                                 clientdll.getAvatarData(id,avatar_id,&joints,&avatar_data);
00325                                                 clientdll.getAvatarName(id,avatar_id,&fname,&lname);
00326                                                 //update and draw remote avatars, see commented code below
00327                                                 var halcaId = avatarmap.get(id+avatar_id);
00328                                                 //ConsoleText(0.04,0.20,str(halcaId));
00329                                                 if (halcaId==Void)
00330                                                 {
00331                                                         //ConsoleText(0.04,0.10,str(avatar_data));
00332                                                         OutputLN("Adding ", id, ": ", node);
00333                                                         OutputLN("\t", avatar_id, ": ",/* str(avatar_data),*/ fname," ", lname);
00334                                                         halcaId = Avatars.addCharacter(left(avatar_cfg,(len(avatar_cfg)-4)),avatar_cfg);//("m016","m016.cfg");
00335                                                         avatarmap.put(id+avatar_id,halcaId);
00336                                                         Avatars.IdleOne(halcaId);
00337                                                 }
00338                                                 var c = 0;
00339                                                 var joints_info = split(joints,",");
00340                                                 foreach (var joint in joints_info)
00341                                                 {
00342                                                         if (c==0) {
00343                                                         #ifdef IBMCLIENT
00344                                                                 Avatars.setTranslation(halcaId,0,[avatar_data[2],5.6,-avatar_data[0]]); //position update
00345                                                         #else
00346                                                                 Avatars.setTranslation(halcaId,0,[avatar_data[0],5.6,avatar_data[2]]); //position update
00347                                                         #endif
00348                                                         } else {
00349                                                                 Avatars.setTranslation(halcaId,c,[avatar_data[0+(c*7)],avatar_data[1+(c*7)],avatar_data[2+(c*7)]]); //position update
00350                                                         }
00351                                                         var my_quat = [avatar_data[3+(c*7)],avatar_data[4+(c*7)],avatar_data[5+(c*7)],avatar_data[6+(c*7)]];
00352                                                         if ((c==113 || c==118) && (resetleg))
00353                                                         {
00354                                                                 Avatars.resetBone(halcaId,c);
00355                                                                 Avatars.addRotation(halcaId,c,my_quat);
00356                                                         }
00357                                                         else 
00358                                                         {       
00359                                                                 Avatars.setRotation(halcaId,c,my_quat); //orientation update
00360                                                         }
00361                                                         if (c==0)
00362                                                         {
00363                                                                 AxisAngleToQuat([0,1,0,DEGTORAD*-90],my_quat);
00364                                                                 Avatars.addRotation(halcaId,2,my_quat);
00365                                                         }
00366                                                         c++;
00367                                                 }
00368                                                 #ifdef VA_TRANS
00369                                                         Avatars.Draw(0);
00370                                                 #else
00371                                                         skinshader.start();
00372                                                         skinshader.setParameter("normalinfl",normalinfl);
00373                                                         
00374                                                         Avatars.DrawExtShaderOne(halcaId);
00375                                                         skinshader.stop();
00376                                                 #endif
00377                                         }
00378                                 }
00379                         }
00380                 }   
00381         }
00382 }
00383 
00384 
00385 function shortestArc(from, to)
00386 { //not sure if this is correct - hint: normalise axis part of the result
00387         var cross = from ^ to ; //Compute vector cross product
00388         var dot   = from * to ; //Compute dot product
00389     
00390     dot = sqrt( 2*(dot+1) ) ; //We will use this equation twice
00391     cross /= dot ; //Get the x, y, z components
00392 
00393         //Return with the w component (Note that w is inverted because Cal3D has left-handed rotations
00394         return [cross[0], cross[1], cross[2], dot/2];
00395 }
00396 
00397 //call this function to draw the axes of one bone
00398 function DrawBoneAxes(avId,boneId)
00399 {
00400         Var bTrans=Vector(3);
00401         Var xAx=Vector(3);
00402         Var yAx=Vector(3);
00403         Var zAx=Vector(3);
00404         Var bMat=Vector(9);
00405 
00406         Avatars.getTranslationAbs(avId,boneId,&bTrans);
00407         Avatars.getRotationMatrixAbs(avId,boneId,&bMat);
00408         xAx[0]=bMat[0];xAx[1]=bMat[3];xAx[2]=bMat[6];
00409         yAx[0]=bMat[1];yAx[1]=bMat[4];yAx[2]=bMat[7];
00410         zAx[0]=bMat[2];zAx[1]=bMat[5];zAx[2]=bMat[8];
00411         DrawAxes(bTrans,xAx,yAx,zAx);
00412 }
00413 
00414 function slerp(qa, qb, t) 
00415 {
00416         // quaternion to return
00417         var qm = Vector(4);
00418         // Calculate angle between them.
00419         var  cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z;
00420         // if qa=qb or qa=-qb then theta = 0 and we can return qa
00421         if (abs(cosHalfTheta) >= 1.0){
00422                 qm.w = qa.w;qm.x = qa.x;qm.y = qa.y;qm.z = qa.z;
00423                 return qm;
00424         }
00425         // Calculate temporary values.
00426         var halfTheta = acos(cosHalfTheta);
00427         var sinHalfTheta = sqrt(1.0 - cosHalfTheta*cosHalfTheta);
00428         // if theta = 180 degrees then result is not fully defined
00429         // we could rotate around any axis normal to qa or qb
00430         if (abs(sinHalfTheta) < 0.001){ // fabs is floating point absolute
00431                 qm.w = (qa.w * 0.5 + qb.w * 0.5);
00432                 qm.x = (qa.x * 0.5 + qb.x * 0.5);
00433                 qm.y = (qa.y * 0.5 + qb.y * 0.5);
00434                 qm.z = (qa.z * 0.5 + qb.z * 0.5);
00435                 return qm;
00436         }
00437         var ratioA = sin((1 - t) * halfTheta) / sinHalfTheta;
00438         var ratioB = sin(t * halfTheta) / sinHalfTheta; 
00439         //calculate Quaternion.
00440         qm.w = (qa.w * ratioA + qb.w * ratioB);
00441         qm.x = (qa.x * ratioA + qb.x * ratioB);
00442         qm.y = (qa.y * ratioA + qb.y * ratioB);
00443         qm.z = (qa.z * ratioA + qb.z * ratioB);
00444         return qm;
00445 }
00446 
00447 function AxisAngleToQuat(a1, q1) {
00448   var s = sin(a1[3]/2);
00449   q1[0] = a1[0] * s;
00450   q1[1] = a1[1] * s;
00451   q1[2] = a1[2] * s;
00452   q1[3] = cos(a1[3]/2);
00453 }
00454 
00455 // return euler angles in radians from a rotation matrix
00456 function matrixToAngles(m, a){
00457         if(abs(m[1]) >= 0.9999)
00458         {
00459                 a[0] = atan2(-m[4],m[8]);
00460                 a[1] = 0;
00461                 a[2] = PI/2;
00462                 return a;       
00463         }
00464         a[0] = atan2(-m[9], m[5]);
00465         a[1] = atan2(-m[2], m[0]);
00466         a[2] = asin(m[1]);
00467         return a;
00468 }
00469 
00470 function OnExit()
00471 {
00472         clientdll.deleteRocketBoxAvatar("ucl_avatar");
00473         clientdll.removeAllNodes();
00474     clientdll.stop();
00475     clientdll.exitlibrary();
00476         Avatars.ShutDown();
00477 }
00478 
00479 //-------------------------------------------------------------
00480 // I/O management
00481 //-------------------------------------------------------------
00482 function CameraMoveWand(){
00483         var wand_xrot, wand_yrot;
00484         var WandFrontVector[3];
00485         var cos_wand_xrot;
00486         var button_mask; //buttonmask: red 1, yellow 2, green 4, blue 8, joystick button 16, trigger 32
00487         
00488         static var joystick_move_sensitivity = 0.2;
00489         static var joystick_rotate_sensitivity = 2.0;
00490         static var joystick_dead_zone = 0.2;
00491         
00492         wand_xrot = GetTrackerRot(1).x;
00493         wand_yrot = GetTrackerRot(1).y;
00494         cos_wand_xrot = cos(DEGTORAD*wand_xrot);
00495         button_mask = GetTrackerButtons(1); 
00496         var joy_x = GetTrackerJoy(1).x;
00497         if(abs(joy_x) >joystick_dead_zone){
00498                 angolo += joystick_rotate_sensitivity*joy_x;
00499         }
00500         
00501         var joy_y = GetTrackerJoy(1).y;
00502         if(abs(joy_y) > joystick_dead_zone){
00503                 CamPos = CameraGetPosition();
00504                 var temp = [0,0,-joy_y];
00505                 norm(temp);
00506                 temp = VectorRotate(-angolo, [0,1,0], temp);
00507                 temp = VectorRotate(wand_xrot, [1,0,0], temp);
00508                 temp = VectorRotate(wand_yrot, [0,1,0], temp);
00509                 CameraSetPosition(CamPos + temp.x0z*joystick_move_sensitivity);
00510         }
00511         
00512         CameraSetRotation(angolo,0,1,0);
00513 }
00514 
00515 
00516 function KeyboardManager()
00517 {
00518         if (KeyStatus("S") == 1)
00519         {
00520                         output("key press \"S\"\n");
00521         }
00522         var key = keyboard();
00523         if (key != ""){
00524         output("key is *", key, "*\n");
00525                 CamPos = CameraGetPosition();
00526                 if (key == "q"){
00527                         /*Avatars.ShutDown();
00528                         clientdll.removeAllNodes();
00529                     clientdll.stop();
00530                         Quit();*/
00531                 }
00532                 if (key == "r"){
00533                         resetleg = !resetleg;
00534                 }
00535                 if (key == "h"){
00536                         CamPos[0] -= key_offset;
00537                 }
00538                 if (key == "l"){
00539                         CamPos[0] += key_offset;
00540                 }
00541                 if (key == "j"){
00542                         CamPos[1] -= key_offset;
00543                 }
00544                 if (key == "k"){
00545                         CamPos[1] += key_offset;
00546                 }
00547                 if (key == "a"){
00548                         CamPos[2] -= key_offset;
00549                 }
00550                 if (key == "c"){
00551                         clientdll.addRocketBoxAvatar("avatar_name","m016.cfg");
00552                 }
00553                 if (key == "d"){
00554                         clientdll.removeAllNodes();
00555                 }
00556                 if (key == " ") 
00557                 {
00558                         changemodel = !changemodel;
00559                 }       
00560                 if (key == "z"){
00561                         //CamPos[2] -= key_offset;
00562                         //testing
00563 /*                      output(m_mesh1.GetSubsetNumber(),"\n");
00564                         output(m_mesh.GetSubsetsFromName("",false),"\n");
00565                         output(m_mesh1.GetSubsetsFromName("",false),"\n");
00566                         var rad = 0;
00567                         var bb_center[3];
00568                         var translate[3];
00569                         var rotate[4];
00570                         //obtain positions of all subsets in mesh - safe to assume rotation is fixed
00571                         for (var i=0; i<m_mesh1.GetSubsetNumber(); i++)
00572                         {
00573                                 if (!m_mesh1.IsHidden(i))
00574                                 {
00575                                         m_mesh1.SubsetGetBoundingSphere(i,&rad,&bb_center);
00576                                         //output(m_mesh1.SubsetGetDescription(i), " ", bb_center, "\n");
00577                                 }
00578                         }
00579                         //only gives rotation of joints only (no translation values)
00580                         var allbones = vector(3+4*Avatars.getNumBone(0));
00581                         Avatars.getAllBoneState(0, &allbones);
00582                         //output(len(allbones), " ", allbones);
00583                         //get translation and rotation values of animated avatar 
00584                         for (var i=0; i<Avatars.getNumBone(0); i++)
00585                         {
00586                                 Avatars.getTranslationAbs(0,i,&translate); //tested - gets world coord
00587                                 Avatars.getRotationAA(0,i,&rotate); //local rotation values
00588                                 var ind = 3+4*i;
00589                                 if (i==0)
00590                                 {
00591                                         output(i, " ", translate, " ", rotate, "\n");
00592                                         //output(i, " ", translate, " ", QuaternionToAxisAngle([allbones[ind],allbones[ind+1],allbones[ind+2],allbones[ind+3]]), "\n");
00593                                 }
00594                         }*/
00595                 }
00596                 CameraSetPosition(CamPos);
00597         }
00598         prev_key = key;
00599 }
00600 
00601 
00602 // Camera manager (using mouse)
00603 // Right button (and not Left) -> Translation on X and Z axis
00604 // Right and Left Button -> Translation on Y axis 
00605 // Left button (and not Right) -> Camera rotation
00606 function CameraMouseMove()
00607 {
00608         static var InMouseR = false, InMouseL = false;                          
00609         static var PrecX = 0, PrecY = 0;                        
00610         // Change these values to modify the mouse sensitivity
00611         var TR_SENSITIVITY  = 0.0001;
00612         var ROT_SENSITIVITY = 0.01;
00613         // Mouse manager
00614 
00615         if(Mouse.ButtonL && !Mouse.ButtonR)
00616         {
00617                 //====  Left Button: Camera rotation  ====//
00618                 //output("camera rotation\n");
00619                 if (InMouseL )
00620                 {                       
00621                         CameraRotate(( Mouse.X-PrecX)*ROT_SENSITIVITY,0,1,0);
00622                         //CameraRotateABS((Mouse.Y-PrecY)*ROT_SENSITIVITY,1,0,0);               
00623                 }
00624                 else
00625                 {
00626                         PrecX = Mouse.X;
00627                         PrecY = Mouse.Y;
00628                 }
00629                 InMouseL = true;
00630                 InMouseR = false;
00631         }
00632         else
00633         if(Mouse.ButtonR)
00634         {
00635                 //====  Right Button: Camera translation  ====//
00636                 if (InMouseR)
00637                 {
00638                         var CameraMatrix = CameraGetMatrix();
00639                         CamPos = CameraGetPosition();
00640                         if (!Mouse.ButtonL)
00641                         //====  Translation on X and Z axis ====//
00642                         {
00643                                 //output("camera translation in X and Z\n");
00644                                 CamPos += CameraGetZAxis() * (Mouse.y-PrecY)*TR_SENSITIVITY + CameraGetXAxis() * (Mouse.X- PrecX)*TR_SENSITIVITY;
00645                                 if (rotSwitch)
00646                                         angleX-=0.5;
00647                                 else if (!rotSwitch)
00648                                         angleX+=0.5;
00649                                 if (angleX > 20)
00650                                         rotSwitch = true;
00651                                 if (angleX < -20)
00652                                         rotSwitch = false;
00653                         }
00654                         else
00655                         //====  Right + Left Button: Translation on Y axis ====//
00656                         {
00657                                 CamPos -= CameraGetYAxis() * (Mouse.y-PrecY)*TR_SENSITIVITY;
00658                         }
00659                         CameraSetPosition(CamPos);              
00660                 }
00661                 else
00662                 {
00663                         PrecX = Mouse.X;
00664                         PrecY = Mouse.Y;
00665                 }
00666                 InMouseR = true;
00667                 InMouseL = false;
00668         }       
00669         else
00670         {
00671                 InMouseR = false;
00672                 InMouseL = false;
00673                 if ((angleX != 0)&&(angleX > 0))
00674                         angleX-=0.5;
00675                 if ((angleX != 0)&&(angleX < 0))
00676                         angleX+=0.5;
00677         }
00678 }
00679 
00680 
00681 function KeyStatus(keycode)
00682 {
00683 //      output("Keystatus(", keycode, ")\n");
00684 
00685         if (valtype(keycode) == "S")
00686         {
00687                 keycode = asc(keycode);
00688         }
00689                 
00690         static var keys_stati = vector(255);
00691         if (KeyPressed(keycode))
00692         {       
00693                 if (keys_stati[keycode] == 0.0)
00694                 {
00695                         keys_stati[keycode] = 1.0;
00696                         return 1;
00697                 }
00698         }
00699         else
00700         {
00701                 keys_stati[keycode] = 0.0;
00702                 return 0;
00703         }
00704         return -1;              
00705 }
00706 
00707 function OnTimer()
00708 {
00709         /*for (var i=0;i<Avatars.numCharacters();i++)
00710         {
00711         if(animationPlaying == true)
00712         {
00713                 var animationId = Avatars.GetAnimationId(my_avatarId,"m016Standing");
00714                 //blendCycle (int modelnum, int aninum, float weight, float delay)
00715                 Avatars.blendCycle(my_avatarId, animationId, 0.0, 1.0); 
00716                 animationPlaying = false;
00717         }
00718         else
00719         {
00720                 var animationId = Avatars.GetAnimationId(i,"m016Standing");
00721                 //blendCycle (int modelnum, int aninum, float weight, float delay)
00722                 Avatars.blendCycle(i, animationId, 1.0, 1.0);
00723                 //animationPlaying = true;              
00724         //}
00725         }*/
00726 }
00727 
00728 function OnEvent()
00729 {
00730 }
00731 
00732 function OnError()
00733 {
00734 }
 All Classes Files Functions Variables Enumerations Enumerator Defines