void GutUserControl_RotateObject(Matrix4x4 object_matrix, float time_advance) { // 讀取滑鼠 GutMouseInfo mouse; if ( GutReadMouse(&mouse) ) { float rotation_speed = 1.0 * time_advance; // 如果按下滑鼠左鍵,就旋轉鏡頭 if ( mouse.button[0] ) { Matrix4x4 rotate_matrix; rotate_matrix.RotateY_Replace(mouse.x * rotation_speed); rotate_matrix.RotateX(mouse.y * rotation_speed); object_matrix = object_matrix * rotate_matrix; } // 用滾輪來縮放物件 if ( mouse.z ) { float scale = 1.0f + mouse.z / 1000.0f; object_matrix.Scale(scale, scale, scale); } } }
void __GetUserInput() { GutMouseInfo mouse; GutReadMouse(&mouse); if (mouse.button[0]) { if (!m_bIsTouch) { __track_down(mouse); }else { __track_move(mouse); } }else { if (m_bIsTouch) { __track_up(mouse); } } }
void GetUserInput(void) { // 讀取滑鼠 GutMouseInfo mouse; if ( GutReadMouse(&mouse) ) { // 取得畫完前一個畫面到現在所經歷的時間 g_fFrame_Time = g_Timer.Stop(); g_Timer.Restart(); float moving_speed = 2.0f * g_fFrame_Time; float rotation_speed = 1.0 * g_fFrame_Time; // 如果按下滑鼠左鍵,就旋轉鏡頭 if ( mouse.button[0] ) { Matrix4x4 rotate_matrix; rotate_matrix.RotateY_Replace(mouse.x * rotation_speed); rotate_matrix.RotateX(mouse.y * rotation_speed); g_world_matrix = g_world_matrix * rotate_matrix; } } }
void CGutUserControl::RotateObject(float time_advance) { static float rx=0, ry=0; // 讀取滑鼠 GutMouseInfo mouse; if ( GutReadMouse(&mouse) ) { float rotation_speed = m_fRotateSpeed * time_advance; m_eUP=UP_CUSTOMIZED; // 如果按下滑鼠左鍵,就旋轉鏡頭 if ( mouse.button[0] ) { Matrix4x4 rotate_matrix; rotate_matrix.Identity(); rotate_matrix.Rotate(m_CameraMatrix[1], mouse.x * rotation_speed); rotate_matrix.Rotate(m_CameraMatrix[0], mouse.y * rotation_speed); Vector4 vPos = m_ObjectMatrix[3]; m_ObjectMatrix.NoTranslate(); m_ObjectMatrix = m_ObjectMatrix * rotate_matrix; m_ObjectMatrix[3] = vPos; } // 用滾輪來縮放物件 if ( mouse.z ) { float scale = 1.0f + mouse.z / 1000.0f; m_ObjectMatrix.Scale(scale, scale, scale); } } }
void GetUserInput(void) { // `讀取滑鼠` GutMouseInfo mouse; GutReadMouse(&mouse); // `讀取鍵盤` char keyboard_state[256]; GutReadKeyboard(keyboard_state); // `取得畫完前一個畫面到現在所經歷的時間` float time_diff = g_Timer.Stop(); g_Timer.Restart(); float moving_speed = 2.0f * time_diff; float rotation_speed = 1.0 * time_diff; // `極座標系統` static float theta = -MATH_PI * 0.5f; static float phi = 0.0f; // `如果按下滑鼠左鍵, 就旋轉鏡頭.` if ( mouse.button[0] ) { theta += mouse.x * rotation_speed; phi -= mouse.y * rotation_speed; } float sin_phi, cos_phi; float sin_theta, cos_theta; FastMath::SinCos(phi, sin_phi, cos_phi); FastMath::SinCos(theta, sin_theta, cos_theta); // `計算鏡頭的面向` Vector4 camera_facing; camera_facing[0] = cos_phi * cos_theta; camera_facing[1] = sin_phi; camera_facing[2] = cos_phi * sin_theta; // `計算鏡頭正上方的軸向` Vector4 camera_up; FastMath::SinCos(phi + MATH_PI*0.5f, sin_phi, cos_phi); camera_up[0] = cos_phi * cos_theta; camera_up[1] = sin_phi; camera_up[2] = cos_phi * sin_theta; // `取得鏡面右方的方向` Vector4 camera_right = Vector3CrossProduct(camera_facing, camera_up); // `按下W或方向鍵向上` if ( keyboard_state[GUTKEY_W] || keyboard_state[GUTKEY_UP] ) { g_eye += camera_facing * moving_speed; } // `按下S或方向鍵向下` if ( keyboard_state[GUTKEY_S] || keyboard_state[GUTKEY_DOWN] ) { g_eye -= camera_facing * moving_speed; } // `按下A或方向鍵向左` if ( keyboard_state[GUTKEY_A] || keyboard_state[GUTKEY_LEFT] ) { g_eye -= camera_right * moving_speed; } // `按下D或方向鍵向右` if ( keyboard_state[GUTKEY_D] || keyboard_state[GUTKEY_RIGHT] ) { g_eye += camera_right * moving_speed; } // `計算出鏡頭對準的點, 產生鏡頭轉換矩陣時會用到.` g_lookat = g_eye + camera_facing; // `因為是對2個軸轉動, 需要更新鏡頭朝上的軸.` g_up = camera_up; }
void CGutUserControl::FPSCamera(float time_advance) { char keyboard_state[256]; float moving_speed = m_fMoveSpeed * time_advance; float rotation_speed = m_fRotateSpeed * time_advance; if ( time_advance ) { // 讀取滑鼠 GutMouseInfo mouse; GutReadMouse(&mouse); // 讀取鍵盤 GutReadKeyboard(keyboard_state); // 速度 // 如果按下滑鼠左鍵,就旋轉鏡頭 if ( mouse.button[0] ) { m_fCameraHeading -= mouse.x * rotation_speed; m_fCameraPitch -= mouse.y * rotation_speed; } } /* // 極座標系統 float heading_sin, heading_cos; float pitch_sin, pitch_cos; float up_sin, up_cos; FastMath::SinCos(m_fCameraHeading, heading_sin, heading_cos); FastMath::SinCos(m_fCameraPitch, pitch_sin, pitch_cos); FastMath::SinCos(m_fCameraPitch + MATH_PI*0.5f, up_sin, up_cos); Vector4 camera_facing, camera_up; switch(m_eUP) { case UP_X: break; case UP_Y: // 計算鏡頭的面向 camera_facing[0] = pitch_cos * heading_cos; camera_facing[1] = pitch_sin; camera_facing[2] = pitch_cos * heading_sin; // 計算鏡頭正上方的軸向 camera_up[0] = up_cos * heading_cos; camera_up[1] = up_sin; camera_up[2] = up_cos * heading_sin; break; case UP_Z: // 計算鏡頭的面向 camera_facing[0] = pitch_cos * heading_cos; camera_facing[2] = pitch_sin; camera_facing[1] = -pitch_cos * heading_sin; // 計算鏡頭正上方的軸向 camera_up[0] = up_cos * heading_cos; camera_up[2] = up_sin; camera_up[1] = -up_cos * heading_sin; break; } // 取得鏡面右方的方向 Vector4 camera_right = Vector3CrossProduct(camera_up, camera_facing); */ Matrix4x4 rot_matrix; rot_matrix.Identity(); rot_matrix.RotateY_Replace(m_fCameraHeading); rot_matrix.RotateX(m_fCameraPitch); Matrix4x4 CameraOrient = rot_matrix * m_RefCameraMatrix; Vector4 camera_right = CameraOrient[0]; Vector4 camera_up = CameraOrient[1]; Vector4 camera_facing = -CameraOrient[2]; if ( time_advance ) { // 按下W或方向鍵向上 if ( keyboard_state[GUTKEY_W] || keyboard_state[GUTKEY_UP] ) { m_vEye += camera_facing * moving_speed; } // 按下S或方向鍵向下 if ( keyboard_state[GUTKEY_S] || keyboard_state[GUTKEY_DOWN] ) { m_vEye -= camera_facing * moving_speed; } // 按下A或方向鍵向左 if ( keyboard_state[GUTKEY_A] || keyboard_state[GUTKEY_LEFT] ) { m_vEye -= camera_right * moving_speed; } // 按下D或方向鍵向右 if ( keyboard_state[GUTKEY_D] || keyboard_state[GUTKEY_RIGHT] ) { m_vEye += camera_right * moving_speed; } } // 計算出鏡頭對準的點, 產生鏡頭轉換矩陣時會用到. m_vLookAt = m_vEye + camera_facing; // 因為是對2個軸轉動, 需要更新鏡頭朝上的軸 m_vUp = camera_up; // 確認w = 1 m_vEye[3] = m_vUp[3] = m_vLookAt[3] = 1.0f; UpdateViewMatrix(); }
void GetUserControl_FPSCamera(Vector4 &vEye, Vector4 &vUp, Vector4 &vLookAt, float time_advance) { // 讀取滑鼠 GutMouseInfo mouse; GutReadMouse(&mouse); // 讀取鍵盤 char keyboard_state[256]; GutReadKeyboard(keyboard_state); float moving_speed = 2.0f * time_advance; float rotation_speed = 1.0f * time_advance; // 極座標系統 static float theta = -MATH_PI * 0.5f; static float phi = 0.0f; // 如果按下滑鼠左鍵,就旋轉鏡頭 if ( mouse.button[0] ) { theta += mouse.x * rotation_speed; phi -= mouse.y * rotation_speed; } float sin_phi, cos_phi; float sin_theta, cos_theta; FastMath::SinCos(phi, sin_phi, cos_phi); FastMath::SinCos(theta, sin_theta, cos_theta); // 計算鏡頭的面向 Vector4 camera_facing; camera_facing[0] = cos_phi * cos_theta; camera_facing[1] = sin_phi; camera_facing[2] = cos_phi * sin_theta; // 計算鏡頭正上方的軸向 Vector4 camera_up; FastMath::SinCos(phi + MATH_PI*0.5f, sin_phi, cos_phi); camera_up[0] = cos_phi * cos_theta; camera_up[1] = sin_phi; camera_up[2] = cos_phi * sin_theta; // 取得鏡面右方的方向 Vector4 camera_right = Vector3CrossProduct(camera_up, camera_facing); // 按下W或方向鍵向上 if ( keyboard_state[GUTKEY_W] || keyboard_state[GUTKEY_UP] ) { vEye += camera_facing * moving_speed; } // 按下S或方向鍵向下 if ( keyboard_state[GUTKEY_S] || keyboard_state[GUTKEY_DOWN] ) { vEye -= camera_facing * moving_speed; } // 按下A或方向鍵向左 if ( keyboard_state[GUTKEY_A] || keyboard_state[GUTKEY_LEFT] ) { vEye += camera_right * moving_speed; } // 按下D或方向鍵向右 if ( keyboard_state[GUTKEY_D] || keyboard_state[GUTKEY_RIGHT] ) { vEye -= camera_right * moving_speed; } // 計算出鏡頭對準的點, 產生鏡頭轉換矩陣時會用到. vLookAt = vEye + camera_facing; // 因為是對2個軸轉動, 需要更新鏡頭朝上的軸 vUp = camera_up; vEye[3] = vUp[3] = vLookAt[3] = 1.0f; }