//从四元数设置旋转矩阵 public static void Matrix3fSetRotationFromQuat4f(ref Matrix3fT NewObj, Tuple4fT q1) { float n, s; float xs, ys, zs; float wx, wy, wz; float xx, xy, xz; float yy, yz, zz; n = (q1.s.X * q1.s.X) + (q1.s.Y * q1.s.Y) + (q1.s.Z * q1.s.Z) + (q1.s.W * q1.s.W); s = (n > 0.0f) ? (2.0f / n) : 0.0f; xs = q1.s.X * s; ys = q1.s.Y * s; zs = q1.s.Z * s; wx = q1.s.W * xs; wy = q1.s.W * ys; wz = q1.s.W * zs; xx = q1.s.X * xs; xy = q1.s.X * ys; xz = q1.s.X * zs; yy = q1.s.Y * ys; yz = q1.s.Y * zs; zz = q1.s.Z * zs; NewObj.s.XX = 1.0f - (yy + zz); NewObj.s.YX = xy - wz; NewObj.s.ZX = xz + wy; NewObj.s.XY = xy + wz; NewObj.s.YY = 1.0f - (xx + zz); NewObj.s.ZY = yz - wx; NewObj.s.XZ = xz - wy; NewObj.s.YZ = yz + wx; NewObj.s.ZZ = 1.0f - (xx + yy); }
//更新鼠标状态 public void upstate() { if (!this.isZooming && this.isRClicked) { // 开始拖动 this.isZooming = true; // 设置拖动为变量为true this.LastPt = this.MousePt; this.lastZoomRate = this.zoomRate; } else if (this.isZooming) {//正在拖动 if (this.isRClicked) { //拖动 CMatrixMath.Point2fSub(ref MousePt, LastPt); this.zoomRate = this.lastZoomRate + this.MousePt.s.X * this.AdjustWidth * 2; } else { //停止拖动 this.isZooming = false; } } else if (!this.isDragging && this.isClicked) { // 如果没有拖动 this.isDragging = true; // 设置拖动为变量为true this.LastRot = this.ThisRot; this.click(this.MousePt); } else if (this.isDragging) { if (this.isClicked) { //如果按住拖动 Tuple4fT ThisQuat = new Tuple4fT(); this.drag(MousePt, ref ThisQuat); // 更新轨迹球的变量 CMatrixMath.Matrix3fSetRotationFromQuat4f(ref ThisRot, ThisQuat); // 计算旋转量 CMatrixMath.Matrix3fMulMatrix3f(ref this.ThisRot, this.LastRot); CMatrixMath.Matrix4fSetRotationFromMatrix3f(ref this.Transform, this.ThisRot); } else // 如果放开鼠标,设置拖动为false this.isDragging = false; } }
//鼠标拖动计算旋转 public void drag(Tuple2fT NewPt, ref Tuple4fT NewRot) { //新的位置 _mapToSphere(NewPt, ref EnVec); //计算旋转 //if (NewRot) { Tuple3fT Perp = new Tuple3fT(); //计算旋转轴 CMatrixMath.Vector3fCross(ref Perp, StVec, EnVec); //如果不为0 if (CMatrixMath.Vector3fLength(Perp) > CMatrixMath.Epsilon) { //记录旋转轴 NewRot.s.X = Perp.s.X; NewRot.s.Y = Perp.s.Y; NewRot.s.Z = Perp.s.Z; //在四元数中,w=cos(a/2),a为旋转的角度 NewRot.s.W = CMatrixMath.Vector3fDot(StVec, EnVec); } //是0,说明没有旋转 else { NewRot.s.X = NewRot.s.Y = NewRot.s.Z = NewRot.s.W = 0.0f; } } }
public override bool OnTouchEvent(MotionEvent evt) { if (gMitcRender.TransState || gMitcRender.RotAState) { if (evt.Action == MotionEventActions.Down) { m_lastPos.Set(evt.GetX(), evt.GetY()); mMitcRender.GetWorldPoint(m_lastPos.X, m_lastPos.Y, ref m_lastwx, ref m_lastwy, ref m_lastwz); m_curPos.X = m_lastPos.X; m_curPos.Y = m_lastPos.Y; m_curwx = m_lastwx; m_curwy = m_lastwy; m_curwz = m_lastwz; Transing = true; //gMitcRender.updateMatrix(); if (gMitcRender.RotAState) { Tuple2fT MousePt = new Tuple2fT(); MousePt.s.X = evt.GetX(); MousePt.s.Y = evt.GetY(); isDragging = true; // string strWp = String.Format("Down ThisRot Rot:({0},{1},{2})", ThisRot.s.M00, ThisRot.s.M11, ThisRot.s.M22); // Log.Debug(TAG, strWp); LastRot = (Matrix3fT)ThisRot.Clone(); // strWp = String.Format("After Down LastRot Rot:({0},{1},{2})", LastRot.s.M00, LastRot.s.M11, LastRot.s.M22); // Log.Debug(TAG, strWp); m_arcball.click(MousePt); } } else if (evt.Action == MotionEventActions.Up) { Transing = false; if (gMitcRender.RotAState) { isDragging = false; } } else if (evt.Action == MotionEventActions.Move) { m_curPos.Set(evt.GetX(), evt.GetY()); mMitcRender.GetWorldPoint(m_curPos.X, m_curPos.Y, ref m_curwx, ref m_curwy, ref m_curwz); mMitcRender.buildTransform((m_curwx - m_lastwx), (m_curwy - m_lastwy), m_curwz - m_lastwz); m_lastPos.X = m_curPos.X; m_lastPos.Y = m_curPos.Y; m_lastwx = m_curwx; m_lastwy = m_curwy; m_lastwz = m_curwz; string strWp = String.Format("1.Move This Point:({0},{1},{2})", m_curwx, m_curwy, m_curwz); Log.Debug(TAG, strWp); if (gMitcRender.RotAState && isDragging) { Tuple4fT ThisQuat = new Tuple4fT(); Tuple2fT MousePt = new Tuple2fT(); MousePt.s.X = evt.GetX(); MousePt.s.Y = evt.GetY(); m_arcball.drag(MousePt, ref ThisQuat); //string strWp = String.Format("1.Move This Rot:({0},{1},{2})", ThisRot.s.M00, ThisRot.s.M11, ThisRot.s.M22); //Log.Debug(TAG, strWp); CMatrixMath.Matrix3fSetRotationFromQuat4f(ref ThisRot, ThisQuat); //strWp = String.Format("2.Move This Rot:({0},{1},{2})", ThisRot.s.M00, ThisRot.s.M11, ThisRot.s.M22); //Log.Debug(TAG, strWp); CMatrixMath.Matrix3fMulMatrix3f(ref ThisRot, LastRot); //strWp = String.Format("3.Move This Rot:({0},{1},{2})", ThisRot.s.M00, ThisRot.s.M11, ThisRot.s.M22); //Log.Debug(TAG, strWp); //strWp = String.Format("Move Last Rot:({0},{1},{2})", LastRot.s.M00, LastRot.s.M11, LastRot.s.M22); //Log.Debug(TAG, strWp); Matrix4fT _tempTransform = mMitcRender.Transform; CMatrixMath.Matrix4fSetRotationFromMatrix3f(ref _tempTransform, ThisRot); m_arcball.upstate(); } this.RequestRender(); } return true; } else if (gMitcRender.ScaleState) { int nCnt = evt.PointerCount; if ((evt.Action & MotionEventActions.Mask) == MotionEventActions.PointerDown && 2 == nCnt)//<span style="color:#ff0000;">2��ʾ������ָ</span> { m_lastPos.Set(evt.GetX(evt.GetPointerId(0)), evt.GetY(evt.GetPointerId(0))); m_curPos.Set(evt.GetX(evt.GetPointerId(nCnt - 1)), evt.GetY(evt.GetPointerId(nCnt - 1))); string strWp = String.Format("2 Down Point:({0}),({1})", m_lastPos.ToString(), m_curPos.ToString()); Log.Debug(TAG, strWp); mOldCounts = 2; mScaleFactor = 1.0f; //gMitcRender.updateMatrix(); } else if ((evt.Action & MotionEventActions.Mask) == MotionEventActions.PointerUp && 2 == nCnt) { m_lastPos.Set(evt.GetX(evt.GetPointerId(0)), evt.GetY(evt.GetPointerId(0))); m_curPos.Set(evt.GetX(evt.GetPointerId(nCnt - 1)), evt.GetY(evt.GetPointerId(nCnt - 1))); string strWp = String.Format("2 Up Point:({0}),({1})", m_lastPos.ToString(), m_curPos.ToString()); Log.Debug(TAG, strWp); mOldCounts = 0; mScaleFactor = 1.0f; } else if (evt.Action == MotionEventActions.Move && 2 == nCnt) { float lastlength = GetLength(m_lastPos, m_curPos); m_lastPos.Set(evt.GetX(evt.GetPointerId(0)), evt.GetY(evt.GetPointerId(0))); m_curPos.Set(evt.GetX(evt.GetPointerId(nCnt - 1)), evt.GetY(evt.GetPointerId(nCnt - 1))); float curlength = GetLength(m_lastPos, m_curPos); mScaleFactor = Math.Abs(lastlength) < 1.0e-6 ? 1.0f : (curlength / lastlength); mMitcRender.buildScale(mScaleFactor); this.RequestRender(); string strWp = String.Format("2 Move Point:({0}),({1})", m_lastPos.ToString(), m_curPos.ToString()); Log.Debug(TAG, strWp); } } return true; }