public static XMVector RotationRollPitchYawFromVector(XMVector angles) { XMVector sign = XMVector.FromFloat(1.0f, -1.0f, -1.0f, 1.0f); XMVector halfAngles = XMVector.Multiply(angles, XMGlobalConstants.OneHalf); XMVector sinAngles; XMVector cosAngles; halfAngles.SinCos(out sinAngles, out cosAngles); XMVector p0 = new XMVector(sinAngles.X, cosAngles.X, cosAngles.X, cosAngles.X); XMVector y0 = new XMVector(cosAngles.Y, sinAngles.Y, cosAngles.Y, cosAngles.Y); XMVector r0 = new XMVector(cosAngles.Z, cosAngles.Z, sinAngles.Z, cosAngles.Z); XMVector p1 = new XMVector(cosAngles.X, sinAngles.X, sinAngles.X, sinAngles.X); XMVector y1 = new XMVector(sinAngles.Y, cosAngles.Y, sinAngles.Y, sinAngles.Y); XMVector r1 = new XMVector(sinAngles.Z, sinAngles.Z, cosAngles.Z, sinAngles.Z); XMVector q1 = XMVector.Multiply(p1, sign); XMVector q0 = XMVector.Multiply(p0, y0); q1 = XMVector.Multiply(q1, y1); q0 = XMVector.Multiply(q0, r0); return(XMVector.MultiplyAdd(q1, r1, q0)); }
public virtual void SetViewParams(XMVector vEyePt, XMVector vLookatPt) { m_vEye = vEyePt; m_vDefaultEye = vEyePt; m_vLookAt = vLookatPt; m_vDefaultLookAt = vLookatPt; // Calc the view matrix XMMatrix mView = XMMatrix.LookAtLH(vEyePt, vLookatPt, XMVector.FromFloat(0.0f, 1.0f, 0.0f, 0.0f)); m_mView = mView; XMMatrix mInvView = mView.Inverse(); // The axis basis vectors and camera position are stored inside the // position matrix in the 4 rows of the camera's world matrix. // To figure out the yaw/pitch of the camera, we just need the Z basis vector XMFloat3 zBasis = new XMFloat3(mInvView.M31, mInvView.M32, mInvView.M33); m_fCameraYawAngle = (float)Math.Atan2(zBasis.X, zBasis.Z); float fLen = (float)Math.Sqrt(zBasis.Z * zBasis.Z + zBasis.X * zBasis.X); m_fCameraPitchAngle = -(float)Math.Atan2(zBasis.Y, fLen); }
public static XMVector Unproject( XMVector v, float viewportX, float viewportY, float viewportWidth, float viewportHeight, float viewportMinZ, float viewportMaxZ, XMMatrix projection, XMMatrix view, XMMatrix world) { XMVector d = XMVector.FromFloat(-1.0f, 1.0f, 0.0f, 0.0f); XMVector scale = new XMVector(viewportWidth * 0.5f, -viewportHeight * 0.5f, viewportMaxZ - viewportMinZ, 1.0f); scale = scale.Reciprocal(); XMVector offset = new XMVector(-viewportX, -viewportY, -viewportMinZ, 0.0f); offset = XMVector.MultiplyAdd(scale, offset, d); XMMatrix transform = XMMatrix.Multiply(world, view); transform = XMMatrix.Multiply(transform, projection); transform = transform.Inverse(); XMVector result = XMVector.MultiplyAdd(v, scale, offset); return(XMVector3.TransformCoord(result, transform)); }
public static XMVector YuvToRgbHD(XMVector yuv) { XMVector scale1 = XMVector.FromFloat(0.0f, -0.2153f, 2.1324f, 0.0f); XMVector scale2 = XMVector.FromFloat(1.2803f, -0.3806f, 0.0f, 0.0f); XMMatrix m = new XMMatrix(XMGlobalConstants.One, scale1, scale2, XMGlobalConstants.Zero); XMVector clr = XMVector3.Transform(yuv, m); return(XMVector.Select(yuv, clr, XMGlobalConstants.Select1110)); }
public static XMVector YuvToRgb(XMVector yuv) { XMVector scale1 = XMVector.FromFloat(0.0f, -0.395f, 2.032f, 0.0f); XMVector scale2 = XMVector.FromFloat(1.140f, -0.581f, 0.0f, 0.0f); XMMatrix m = new XMMatrix(XMGlobalConstants.One, scale1, scale2, XMGlobalConstants.Zero); XMVector clr = XMVector3.Transform(yuv, m); return(XMVector.Select(yuv, clr, XMGlobalConstants.Select1110)); }
public static XMVector RgbToYuvHD(XMVector rgb) { XMVector scale0 = XMVector.FromFloat(0.2126f, -0.0997f, 0.6150f, 0.0f); XMVector scale1 = XMVector.FromFloat(0.7152f, -0.3354f, -0.5586f, 0.0f); XMVector scale2 = XMVector.FromFloat(0.0722f, 0.4351f, -0.0564f, 0.0f); XMMatrix m = new XMMatrix(scale0, scale1, scale2, XMGlobalConstants.Zero); XMVector clr = XMVector3.Transform(rgb, m); return(XMVector.Select(rgb, clr, XMGlobalConstants.Select1110)); }
public static XMVector RgbToYuv(XMVector rgb) { XMVector scale0 = XMVector.FromFloat(0.299f, -0.147f, 0.615f, 0.0f); XMVector scale1 = XMVector.FromFloat(0.587f, -0.289f, -0.515f, 0.0f); XMVector scale2 = XMVector.FromFloat(0.114f, 0.436f, -0.100f, 0.0f); XMMatrix m = new XMMatrix(scale0, scale1, scale2, XMGlobalConstants.Zero); XMVector clr = XMVector3.Transform(rgb, m); return(XMVector.Select(rgb, clr, XMGlobalConstants.Select1110)); }
public static XMVector XyzToRgb(XMVector xyz) { XMVector scale0 = XMVector.FromFloat(2.3706743f, -0.5138850f, 0.0052982f, 0.0f); XMVector scale1 = XMVector.FromFloat(-0.9000405f, 1.4253036f, -0.0146949f, 0.0f); XMVector scale2 = XMVector.FromFloat(-0.4706338f, 0.0885814f, 1.0093968f, 0.0f); XMVector scale = XMVector.FromFloat(0.17697f, 0.17697f, 0.17697f, 0.0f); XMMatrix m = new XMMatrix(scale0, scale1, scale2, XMGlobalConstants.Zero); XMVector clr = XMVector3.Transform(XMVector.Multiply(xyz, scale), m); return(XMVector.Select(xyz, clr, XMGlobalConstants.Select1110)); }
public static XMVector RgbToXyz(XMVector rgb) { XMVector scale0 = XMVector.FromFloat(0.4887180f, 0.1762044f, 0.0000000f, 0.0f); XMVector scale1 = XMVector.FromFloat(0.3106803f, 0.8129847f, 0.0102048f, 0.0f); XMVector scale2 = XMVector.FromFloat(0.2006017f, 0.0108109f, 0.9897952f, 0.0f); XMVector scale = XMVector.FromFloat(1.0f / 0.17697f, 1.0f / 0.17697f, 1.0f / 0.17697f, 0.0f); XMMatrix m = new XMMatrix(scale0, scale1, scale2, XMGlobalConstants.Zero); XMVector clr = XMVector.Multiply(XMVector3.Transform(rgb, m), scale); return(XMVector.Select(rgb, clr, XMGlobalConstants.Select1110)); }
public static XMVector SlerpV(XMVector q0, XMVector q1, XMVector t) { Debug.Assert(t.Y == t.X && t.Z == t.X && t.W == t.X, "Reviewed"); //// Result = Q0 * sin((1.0 - t) * Omega) / sin(Omega) + Q1 * sin(t * Omega) / sin(Omega) XMVector oneMinusEpsilon = XMVector.FromFloat(1.0f - 0.00001f, 1.0f - 0.00001f, 1.0f - 0.00001f, 1.0f - 0.00001f); XMVector cosOmega = XMQuaternion.Dot(q0, q1); XMVector zero = XMVector.Zero; XMVector control = XMVector.Less(cosOmega, zero); XMVector sign = XMVector.Select(XMGlobalConstants.One, XMGlobalConstants.NegativeOne, control); cosOmega = XMVector.Multiply(cosOmega, sign); control = XMVector.Less(cosOmega, oneMinusEpsilon); XMVector sinOmega = XMVector .NegativeMultiplySubtract(cosOmega, cosOmega, XMGlobalConstants.One) .Sqrt(); XMVector omega = XMVector.ATan2(sinOmega, cosOmega); XMVector signMask = XMVector.SignMask; XMVector v01 = XMVector.ShiftLeft(t, zero, 2); signMask = XMVector.ShiftLeft(signMask, zero, 3); v01 = XMVector.XorInt(v01, signMask); v01 = XMVector.Add(XMGlobalConstants.IdentityR0, v01); XMVector invSinOmega = sinOmega.Reciprocal(); XMVector s0 = XMVector .Multiply(v01, omega) .Sin(); s0 = XMVector.Multiply(s0, invSinOmega); s0 = XMVector.Select(v01, s0, control); XMVector s1 = XMVector.SplatY(s0); s0 = XMVector.SplatX(s0); s1 = XMVector.Multiply(s1, sign); XMVector result = XMVector.Multiply(q0, s0); result = XMVector.MultiplyAdd(q1, s1, result); return(result); }
public static XMVector RgbToSrgb(XMVector rgb) { XMVector cutoff = XMVector.FromFloat(0.0031308f, 0.0031308f, 0.0031308f, 1.0f); XMVector linear = XMVector.FromFloat(12.92f, 12.92f, 12.92f, 1.0f); XMVector scale = XMVector.FromFloat(1.055f, 1.055f, 1.055f, 1.0f); XMVector bias = XMVector.FromFloat(0.055f, 0.055f, 0.055f, 0.0f); XMVector invGamma = XMVector.FromFloat(1.0f / 2.4f, 1.0f / 2.4f, 1.0f / 2.4f, 1.0f); XMVector v = rgb.Saturate(); XMVector v0 = XMVector.Multiply(v, linear); XMVector v1 = XMVector.Subtract(XMVector.Multiply(scale, XMVector.Pow(v, invGamma)), bias); XMVector select = XMVector.Less(v, cutoff); v = XMVector.Select(v1, v0, select); return(XMVector.Select(rgb, v, XMGlobalConstants.Select1110)); }
public static XMVector SrgbToRgb(XMVector srgb) { XMVector cutoff = XMVector.FromFloat(0.04045f, 0.04045f, 0.04045f, 1.0f); XMVector invLinear = XMVector.FromFloat(1.0f / 12.92f, 1.0f / 12.92f, 1.0f / 12.92f, 1.0f); XMVector scale = XMVector.FromFloat(1.0f / 1.055f, 1.0f / 1.055f, 1.0f / 1.055f, 1.0f); XMVector bias = XMVector.FromFloat(0.055f, 0.055f, 0.055f, 0.0f); XMVector gamma = XMVector.FromFloat(2.4f, 2.4f, 2.4f, 1.0f); XMVector v = srgb.Saturate(); XMVector v0 = XMVector.Multiply(v, invLinear); XMVector v1 = XMVector.Pow(XMVector.Multiply(XMVector.Add(v, bias), scale), gamma); XMVector select = XMVector.Greater(v, cutoff); v = XMVector.Select(v0, v1, select); return(XMVector.Select(srgb, v, XMGlobalConstants.Select1110)); }
public static XMVector Ln(XMVector q) { XMVector oneMinusEpsilon = XMVector.FromFloat(1.0f - 0.00001f, 1.0f - 0.00001f, 1.0f - 0.00001f, 1.0f - 0.00001f); XMVector qw = XMVector.SplatW(q); XMVector q0 = XMVector.Select(XMGlobalConstants.Select1110, q, XMGlobalConstants.Select1110); XMVector controlW = qw.InBounds(oneMinusEpsilon); XMVector theta = qw.ACos(); XMVector sinTheta = theta.Sin(); XMVector s = XMVector.Divide(theta, sinTheta); XMVector result = XMVector.Multiply(q0, s); result = XMVector.Select(q0, result, controlW); return(result); }
public override void SetViewParams(XMVector vEyePt, XMVector vLookatPt) { base.SetViewParams(vEyePt, vLookatPt); // Propogate changes to the member arcball XMMatrix mRotation = XMMatrix.LookAtLH(vEyePt, vLookatPt, XMVector.FromFloat(0.0f, 1.0f, 0.0f, 0.0f)); XMVector quat = XMQuaternion.RotationMatrix(mRotation); m_ViewArcBall.SetQuatNow(quat); // Set the radius according to the distance XMVector vEyeToPoint = XMVector.Subtract(vLookatPt, vEyePt); float len = XMVector3.Length(vEyeToPoint).X; SetRadius(len); // View information changed. FrameMove should be called. m_bDragSinceLastUpdate = true; }
private static XMVector HueToClr(XMVector p, XMVector q, XMVector h) { XMVector oneSixth = XMVector.FromFloat(1.0f / 6.0f, 1.0f / 6.0f, 1.0f / 6.0f, 1.0f / 6.0f); XMVector twoThirds = XMVector.FromFloat(2.0f / 3.0f, 2.0f / 3.0f, 2.0f / 3.0f, 2.0f / 3.0f); XMVector t = h; if (XMVector3.Less(t, XMGlobalConstants.Zero)) { t = XMVector.Add(t, XMGlobalConstants.One); } if (XMVector3.Greater(t, XMGlobalConstants.One)) { t = XMVector.Subtract(t, XMGlobalConstants.One); } if (XMVector3.Less(t, oneSixth)) { // p + (q - p) * 6 * t XMVector t1 = XMVector.Subtract(q, p); XMVector t2 = XMVector.Multiply(XMGlobalConstants.Six, t); return(XMVector.MultiplyAdd(t1, t2, p)); } if (XMVector3.Less(t, XMGlobalConstants.OneHalf)) { return(q); } if (XMVector3.Less(t, twoThirds)) { // p + (q - p) * 6 * (2/3 - t) XMVector t1 = XMVector.Subtract(q, p); XMVector t2 = XMVector.Multiply(XMGlobalConstants.Six, XMVector.Subtract(twoThirds, t)); return(XMVector.MultiplyAdd(t1, t2, p)); } return(p); }
public static XMVector XyzToSrgb(XMVector xyz) { XMVector scale0 = XMVector.FromFloat(3.2406f, -0.9689f, 0.0557f, 0.0f); XMVector scale1 = XMVector.FromFloat(-1.5372f, 1.8758f, -0.2040f, 0.0f); XMVector scale2 = XMVector.FromFloat(-0.4986f, 0.0415f, 1.0570f, 0.0f); XMVector cutoff = XMVector.FromFloat(0.0031308f, 0.0031308f, 0.0031308f, 0.0f); XMVector exp = XMVector.FromFloat(1.0f / 2.4f, 1.0f / 2.4f, 1.0f / 2.4f, 1.0f); XMMatrix m = new XMMatrix(scale0, scale1, scale2, XMGlobalConstants.Zero); XMVector lclr = XMVector3.Transform(xyz, m); XMVector sel = XMVector.Greater(lclr, cutoff); // clr = 12.92 * lclr for lclr <= 0.0031308f XMVector smallC = XMVector.Multiply(lclr, XMGlobalConstants.MsrgbScale); // clr = (1+a)*pow(lclr, 1/2.4) - a for lclr > 0.0031308 (where a = 0.055) XMVector largeC = XMVector.Subtract(XMVector.Multiply(XMGlobalConstants.MsrgbA1, XMVector.Pow(lclr, exp)), XMGlobalConstants.MsrgbA); XMVector clr = XMVector.Select(smallC, largeC, sel); return(XMVector.Select(xyz, clr, XMGlobalConstants.Select1110)); }
public static XMVector HslToRgb(XMVector hsl) { XMVector oneThird = XMVector.FromFloat(1.0f / 3.0f, 1.0f / 3.0f, 1.0f / 3.0f, 1.0f / 3.0f); XMVector s = XMVector.SplatY(hsl); XMVector l = XMVector.SplatZ(hsl); if (XMVector3.NearEqual(s, XMGlobalConstants.Zero, XMGlobalConstants.Epsilon)) { // Achromatic return(XMVector.Select(hsl, l, XMGlobalConstants.Select1110)); } else { XMVector h = XMVector.SplatX(hsl); XMVector q; if (XMVector3.Less(l, XMGlobalConstants.OneHalf)) { q = XMVector.Multiply(l, XMVector.Add(XMGlobalConstants.One, s)); } else { q = XMVector.Subtract(XMVector.Add(l, s), XMVector.Multiply(l, s)); } XMVector p = XMVector.Subtract(XMVector.Multiply(XMGlobalConstants.Two, l), q); XMVector r = XMColor.HueToClr(p, q, XMVector.Add(h, oneThird)); XMVector g = XMColor.HueToClr(p, q, h); XMVector b = XMColor.HueToClr(p, q, XMVector.Subtract(h, oneThird)); XMVector rg = XMVector.Select(g, r, XMGlobalConstants.Select1000); XMVector ba = XMVector.Select(hsl, b, XMGlobalConstants.Select1110); return(XMVector.Select(ba, rg, XMGlobalConstants.Select1100)); } }
public SdkBaseCamera() { m_isActive = true; m_fFramesToSmoothMouseData = 2.0f; m_fTotalDragTimeToZero = 0.25f; m_fNearPlane = 0.0f; m_fFarPlane = 1.0f; m_fRotationScaler = 0.01f; m_fMoveScaler = 5.0f; m_bEnablePositionMovement = true; m_bEnableYAxisMovement = true; m_vMinBoundary = new XMFloat3(-1.0f, -1.0f, -1.0f); m_vMaxBoundary = new XMFloat3(1.0f, 1.0f, 1.0f); SetViewParams(XMVector.Zero, XMVector.FromFloat(0.0f, 0.0f, 1.0f, 0.0f)); SetProjParams(XMMath.PIDivFour, 1.0f, 1.0f, 1000.0f); NativeMethods.GetCursorPos(out m_ptLastMousePosition); m_rcDrag = new XMInt4(int.MinValue, int.MinValue, int.MaxValue, int.MaxValue); }
private void UpdateLightDir() { XMMatrix mInvView = m_mView.Inverse(); mInvView.M41 = 0; mInvView.M42 = 0; mInvView.M43 = 0; XMMatrix mLastRotInv = m_mRotSnapshot.Inverse(); XMMatrix mRot = m_ArcBall.GetRotationMatrix(); m_mRotSnapshot = mRot; // Accumulate the delta of the arcball's rotation in view space. // Note that per-frame delta rotations could be problematic over long periods of time. m_mRot *= m_mView * mLastRotInv * mRot * mInvView; // Since we're accumulating delta rotations, we need to orthonormalize // the matrix to prevent eventual matrix skew XMVector pXBasis = XMVector.FromFloat(m_mRot.M11, m_mRot.M12, m_mRot.M13, 0); XMVector pYBasis = XMVector.FromFloat(m_mRot.M21, m_mRot.M22, m_mRot.M23, 0); XMVector pZBasis = XMVector.FromFloat(m_mRot.M31, m_mRot.M32, m_mRot.M33, 0); pXBasis = XMVector3.Normalize(pXBasis); pYBasis = XMVector3.Cross(pZBasis, pXBasis); pYBasis = XMVector3.Normalize(pYBasis); pZBasis = XMVector3.Cross(pXBasis, pYBasis); pXBasis.W = m_mRot.M14; pYBasis.W = m_mRot.M24; pZBasis.W = m_mRot.M34; XMVector pWBasis = XMVector.FromFloat(m_mRot.M41, m_mRot.M42, m_mRot.M43, m_mRot.M44); m_mRot = new XMMatrix(pXBasis, pYBasis, pZBasis, pWBasis); // Transform the default direction vector by the light's rotation matrix m_vCurrentDir = XMVector3.TransformNormal(m_vDefaultDir, m_mRot); }
public static XMVector SrgbToXyz(XMVector srgb) { XMVector scale0 = XMVector.FromFloat(0.4124f, 0.2126f, 0.0193f, 0.0f); XMVector scale1 = XMVector.FromFloat(0.3576f, 0.7152f, 0.1192f, 0.0f); XMVector scale2 = XMVector.FromFloat(0.1805f, 0.0722f, 0.9505f, 0.0f); XMVector cutoff = XMVector.FromFloat(0.04045f, 0.04045f, 0.04045f, 0.0f); XMVector exp = XMVector.FromFloat(2.4f, 2.4f, 2.4f, 1.0f); XMVector sel = XMVector.Greater(srgb, cutoff); // lclr = clr / 12.92 XMVector smallC = XMVector.Divide(srgb, XMGlobalConstants.MsrgbScale); // lclr = pow( (clr + a) / (1+a), 2.4 ) XMVector largeC = XMVector.Pow(XMVector.Divide(XMVector.Add(srgb, XMGlobalConstants.MsrgbA), XMGlobalConstants.MsrgbA1), exp); XMVector lclr = XMVector.Select(smallC, largeC, sel); XMMatrix m = new XMMatrix(scale0, scale1, scale2, XMGlobalConstants.Zero); XMVector clr = XMVector3.Transform(lclr, m); return(XMVector.Select(srgb, clr, XMGlobalConstants.Select1110)); }
public override void FrameMove(double fElapsedTime) { if (IsKeyDown(m_aKeys[(int)SdkCameraKey.Reset])) { Reset(); } // Get keyboard/mouse/gamepad input GetInput(m_bEnablePositionMovement, (m_nActiveButtonMask & m_nCurrentButtonMask) != 0 || m_bRotateWithoutButtonDown); //// Get the mouse movement (if any) if the mouse button are down //if( (m_nActiveButtonMask & m_nCurrentButtonMask) || m_bRotateWithoutButtonDown ) // UpdateMouseDelta( fElapsedTime ); // Get amount of velocity based on the keyboard input and drag (if any) UpdateVelocity(fElapsedTime); // Simple euler method to calculate position delta XMVector vVelocity = m_vVelocity; XMVector vPosDelta = vVelocity * (float)fElapsedTime; // If rotating the camera if ((m_nActiveButtonMask & m_nCurrentButtonMask) != 0 || m_bRotateWithoutButtonDown) { // Update the pitch & yaw angle based on mouse movement float fYawDelta = m_vRotVelocity.X; float fPitchDelta = m_vRotVelocity.Y; // Invert pitch if requested if (m_bInvertPitch) { fPitchDelta = -fPitchDelta; } m_fCameraPitchAngle += fPitchDelta; m_fCameraYawAngle += fYawDelta; // Limit pitch to straight up or straight down m_fCameraPitchAngle = Math.Max(-XMMath.PIDivTwo, m_fCameraPitchAngle); m_fCameraPitchAngle = Math.Min(+XMMath.PIDivTwo, m_fCameraPitchAngle); } // Make a rotation matrix based on the camera's yaw & pitch XMMatrix mCameraRot = XMMatrix.RotationRollPitchYaw(m_fCameraPitchAngle, m_fCameraYawAngle, 0); // Transform vectors based on camera's rotation matrix XMVector vWorldUp = XMVector3.TransformCoord(XMVector.FromFloat(0.0f, 1.0f, 0.0f, 0.0f), mCameraRot); XMVector vWorldAhead = XMVector3.TransformCoord(XMVector.FromFloat(0.0f, 0.0f, 1.0f, 0.0f), mCameraRot); // Transform the position delta by the camera's rotation if (!m_bEnableYAxisMovement) { // If restricting Y movement, do not include pitch // when transforming position delta vector. mCameraRot = XMMatrix.RotationRollPitchYaw(0.0f, m_fCameraYawAngle, 0.0f); } XMVector vPosDeltaWorld = XMVector3.TransformCoord(vPosDelta, mCameraRot); // Move the eye position XMVector vEye = m_vEye; vEye += vPosDeltaWorld; if (m_bClipToBoundary) { vEye = ConstrainToBoundary(vEye); } m_vEye = vEye; // Update the lookAt position based on the eye position XMVector vLookAt = vEye + vWorldAhead; m_vLookAt = vLookAt; // Update the view matrix XMMatrix mView = XMMatrix.LookAtLH(vEye, vLookAt, vWorldUp); m_mView = mView; XMMatrix mCameraWorld = mView.Inverse(); m_mCameraWorld = mCameraWorld; }
public new XMVector GetEyePt() { return(XMVector.FromFloat(m_mCameraWorld.M41, m_mCameraWorld.M42, m_mCameraWorld.M43, 0.0f)); }
public XMVector GetWorldAheads() { return(XMVector.FromFloat(m_mCameraWorld.M31, m_mCameraWorld.M32, m_mCameraWorld.M33, 0.0f)); }
public XMVector GetWorldUp() { return(XMVector.FromFloat(m_mCameraWorld.M21, m_mCameraWorld.M22, m_mCameraWorld.M23, 0.0f)); }
public XMVector GetWorldRight() { return(XMVector.FromFloat(m_mCameraWorld.M11, m_mCameraWorld.M12, m_mCameraWorld.M13, 0.0f)); }
public override void FrameMove(double fElapsedTime) { if (IsKeyDown(m_aKeys[(int)SdkCameraKey.Reset])) { Reset(); } // If no dragged has happend since last time FrameMove is called, // and no camera key is held down, then no need to handle again. if (!m_bDragSinceLastUpdate && m_cKeysDown == 0) { return; } m_bDragSinceLastUpdate = false; //// If no mouse button is held down, //// Get the mouse movement (if any) if the mouse button are down //if( m_nCurrentButtonMask != 0 ) // UpdateMouseDelta( fElapsedTime ); GetInput(m_bEnablePositionMovement, m_nCurrentButtonMask != 0); // Get amount of velocity based on the keyboard input and drag (if any) UpdateVelocity(fElapsedTime); // Simple euler method to calculate position delta XMVector vPosDelta = XMVector.LoadFloat3(m_vVelocity) * (float)fElapsedTime; // Change the radius from the camera to the model based on wheel scrolling if (m_nMouseWheelDelta != 0 && m_nZoomButtonMask == SdkCameraMouseKeys.Wheel) { m_fRadius -= m_nMouseWheelDelta * m_fRadius * 0.1f / 120.0f; } m_fRadius = Math.Min(m_fMaxRadius, m_fRadius); m_fRadius = Math.Max(m_fMinRadius, m_fRadius); m_nMouseWheelDelta = 0; // Get the inverse of the arcball's rotation matrix XMMatrix mCameraRot = m_ViewArcBall.GetRotationMatrix().Inverse(); // Transform vectors based on camera's rotation matrix XMVector vWorldUp = XMVector3.TransformCoord(XMVector.FromFloat(0.0f, 1.0f, 0.0f, 0.0f), mCameraRot); XMVector vWorldAhead = XMVector3.TransformCoord(XMVector.FromFloat(0.0f, 0.0f, 1.0f, 0.0f), mCameraRot); // Transform the position delta by the camera's rotation XMVector vPosDeltaWorld = XMVector3.TransformCoord(vPosDelta, mCameraRot); // Move the lookAt position XMVector vLookAt = m_vLookAt; vLookAt += vPosDeltaWorld; if (m_bClipToBoundary) { vLookAt = ConstrainToBoundary(vLookAt); } m_vLookAt = vLookAt; // Update the eye point based on a radius away from the lookAt position XMVector vEye = vLookAt - vWorldAhead * m_fRadius; m_vEye = vEye; // Update the view matrix XMMatrix mView = XMMatrix.LookAtLH(vEye, vLookAt, vWorldUp); m_mView = mView; XMMatrix mInvView = mView.Inverse(); mInvView.M41 = 0.0f; mInvView.M42 = 0.0f; mInvView.M43 = 0.0f; XMMatrix mModelLastRot = m_mModelLastRot; XMMatrix mModelLastRotInv = mModelLastRot.Inverse(); // Accumulate the delta of the arcball's rotation in view space. // Note that per-frame delta rotations could be problematic over long periods of time. XMMatrix mModelRot0 = m_WorldArcBall.GetRotationMatrix(); XMMatrix mModelRot = m_mModelRot; mModelRot *= mView * mModelLastRotInv * mModelRot0 * mInvView; if (m_ViewArcBall.IsBeingDragged() && m_bAttachCameraToModel && !IsKeyDown(m_aKeys[(int)SdkCameraKey.ControlDown])) { // Attach camera to model by inverse of the model rotation XMMatrix mCameraRotLast = m_mCameraRotLast; XMMatrix mCameraLastRotInv = mCameraRotLast.Inverse(); XMMatrix mCameraRotDelta = mCameraLastRotInv * mCameraRot; // local to world matrix mModelRot *= mCameraRotDelta; } m_mModelLastRot = mModelRot0; m_mCameraRotLast = mCameraRot; // Since we're accumulating delta rotations, we need to orthonormalize // the matrix to prevent eventual matrix skew XMVector xBasis = XMVector3.Normalize(XMVector.FromFloat(mModelRot.M11, mModelRot.M12, mModelRot.M13, mModelRot.M14)); XMVector yBasis = XMVector3.Cross(XMVector.FromFloat(mModelRot.M31, mModelRot.M32, mModelRot.M33, mModelRot.M34), xBasis); yBasis = XMVector3.Normalize(yBasis); XMVector zBasis = XMVector3.Cross(xBasis, yBasis); mModelRot.M11 = xBasis.X; mModelRot.M12 = xBasis.Y; mModelRot.M13 = xBasis.Z; mModelRot.M21 = yBasis.X; mModelRot.M22 = yBasis.Y; mModelRot.M23 = yBasis.Z; mModelRot.M31 = zBasis.X; mModelRot.M32 = zBasis.Y; mModelRot.M33 = zBasis.Z; // Translate the rotation matrix to the same position as the lookAt position mModelRot.M41 = vLookAt.X; mModelRot.M42 = vLookAt.Y; mModelRot.M43 = vLookAt.Z; m_mModelRot = mModelRot; // Translate world matrix so its at the center of the model XMMatrix mTrans = XMMatrix.Translation(-m_vModelCenter.X, -m_vModelCenter.Y, -m_vModelCenter.Z); XMMatrix mWorld = mTrans * mModelRot; m_mWorld = mWorld; }