llviewerjoystick.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 #include "llviewercontrol.h"
00034 #include "llviewerwindow.h"
00035 #include "llviewercamera.h"
00036 #include "llviewerjoystick.h"
00037 #include "viewer.h"
00038 #include "llkeyboard.h"
00039 
00040 static LLQuaternion sFlycamRotation;
00041 static LLVector3 sFlycamPosition;
00042 static F32              sFlycamZoom;
00043 
00044 BOOL  LLViewerJoystick::sOverrideCamera = FALSE;
00045 
00046 void LLViewerJoystick::updateCamera(BOOL reset)
00047 {
00048         static F32 last_delta[] = {0,0,0,0,0,0,0};
00049         static F32 delta[] = { 0,0,0,0,0,0,0 };
00050 
00051         LLWindow* window = gViewerWindow->getWindow();
00052 
00053         F32 time = gFrameIntervalSeconds;
00054 
00055         S32 axis[] = 
00056         {
00057                 gSavedSettings.getS32("FlycamAxis0"),
00058                 gSavedSettings.getS32("FlycamAxis1"),
00059                 gSavedSettings.getS32("FlycamAxis2"),
00060                 gSavedSettings.getS32("FlycamAxis3"),
00061                 gSavedSettings.getS32("FlycamAxis4"),
00062                 gSavedSettings.getS32("FlycamAxis5"),
00063                 gSavedSettings.getS32("FlycamAxis6")
00064         };
00065 
00066         F32 axis_scale[] =
00067         {
00068                 gSavedSettings.getF32("FlycamAxisScale0"),
00069                 gSavedSettings.getF32("FlycamAxisScale1"),
00070                 gSavedSettings.getF32("FlycamAxisScale2"),
00071                 gSavedSettings.getF32("FlycamAxisScale3"),
00072                 gSavedSettings.getF32("FlycamAxisScale4"),
00073                 gSavedSettings.getF32("FlycamAxisScale5"),
00074                 gSavedSettings.getF32("FlycamAxisScale6")
00075         };
00076 
00077         F32 dead_zone[] =
00078         {
00079                 gSavedSettings.getF32("FlycamAxisDeadZone0"),
00080                 gSavedSettings.getF32("FlycamAxisDeadZone1"),
00081                 gSavedSettings.getF32("FlycamAxisDeadZone2"),
00082                 gSavedSettings.getF32("FlycamAxisDeadZone3"),
00083                 gSavedSettings.getF32("FlycamAxisDeadZone4"),
00084                 gSavedSettings.getF32("FlycamAxisDeadZone5"),
00085                 gSavedSettings.getF32("FlycamAxisDeadZone6")
00086         };
00087 
00088         if (reset)
00089         {
00090                 sFlycamPosition = gCamera->getOrigin();
00091                 sFlycamRotation = gCamera->getQuaternion();
00092                 sFlycamZoom = gCamera->getView();
00093 
00094                 for (U32 i = 0; i < 7; i++)
00095                 {
00096                         last_delta[i] = -window->getJoystickAxis(axis[i]);
00097                         delta[i] = 0.f;
00098                 }
00099                 return;
00100         }
00101 
00102         F32 cur_delta[7];
00103         F32 feather = gSavedSettings.getF32("FlycamFeathering");
00104         BOOL absolute = gSavedSettings.getBOOL("FlycamAbsolute");
00105 
00106         for (U32 i = 0; i < 7; i++)
00107         {
00108                 cur_delta[i] = -window->getJoystickAxis(axis[i]);       
00109                 F32 tmp = cur_delta[i];
00110                 if (absolute)
00111                 {
00112                         cur_delta[i] = cur_delta[i] - last_delta[i];
00113                 }
00114                 last_delta[i] = tmp;
00115 
00116                 if (cur_delta[i] > 0)
00117                 {
00118                         cur_delta[i] = llmax(cur_delta[i]-dead_zone[i], 0.f);
00119                 }
00120                 else
00121                 {
00122                         cur_delta[i] = llmin(cur_delta[i]+dead_zone[i], 0.f);
00123                 }
00124                 cur_delta[i] *= axis_scale[i];
00125                 
00126                 if (!absolute)
00127                 {
00128                         cur_delta[i] *= time;
00129                 }
00130 
00131                 delta[i] = delta[i] + (cur_delta[i]-delta[i])*time*feather;
00132         }
00133         
00134         sFlycamPosition += LLVector3(delta) * sFlycamRotation;
00135 
00136         LLMatrix3 rot_mat(delta[3],
00137                                           delta[4],
00138                                           delta[5]);
00139         
00140         sFlycamRotation = LLQuaternion(rot_mat)*sFlycamRotation;
00141 
00142         if (gSavedSettings.getBOOL("FlycamAutoLeveling"))
00143         {
00144                 LLMatrix3 level(sFlycamRotation);
00145 
00146                 LLVector3 x = LLVector3(level.mMatrix[0]);
00147                 LLVector3 y = LLVector3(level.mMatrix[1]);
00148                 LLVector3 z = LLVector3(level.mMatrix[2]);
00149 
00150                 y.mV[2] = 0.f;
00151                 y.normVec();
00152 
00153                 level.setRows(x,y,z);
00154                 level.orthogonalize();
00155                                 
00156                 LLQuaternion quat = LLQuaternion(level);
00157                 sFlycamRotation = nlerp(llmin(feather*time,1.f), sFlycamRotation, quat);
00158         }
00159 
00160         if (gSavedSettings.getBOOL("FlycamZoomDirect"))
00161         {
00162                 sFlycamZoom = last_delta[6]*axis_scale[6]+dead_zone[6];
00163         }
00164         else
00165         {
00166                 sFlycamZoom += delta[6];
00167         }
00168 
00169         LLMatrix3 mat(sFlycamRotation);
00170 
00171         gCamera->setView(sFlycamZoom);
00172         gCamera->setOrigin(sFlycamPosition);
00173         gCamera->mXAxis = LLVector3(mat.mMatrix[0]);
00174         gCamera->mYAxis = LLVector3(mat.mMatrix[1]);
00175         gCamera->mZAxis = LLVector3(mat.mMatrix[2]);
00176 }
00177 
00178 
00179 void LLViewerJoystick::scanJoystick()
00180 {
00181         if (!sOverrideCamera)
00182         {
00183                 static U32 joystick_state = 0;
00184                 static U32 button_state = 0;
00185 
00186                 F32 xval = gViewerWindow->getWindow()->getJoystickAxis(0);
00187                 F32 yval = gViewerWindow->getWindow()->getJoystickAxis(1);
00188 
00189                 if (xval <= -0.5f)
00190                 {
00191                         if (!(joystick_state & 0x1))
00192                         {
00193                                 gKeyboard->handleTranslatedKeyDown(KEY_PAD_LEFT, 0);
00194                                 joystick_state |= 0x1;
00195                         }
00196                 }
00197                 else 
00198                 {
00199                         if (joystick_state & 0x1)
00200                         {
00201                                 gKeyboard->handleTranslatedKeyUp(KEY_PAD_LEFT, 0);
00202                                 joystick_state &= ~0x1;
00203                         }
00204                 }
00205                 if (xval >= 0.5f)
00206                 {
00207                         if (!(joystick_state & 0x2))
00208                         {
00209                                 gKeyboard->handleTranslatedKeyDown(KEY_PAD_RIGHT, 0);
00210                                 joystick_state |= 0x2;
00211                         }
00212                 }
00213                 else 
00214                 {
00215                         if (joystick_state & 0x2)
00216                         {
00217                                 gKeyboard->handleTranslatedKeyUp(KEY_PAD_RIGHT, 0);
00218                                 joystick_state &= ~0x2;
00219                         }
00220                 }
00221                 if (yval <= -0.5f)
00222                 {
00223                         if (!(joystick_state & 0x4))
00224                         {
00225                                 gKeyboard->handleTranslatedKeyDown(KEY_PAD_UP, 0);
00226                                 joystick_state |= 0x4;
00227                         }
00228                 }
00229                 else 
00230                 {
00231                         if (joystick_state & 0x4)
00232                         {
00233                                 gKeyboard->handleTranslatedKeyUp(KEY_PAD_UP, 0);
00234                                 joystick_state &= ~0x4;
00235                         }
00236                 }
00237                 if (yval >=  0.5f)
00238                 {
00239                         if (!(joystick_state & 0x8))
00240                         {
00241                                 gKeyboard->handleTranslatedKeyDown(KEY_PAD_DOWN, 0);
00242                                 joystick_state |= 0x8;
00243                         }
00244                 }
00245                 else 
00246                 {
00247                         if (joystick_state & 0x8)
00248                         {
00249                                 gKeyboard->handleTranslatedKeyUp(KEY_PAD_DOWN, 0);
00250                                 joystick_state &= ~0x8;
00251                         }
00252                 }
00253 
00254                 for( int i = 0; i < 15; i++ )
00255                 {
00256                         if ( gViewerWindow->getWindow()->getJoystickButton(i) & 0x80 )
00257                         {
00258                                 if (!(button_state & (1<<i)))
00259                                 {
00260                                         gKeyboard->handleTranslatedKeyDown(KEY_BUTTON1+i, 0);
00261                                         button_state |= (1<<i);
00262                                 }
00263                         }
00264                         else
00265                         {
00266                                 if (button_state & (1<<i))
00267                                 {
00268                                         gKeyboard->handleTranslatedKeyUp(KEY_BUTTON1+i, 0);
00269                                         button_state &= ~(1<<i);
00270                                 }
00271                         }
00272                 }
00273         }
00274 }
00275 

Generated on Thu Jul 1 06:09:29 2010 for Second Life Viewer by  doxygen 1.4.7