internal void TransformFrameAbsolute(SdkMeshFile file, double time) { int tick = file.GetAnimationKeyFromTime(time); if (this.AnimationFrameIndex != -1) { SdkMeshAnimationFrame animationFrame = file.AnimationFrames[this.AnimationFrameIndex]; SdkMeshAnimationKey animationKey = animationFrame.AnimationKeys[tick]; SdkMeshAnimationKey animationKeyOrig = animationFrame.AnimationKeys[0]; XMMatrix mTrans1 = XMMatrix.Translation(-animationKeyOrig.Translation.X, -animationKeyOrig.Translation.Y, -animationKeyOrig.Translation.Z); XMMatrix mTrans2 = XMMatrix.Translation(animationKey.Translation.X, animationKey.Translation.Y, animationKey.Translation.Z); XMVector quat1 = animationKeyOrig.Orientation; quat1 = XMQuaternion.Inverse(quat1); XMMatrix mRot1 = XMMatrix.RotationQuaternion(quat1); XMMatrix mInvTo = mTrans1 * mRot1; XMVector quat2 = animationKey.Orientation; XMMatrix mRot2 = XMMatrix.RotationQuaternion(quat2); XMMatrix mFrom = mRot2 * mTrans2; XMMatrix mOutput = mInvTo * mFrom; this.TransformedFrameMatrix = mOutput; } }
static void Transform(XMMatrix target, Viewport viewport, float angle, float scale, float w, float h) { using var r = new XMMatrix(); using var b = new XMMatrix(); using var s = new XMMatrix(); using var t = new XMMatrix(); r.RotationZ((float)(angle / 180 * Math.PI)); s.Scaling(scale, scale, 1.0f); t.Translation(viewport.Width / 2 - w * scale / 2, viewport.Height / 2 - h * scale / 2, 0.0f); b.Translation(-viewport.Width / 2, -viewport.Height / 2, 0.0f); b.Multiply(r); r.Set(b); b.Identity(); b.Translation(viewport.Width / 2, viewport.Height / 2, 0.0f); r.Multiply(b); s.Multiply(t); s.Multiply(r); target.Set(s); }
public void Update(ITimer timer) { float t = timer == null ? 0.0f : (float)timer.TotalSeconds; this.worldMatrix1 = XMMatrix.RotationY(t); XMMatrix spin = XMMatrix.RotationZ(-t); XMMatrix orbit = XMMatrix.RotationY(-t * 2.0f); XMMatrix translate = XMMatrix.Translation(-4.0f, 0.0f, 0.0f); XMMatrix scale = XMMatrix.Scaling(0.3f, 0.3f, 0.3f); this.worldMatrix2 = scale * spin * translate * orbit; }
internal void TransformFrame(SdkMeshFile file, XMMatrix parentWorld, double time) { // Get the tick data XMMatrix mLocalTransform; int tick = file.GetAnimationKeyFromTime(time); if (this.AnimationFrameIndex == -1) { mLocalTransform = this.Matrix; } else { SdkMeshAnimationFrame animationFrame = file.AnimationFrames[this.AnimationFrameIndex]; SdkMeshAnimationKey animationKey = animationFrame.AnimationKeys[tick]; // turn it into a matrix (Ignore scaling for now) XMFloat3 parentPos = animationKey.Translation; XMMatrix mTranslate = XMMatrix.Translation(parentPos.X, parentPos.Y, parentPos.Z); XMVector quat = animationKey.Orientation; if (XMVector4.Equal(quat, XMVector.Zero)) { quat = XMQuaternion.Identity; } quat = XMQuaternion.Normalize(quat); XMMatrix mQuat = XMMatrix.RotationQuaternion(quat); mLocalTransform = mQuat * mTranslate; } // Transform ourselves XMMatrix mLocalWorld = mLocalTransform * parentWorld; this.TransformedFrameMatrix = mLocalWorld; this.WorldPoseFrameMatrix = mLocalWorld; // Transform our siblings if (this.SiblingFrameIndex != -1) { file.Frames[this.SiblingFrameIndex].TransformFrame(file, parentWorld, time); } // Transform our children if (this.ChildFrameIndex != -1) { file.Frames[this.ChildFrameIndex].TransformFrame(file, mLocalWorld, time); } }
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; }
public static byte[] Generate(uint width, uint height, string optFileName, RenderFace side, RenderMethod method) { var options = new DeviceResourcesOptions { UseHighestFeatureLevel = false, PreferMultisampling = true }; var deviceResources = new RenderTargetDeviceResources(width, height, D3D11FeatureLevel.FeatureLevel100, options); var optComponent = new OptComponent(optFileName); try { optComponent.CreateDeviceDependentResources(deviceResources); optComponent.CreateWindowSizeDependentResources(); optComponent.Update(null); optComponent.WorldMatrix = XMMatrix.Translation(optComponent.OptCenter.X, -optComponent.OptCenter.Z, optComponent.OptCenter.Y) * XMMatrix.ScalingFromVector(XMVector.Replicate(0.95f / optComponent.OptSize)); switch (side) { case RenderFace.Top: default: optComponent.ViewMatrix = XMMatrix.LookAtRH(new XMFloat3(0, 2, 0), new XMFloat3(0, 0, 0), new XMFloat3(0, 0, 1)); break; case RenderFace.Front: optComponent.ViewMatrix = XMMatrix.LookAtRH(new XMFloat3(0, 0, 2), new XMFloat3(0, 0, 0), new XMFloat3(0, 1, 0)); break; case RenderFace.Side: optComponent.ViewMatrix = XMMatrix.LookAtRH(new XMFloat3(2, 0, 0), new XMFloat3(0, 0, 0), new XMFloat3(0, 1, 0)); break; } optComponent.ProjectionMatrix = XMMatrix.OrthographicRH(1, 1, 0, 5); var context = deviceResources.D3DContext; context.OutputMergerSetRenderTargets(new[] { deviceResources.D3DRenderTargetView }, deviceResources.D3DDepthStencilView); context.ClearRenderTargetView(deviceResources.D3DRenderTargetView, new[] { 0.0f, 0.0f, 0.0f, 0.0f }); context.ClearDepthStencilView(deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth, 1.0f, 0); optComponent.Render(); deviceResources.Present(); var buffer = deviceResources.GetBackBufferContent(); for (int i = 0; i < width * height; i++) { byte a = buffer[i * 4 + 3]; buffer[i * 4 + 3] = a == 0 ? (byte)0 : (byte)255; } switch (method) { case RenderMethod.Color: default: return(buffer); case RenderMethod.GrayScaleLight: return(ConvertToGrayScaleLight(buffer, (int)width, (int)height)); case RenderMethod.GrayScaleDark: return(ConvertToGrayScaleDark(buffer, (int)width, (int)height)); case RenderMethod.Blue: return(ConvertToBlue(buffer, (int)width, (int)height)); } } finally { optComponent.ReleaseWindowSizeDependentResources(); optComponent.ReleaseDeviceDependentResources(); deviceResources.Release(); } }
public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; XMFloat3 vCenter = new XMFloat3(0.25767413f, -28.503521f, 111.00689f); XMMatrix m = XMMatrix.Translation(-vCenter.X, -vCenter.Y, -vCenter.Z); m *= XMMatrix.RotationY(XMMath.PI); m *= XMMatrix.RotationX(XMMath.PIDivTwo); this.centerMesh = m; // Load the mesh this.mesh = SdkMeshFile.FromFile( this.deviceResources.D3DDevice, this.deviceResources.D3DContext, "Tiny\\Tiny.sdkmesh"); // Create the shaders byte[] vertexShaderBytecode = File.ReadAllBytes("BasicHLSL11_VS.cso"); this.vertexShader = this.deviceResources.D3DDevice.CreateVertexShader(vertexShaderBytecode, null); D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[] { new D3D11InputElementDesc { SemanticName = "POSITION", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 0, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "NORMAL", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 12, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 }, new D3D11InputElementDesc { SemanticName = "TEXCOORD", SemanticIndex = 0, Format = DxgiFormat.R32G32Float, InputSlot = 0, AlignedByteOffset = 24, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode); byte[] pixelShaderBytecode = File.ReadAllBytes("BasicHLSL11_PS.cso"); this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null); // Create a sampler state D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc( D3D11Filter.MinMagMipLinear, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, D3D11TextureAddressMode.Wrap, 0.0f, 1, D3D11ComparisonFunction.Always, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, 0.0f, float.MaxValue); this.sampler = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc); // Setup constant buffers this.constantBufferVSPerObject = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(ConstantBufferVSPerObject.Size, D3D11BindOptions.ConstantBuffer)); this.constantBufferPSPerObject = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(ConstantBufferPSPerObject.Size, D3D11BindOptions.ConstantBuffer)); this.constantBufferPSPerFrame = this.deviceResources.D3DDevice.CreateBuffer( new D3D11BufferDesc(ConstantBufferPSPerFrame.Size, D3D11BindOptions.ConstantBuffer)); }
private void Animate(double fTime) { float t = (float)(fTime * 0.2); float camera0OriginX = this.cameraOrigins[0].X; float camera1OriginX = this.cameraOrigins[1].X; float camera2OriginX = this.cameraOrigins[2].X; float camera3OriginX = this.cameraOrigins[3].X; float camera3OriginZ = this.cameraOrigins[3].Z; // animate sphere 0 around the frustum { BoundingSphere sphere = this.secondarySpheres[0].Sphere; sphere.Center = new XMFloat3 { X = 10 * XMScalar.Sin(3 * t), Y = 7 * XMScalar.Cos(5 * t), Z = sphere.Center.Z }; this.secondarySpheres[0].Sphere = sphere; } // animate oriented box 0 around the frustum { BoundingOrientedBox box = this.secondaryOrientedBoxes[0].Box; box.Center = new XMFloat3 { X = 8 * XMScalar.Sin(3.5f * t), Y = 5 * XMScalar.Cos(5.1f * t), Z = box.Center.Z }; box.Orientation = XMQuaternion.RotationRollPitchYaw(t * 1.4f, t * 0.2f, t); this.secondaryOrientedBoxes[0].Box = box; } // animate aligned box 0 around the frustum { BoundingBox box = this.secondaryAABoxes[0].Box; box.Center = new XMFloat3 { X = 10 * XMScalar.Sin(2.1f * t), Y = 7 * XMScalar.Cos(3.8f * t), Z = box.Center.Z }; this.secondaryAABoxes[0].Box = box; } // animate sphere 1 around the aligned box { BoundingSphere sphere = this.secondarySpheres[1].Sphere; sphere.Center = new XMFloat3 { X = 8 * XMScalar.Sin(2.9f * t) + camera1OriginX, Y = 8 * XMScalar.Cos(4.6f * t), Z = 8 * XMScalar.Cos(1.6f * t) }; this.secondarySpheres[1].Sphere = sphere; } // animate oriented box 1 around the aligned box { BoundingOrientedBox box = this.secondaryOrientedBoxes[1].Box; box.Center = new XMFloat3 { X = 8 * XMScalar.Sin(3.2f * t) + camera1OriginX, Y = 8 * XMScalar.Cos(2.1f * t), Z = 8 * XMScalar.Sin(1.6f * t) }; box.Orientation = XMQuaternion.RotationRollPitchYaw(t * 0.7f, t * 1.3f, t); this.secondaryOrientedBoxes[1].Box = box; } // animate aligned box 1 around the aligned box { BoundingBox box = this.secondaryAABoxes[1].Box; box.Center = new XMFloat3 { X = 8 * XMScalar.Sin(1.1f * t) + camera1OriginX, Y = 8 * XMScalar.Cos(5.8f * t), Z = 8 * XMScalar.Cos(3.0f * t) }; this.secondaryAABoxes[1].Box = box; } // animate sphere 2 around the oriented box { BoundingSphere sphere = this.secondarySpheres[2].Sphere; sphere.Center = new XMFloat3 { X = 8 * XMScalar.Sin(2.2f * t) + camera2OriginX, Y = 8 * XMScalar.Cos(4.3f * t), Z = 8 * XMScalar.Cos(1.8f * t) }; this.secondarySpheres[2].Sphere = sphere; } // animate oriented box 2 around the oriented box { BoundingOrientedBox box = this.secondaryOrientedBoxes[2].Box; box.Center = new XMFloat3 { X = 8 * XMScalar.Sin(3.7f * t) + camera2OriginX, Y = 8 * XMScalar.Cos(2.5f * t), Z = 8 * XMScalar.Sin(1.1f * t) }; box.Orientation = XMQuaternion.RotationRollPitchYaw(t * 0.9f, t * 1.8f, t); this.secondaryOrientedBoxes[2].Box = box; } // animate aligned box 2 around the oriented box { BoundingBox box = this.secondaryAABoxes[2].Box; box.Center = new XMFloat3 { X = 8 * XMScalar.Sin(1.3f * t) + camera2OriginX, Y = 8 * XMScalar.Cos(5.2f * t), Z = 8 * XMScalar.Cos(3.5f * t) }; this.secondaryAABoxes[2].Box = box; } // triangle points in local space - equilateral triangle with radius of 2 XMVector trianglePointA = new(0, 2, 0, 0); XMVector trianglePointB = new(1.732f, -1, 0, 0); XMVector trianglePointC = new(-1.732f, -1, 0, 0); XMMatrix triangleCoords; XMMatrix translation; // animate triangle 0 around the frustum triangleCoords = XMMatrix.RotationRollPitchYaw(t * 1.4f, t * 2.5f, t); translation = XMMatrix.Translation( 5 * XMScalar.Sin(5.3f * t) + camera0OriginX, 5 * XMScalar.Cos(2.3f * t), 5 * XMScalar.Sin(3.4f * t)); triangleCoords = XMMatrix.Multiply(triangleCoords, translation); this.secondaryTriangles[0].PointA = XMVector3.Transform(trianglePointA, triangleCoords); this.secondaryTriangles[0].PointB = XMVector3.Transform(trianglePointB, triangleCoords); this.secondaryTriangles[0].PointC = XMVector3.Transform(trianglePointC, triangleCoords); // animate triangle 1 around the aligned box triangleCoords = XMMatrix.RotationRollPitchYaw(t * 1.4f, t * 2.5f, t); translation = XMMatrix.Translation( 8 * XMScalar.Sin(5.3f * t) + camera1OriginX, 8 * XMScalar.Cos(2.3f * t), 8 * XMScalar.Sin(3.4f * t)); triangleCoords = XMMatrix.Multiply(triangleCoords, translation); this.secondaryTriangles[1].PointA = XMVector3.Transform(trianglePointA, triangleCoords); this.secondaryTriangles[1].PointB = XMVector3.Transform(trianglePointB, triangleCoords); this.secondaryTriangles[1].PointC = XMVector3.Transform(trianglePointC, triangleCoords); // animate triangle 2 around the oriented box triangleCoords = XMMatrix.RotationRollPitchYaw(t * 1.4f, t * 2.5f, t); translation = XMMatrix.Translation( 8 * XMScalar.Sin(5.3f * t) + camera2OriginX, 8 * XMScalar.Cos(2.3f * t), 8 * XMScalar.Sin(3.4f * t)); triangleCoords = XMMatrix.Multiply(triangleCoords, translation); this.secondaryTriangles[2].PointA = XMVector3.Transform(trianglePointA, triangleCoords); this.secondaryTriangles[2].PointB = XMVector3.Transform(trianglePointB, triangleCoords); this.secondaryTriangles[2].PointC = XMVector3.Transform(trianglePointC, triangleCoords); // animate primary ray (this is the only animated primary object) this.primaryRay.Direction = new XMVector(XMScalar.Sin(t * 3), 0, XMScalar.Cos(t * 3), 0); // animate sphere 3 around the ray { BoundingSphere sphere = this.secondarySpheres[3].Sphere; sphere.Center = new XMFloat3(camera3OriginX - 3, 0.5f * XMScalar.Sin(t * 5), camera3OriginZ); this.secondarySpheres[3].Sphere = sphere; } // animate aligned box 3 around the ray { BoundingBox box = this.secondaryAABoxes[3].Box; box.Center = new XMFloat3(camera3OriginX + 3, 0.5f * XMScalar.Sin(t * 4), camera3OriginZ); this.secondaryAABoxes[3].Box = box; } // animate oriented box 3 around the ray { BoundingOrientedBox box = this.secondaryOrientedBoxes[3].Box; box.Center = new XMFloat3(camera3OriginX, 0.5f * XMScalar.Sin(t * 4.5f), camera3OriginZ + 3); box.Orientation = XMQuaternion.RotationRollPitchYaw(t * 0.9f, t * 1.8f, t); this.secondaryOrientedBoxes[3].Box = box; } // animate triangle 3 around the ray triangleCoords = XMMatrix.RotationRollPitchYaw(t * 1.4f, t * 2.5f, t); translation = XMMatrix.Translation( camera3OriginX, 0.5f * XMScalar.Cos(4.3f * t), camera3OriginZ - 3); triangleCoords = XMMatrix.Multiply(triangleCoords, translation); this.secondaryTriangles[3].PointA = XMVector3.Transform(trianglePointA, triangleCoords); this.secondaryTriangles[3].PointB = XMVector3.Transform(trianglePointB, triangleCoords); this.secondaryTriangles[3].PointC = XMVector3.Transform(trianglePointC, triangleCoords); }