public BoundingBox(XMFloat3 center, XMFloat3 extents) { Debug.Assert(extents.X >= 0 && extents.Y >= 0 && extents.Z >= 0, "Reviewed"); this.center = center; this.extents = extents; }
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 void Init() { { XMFloat3 vecEye = new XMFloat3(100.0f, 5.0f, 5.0f); XMFloat3 vecAt = new XMFloat3(0.0f, 0.0f, 0.0f); XMFloat3 vecUp = new XMFloat3(0.0f, 1.0f, 0.0f); this.ViewerCameraView = XMMatrix.LookAtLH(vecEye, vecAt, vecUp); this.ViewerCameraProjection = XMMatrix.PerspectiveFovLH(XMMath.PIDivFour, 1.0f, 0.05f, 1.0f); this.ViewerCameraNearClip = 0.05f; this.ViewerCameraFarClip = 1.0f; } { XMFloat3 vecEye = new XMFloat3(-320.0f, 300.0f, -220.3f); XMFloat3 vecAt = new XMFloat3(0.0f, 0.0f, 0.0f); XMFloat3 vecUp = new XMFloat3(0.0f, 1.0f, 0.0f); this.LightCameraWorld = XMMatrix.Identity; this.LightCameraView = XMMatrix.LookAtLH(vecEye, vecAt, vecUp); this.LightCameraProjection = XMMatrix.PerspectiveFovLH(XMMath.PIDivFour, 1.0f, 0.1f, 1000.0f); this.LightCameraEyePoint = vecEye; this.LightCameraLookAtPoint = vecAt; } this.ActiveCameraView = this.ViewerCameraView; this.ActiveCameraProjection = this.ViewerCameraProjection; this.MeshLength = 1.0f; }
protected void UpdateVelocity(double fElapsedTime) { XMVector vMouseDelta = m_vMouseDelta; XMVector vRotVelocity = vMouseDelta * m_fRotationScaler; m_vRotVelocity = vRotVelocity; XMVector vKeyboardDirection = m_vKeyboardDirection; XMVector vAccel = vKeyboardDirection; // Normalize vector so if moving 2 dirs (left & forward), // the camera doesn't move faster than if moving in 1 dir vAccel = XMVector3.Normalize(vAccel); // Scale the acceleration vector vAccel *= m_fMoveScaler; if (m_bMovementDrag) { // Is there any acceleration this frame? if (XMVector3.LengthSquare(vAccel).X > 0) { // If so, then this means the user has pressed a movement key // so change the velocity immediately to acceleration // upon keyboard input. This isn't normal physics // but it will give a quick response to keyboard input m_vVelocity = vAccel; m_fDragTimer = m_fTotalDragTimeToZero; m_vVelocityDrag = vAccel / (float)m_fDragTimer; } else { // If no key being pressed, then slowly decrease velocity to 0 if (m_fDragTimer > 0) { // Drag until timer is <= 0 XMVector vVelocity = m_vVelocity; XMVector vVelocityDrag = m_vVelocityDrag; vVelocity -= vVelocityDrag * (float)fElapsedTime; m_vVelocity = vVelocity; m_fDragTimer -= fElapsedTime; } else { // Zero velocity m_vVelocity = XMVector.Zero; } } } else { // No drag, so immediately change the velocity m_vVelocity = vAccel; } }
private void InitCamera() { XMFloat3 vMin = new XMFloat3(-1000.0f, -1000.0f, -1000.0f); XMFloat3 vMax = new XMFloat3(1000.0f, 1000.0f, 1000.0f); { XMFloat3 vecEye = new XMFloat3(100.0f, 5.0f, 5.0f); XMFloat3 vecAt = new XMFloat3(0.0f, 0.0f, 0.0f); this.viewerCamera.SetViewParams(vecEye, vecAt); this.viewerCamera.SetRotateButtons(true, false, false); this.viewerCamera.SetScalers(0.01f, 10.0f); this.viewerCamera.SetDrag(true); this.viewerCamera.SetEnableYAxisMovement(true); this.viewerCamera.SetClipToBoundary(true, vMin, vMax); this.viewerCamera.FrameMove(0.0); } { XMFloat3 vecEye = new XMFloat3(-320.0f, 300.0f, -220.3f); XMFloat3 vecAt = new XMFloat3(0.0f, 0.0f, 0.0f); this.lightCamera.SetViewParams(vecEye, vecAt); this.lightCamera.SetRotateButtons(true, false, false); this.lightCamera.SetScalers(0.01f, 50.0f); this.lightCamera.SetDrag(true); this.lightCamera.SetEnableYAxisMovement(true); this.lightCamera.SetClipToBoundary(true, vMin, vMax); this.lightCamera.SetProjParams(XMMath.PIDivFour, 1.0f, 0.1f, 1000.0f); this.lightCamera.FrameMove(0.0); } }
public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; var d3dDevice = this.deviceResources.D3DDevice; // Create the shaders byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.cso"); this.g_pVertexShader = d3dDevice.CreateVertexShader(vertexShaderBytecode, null); this.g_pHullShaderInteger = d3dDevice.CreateHullShader(File.ReadAllBytes("HullShaderInteger.cso"), null); this.g_pHullShaderFracEven = d3dDevice.CreateHullShader(File.ReadAllBytes("HullShaderFracEven.cso"), null); this.g_pHullShaderFracOdd = d3dDevice.CreateHullShader(File.ReadAllBytes("HullShaderFracOdd.cso"), null); this.g_pDomainShader = d3dDevice.CreateDomainShader(File.ReadAllBytes("DomainShader.cso"), null); this.g_pPixelShader = d3dDevice.CreatePixelShader(File.ReadAllBytes("PixelShader.cso"), null); this.g_pSolidColorPS = d3dDevice.CreatePixelShader(File.ReadAllBytes("SolidColorPS.cso"), null); // Create our vertex input layout - this matches the BEZIER_CONTROL_POINT structure D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[] { new D3D11InputElementDesc { SemanticName = "POSITION", SemanticIndex = 0, Format = DxgiFormat.R32G32B32Float, InputSlot = 0, AlignedByteOffset = 0, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.g_pPatchLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode); // Create constant buffers this.g_pcbPerFrame = d3dDevice.CreateBuffer(new D3D11BufferDesc(ConstantBufferConstants.Size, D3D11BindOptions.ConstantBuffer)); // Create solid and wireframe rasterizer state objects D3D11RasterizerDesc rasterDesc = D3D11RasterizerDesc.Default; rasterDesc.CullMode = D3D11CullMode.None; rasterDesc.IsDepthClipEnabled = true; rasterDesc.FillMode = D3D11FillMode.Solid; this.g_pRasterizerStateSolid = d3dDevice.CreateRasterizerState(rasterDesc); rasterDesc.FillMode = D3D11FillMode.WireFrame; this.g_pRasterizerStateWireframe = d3dDevice.CreateRasterizerState(rasterDesc); D3D11BufferDesc vbdesc = D3D11BufferDesc.From(MobiusStrip.Points, D3D11BindOptions.VertexBuffer); this.g_pControlPointVB = d3dDevice.CreateBuffer(vbdesc, MobiusStrip.Points, 0, 0); XMFloat3 vecEye = new XMFloat3(1.0f, 1.5f, -3.5f); XMFloat3 vecAt = new XMFloat3(0.0f, 0.0f, 0.0f); XMFloat3 vecUp = new XMFloat3(0.0f, 1.0f, 0.0f); this.ViewMatrix = XMMatrix.LookAtLH(vecEye, vecAt, vecUp); this.EyePt = vecEye; }
public BoundingSphere(XMFloat3 center, float radius) { if (radius < 0.0f) { throw new ArgumentOutOfRangeException("radius"); } this.center = center; this.radius = radius; }
protected override void CreateDeviceDependentResources() { base.CreateDeviceDependentResources(); this.mainGameComponent.CreateDeviceDependentResources(this.DeviceResources); // Setup the camera's view parameters XMFloat3 vecEye = new XMFloat3(0.0f, 0.0f, -5.0f); XMFloat3 vecAt = new XMFloat3(0.0f, 0.0f, -0.0f); this.camera.SetViewParams(vecEye, vecAt); }
public DirectionWidget() { m_fRadius = 1.0f; m_vDefaultDir = new XMFloat3(0, 1, 0); m_vCurrentDir = m_vDefaultDir; m_nRotateMask = SdkCameraMouseKeys.RightButton; m_mView = XMMatrix.Identity; m_mRot = XMMatrix.Identity; m_mRotSnapshot = XMMatrix.Identity; m_ArcBall = new SdkArcBall(); }
public void SetClipToBoundary(bool bClipToBoundary, XMFloat3?pvMinBoundary, XMFloat3?pvMaxBoundary) { m_bClipToBoundary = bClipToBoundary; if (pvMinBoundary.HasValue) { m_vMinBoundary = pvMinBoundary.Value; } if (pvMaxBoundary.HasValue) { m_vMaxBoundary = pvMaxBoundary.Value; } }
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 void CreateWindowSizeDependentResources() { this.tessellator.CreateWindowSizeDependentResources(); XMMatrix mWorld = XMMatrix.Identity; XMFloat3 vecEye = new XMFloat3(0.0f, 0.0f, -300.0f); XMFloat3 vecAt = new XMFloat3(10.0f, 20.0f, 0.0f); XMFloat3 vecUp = new XMFloat3(0.0f, 1.0f, 0.0f); XMMatrix mView = XMMatrix.LookAtLH(vecEye, vecAt, vecUp); float fAspectRatio = (float)this.deviceResources.BackBufferWidth / (float)this.deviceResources.BackBufferHeight; XMMatrix mProj = XMMatrix.PerspectiveFovLH(XMMath.PIDivFour, fAspectRatio, 1.0f, 500000.0f); XMMatrix mWorldViewProjection = mWorld * mView * mProj; this.WorldViewProjectionMatrix = mWorldViewProjection; }
public XMFloat3[] GetCorners() { XMFloat3[] corners = new XMFloat3[BoundingBox.CornerCount]; // Load the box XMVector boxCenter = this.center; XMVector boxExtents = this.extents; for (int i = 0; i < BoundingBox.CornerCount; i++) { corners[i] = XMVector.MultiplyAdd(boxExtents, CollisionGlobalConstants.BoxOffsets[i], boxCenter); } return(corners); }
public MainGameComponent() { var vLightDir = new XMFloat3(-1, 1, -1); this.LightDirection = XMVector3.Normalize(vLightDir); XMVector eye = new XMVector(0.0f, 0.0f, -100.0f, 0.0f); XMVector at = new XMVector(0.0f, 0.0f, -0.0f, 0.0f); XMVector up = new XMVector(0.0f, 1.0f, 0.0f, 0.0f); float fObjectRadius = 378.15607f; XMVector radius = XMVector3.Normalize(at - eye).Scale(fObjectRadius * 3.0f); this.ViewMatrix = XMMatrix.LookAtLH(eye, at, up) * XMMatrix.TranslationFromVector(radius); this.WorldMatrix = XMMatrix.Identity; }
protected void GetInput(bool bGetKeyboardInput, bool bGetMouseInput) { m_vKeyboardDirection = XMVector.Zero; if (bGetKeyboardInput) { // Update acceleration vector based on keyboard state if (IsKeyDown(m_aKeys[(int)SdkCameraKey.MoveForward])) { m_vKeyboardDirection.Z += 1.0f; } if (IsKeyDown(m_aKeys[(int)SdkCameraKey.MoveBackward])) { m_vKeyboardDirection.Z -= 1.0f; } if (m_bEnableYAxisMovement) { if (IsKeyDown(m_aKeys[(int)SdkCameraKey.MoveUp])) { m_vKeyboardDirection.Y += 1.0f; } if (IsKeyDown(m_aKeys[(int)SdkCameraKey.MoveDown])) { m_vKeyboardDirection.Y -= 1.0f; } } if (IsKeyDown(m_aKeys[(int)SdkCameraKey.StrafeRight])) { m_vKeyboardDirection.X += 1.0f; } if (IsKeyDown(m_aKeys[(int)SdkCameraKey.StrafeLeft])) { m_vKeyboardDirection.X -= 1.0f; } } if (bGetMouseInput) { UpdateMouseDelta(); } }
protected override void CreateDeviceDependentResources() { base.CreateDeviceDependentResources(); this.mainGameComponent.CreateDeviceDependentResources(this.DeviceResources); // Setup the camera this.camera.Reset(); XMFloat3 vecEye = new XMFloat3(0.95f, 5.83f, -14.48f); XMFloat3 vecAt = new XMFloat3(0.90f, 5.44f, -13.56f); this.camera.SetViewParams(vecEye, vecAt); this.lightCamera.Reset(); XMFloat3 vecEyeL = new XMFloat3(0, 0, 0); XMFloat3 vecAtL = new XMFloat3(0, -0.5f, 1); this.lightCamera.SetViewParams(vecEyeL, vecAtL); }
protected override void CreateDeviceDependentResources() { base.CreateDeviceDependentResources(); this.mainGameComponent.CreateDeviceDependentResources(this.DeviceResources); float fObjectRadius = 378.15607f; // Setup the camera's view parameters XMFloat3 vecEye = new XMFloat3(0.0f, 0.0f, -100.0f); XMFloat3 vecAt = new XMFloat3(0.0f, 0.0f, -0.0f); this.camera.SetViewParams(vecEye, vecAt); this.camera.SetRadius(fObjectRadius * 3.0f, fObjectRadius * 0.5f, fObjectRadius * 10.0f); var vLightDir = new XMFloat3(-1, 1, -1); vLightDir = XMVector3.Normalize(vLightDir); this.lightControl.SetLightDirection(vLightDir); }
private static void LoadParticles(Random rand, Particle[] pParticles, int startIndex, XMFloat3 center, XMFloat4 velocity, float spread, int numParticles) { for (int i = 0; i < numParticles; i++) { XMFloat3 delta = new XMFloat3(spread, spread, spread); while (XMVector3.LengthSquare(delta).X > spread * spread) { delta.X = RPercent(rand) * spread; delta.Y = RPercent(rand) * spread; delta.Z = RPercent(rand) * spread; } pParticles[startIndex + i].pos.X = center.X + delta.X; pParticles[startIndex + i].pos.Y = center.Y + delta.Y; pParticles[startIndex + i].pos.Z = center.Z + delta.Z; pParticles[startIndex + i].pos.W = 10000.0f * 10000.0f; pParticles[startIndex + i].velo = velocity; } }
public SdkModelViewerCamera() { m_nRotateModelButtonMask = SdkCameraMouseKeys.LeftButton; m_nZoomButtonMask = SdkCameraMouseKeys.Wheel; m_nRotateCameraButtonMask = SdkCameraMouseKeys.RightButton; m_bDragSinceLastUpdate = true; m_fRadius = 5.0f; m_fDefaultRadius = 5.0f; m_fMinRadius = 1.0f; m_fMaxRadius = float.MaxValue; XMMatrix id = XMMatrix.Identity; m_mWorld = id; m_mModelRot = id; m_mModelLastRot = id; m_mCameraRotLast = id; m_vModelCenter = XMVector.Zero; m_bEnablePositionMovement = false; }
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 void SetModelCenter(XMFloat3 vModelCenter) { m_vModelCenter = vModelCenter; }
public SimpleVertex(XMFloat3 pos, XMFloat4 color) { this.Pos = pos; this.Color = color; }
public BasicVertex(XMFloat3 position, XMFloat3 normal, XMFloat2 textureCoordinates) { this.Position = position; this.Normal = normal; this.TextureCoordinates = textureCoordinates; }
public static XMVector CalculateEigenVector(float m11, float m12, float m13, float m22, float m23, float m33, float e) { XMVector v_tmp = new XMFloat3( (float)((m12 * m23) - (m13 * (m22 - e))), (float)((m13 * m12) - (m23 * (m11 - e))), (float)(((m11 - e) * (m22 - e)) - (m12 * m12))); // planar or linear if (XMVector3.Equal(v_tmp, XMGlobalConstants.Zero)) { float f1, f2, f3; // we only have one equation - find a valid one if ((m11 - e != 0.0f) || (m12 != 0.0f) || (m13 != 0.0f)) { f1 = m11 - e; f2 = m12; f3 = m13; } else if ((m12 != 0.0f) || (m22 - e != 0.0f) || (m23 != 0.0f)) { f1 = m12; f2 = m22 - e; f3 = m23; } else if ((m13 != 0.0f) || (m23 != 0.0f) || (m33 - e != 0.0f)) { f1 = m13; f2 = m23; f3 = m33 - e; } else { // error, we'll just make something up - we have NO context f1 = 1.0f; f2 = 0.0f; f3 = 0.0f; } if (f1 == 0.0f) { v_tmp.X = 0.0f; } else { v_tmp.X = 1.0f; } if (f2 == 0.0f) { v_tmp.Y = 0.0f; } else { v_tmp.Y = 1.0f; } if (f3 == 0.0f) { v_tmp.Z = 0.0f; // recalculate y to make equation work if (m12 != 0.0f) { v_tmp.Y = (float)(-f1 / f2); } } else { v_tmp.Z = (float)((f2 - f1) / f3); } } if (XMVector3.LengthSquare(v_tmp).X > 1e-5f) { return(XMVector3.Normalize(v_tmp)); } else { // Multiply by a value large enough to make the vector non-zero. v_tmp *= 1e5f; return(XMVector3.Normalize(v_tmp)); } }
public static XMVector CalculateEigenVector(float m11, float m12, float m13, float m22, float m23, float m33, float e) { XMVector v_tmp = new XMFloat3( (float)((m12 * m23) - (m13 * (m22 - e))), (float)((m13 * m12) - (m23 * (m11 - e))), (float)(((m11 - e) * (m22 - e)) - (m12 * m12))); // planar or linear if (XMVector3.Equal(v_tmp, XMGlobalConstants.Zero)) { float f1, f2, f3; // we only have one equation - find a valid one if ((m11 - e != 0.0f) || (m12 != 0.0f) || (m13 != 0.0f)) { f1 = m11 - e; f2 = m12; f3 = m13; } else if ((m12 != 0.0f) || (m22 - e != 0.0f) || (m23 != 0.0f)) { f1 = m12; f2 = m22 - e; f3 = m23; } else if ((m13 != 0.0f) || (m23 != 0.0f) || (m33 - e != 0.0f)) { f1 = m13; f2 = m23; f3 = m33 - e; } else { // error, we'll just make something up - we have NO context f1 = 1.0f; f2 = 0.0f; f3 = 0.0f; } if (f1 == 0.0f) { v_tmp.X = 0.0f; } else { v_tmp.X = 1.0f; } if (f2 == 0.0f) { v_tmp.Y = 0.0f; } else { v_tmp.Y = 1.0f; } if (f3 == 0.0f) { v_tmp.Z = 0.0f; // recalculate y to make equation work if (m12 != 0.0f) { v_tmp.Y = (float)(-f1 / f2); } } else { v_tmp.Z = (float)((f2 - f1) / f3); } } if (XMVector3.LengthSquare(v_tmp).X > 1e-5f) { return XMVector3.Normalize(v_tmp); } else { // Multiply by a value large enough to make the vector non-zero. v_tmp *= 1e5f; return XMVector3.Normalize(v_tmp); } }
public void CreateDeviceDependentResources(DeviceResources resources) { this.deviceResources = resources; var d3dDevice = this.deviceResources.D3DDevice; // Create the shaders byte[] renderParticlesVSBytecode = File.ReadAllBytes("ParticleDrawVS.cso"); this.g_pRenderParticlesVS = d3dDevice.CreateVertexShader(renderParticlesVSBytecode, null); this.g_pRenderParticlesGS = d3dDevice.CreateGeometryShader(File.ReadAllBytes("ParticleDrawGS.cso"), null); this.g_pRenderParticlesPS = d3dDevice.CreatePixelShader(File.ReadAllBytes("ParticleDrawPS.cso"), null); this.g_pCalcCS = d3dDevice.CreateComputeShader(File.ReadAllBytes("NBodyGravityCS.cso"), null); // Create our vertex input layout D3D11InputElementDesc[] layoutDesc = new D3D11InputElementDesc[] { new D3D11InputElementDesc { SemanticName = "COLOR", SemanticIndex = 0, Format = DxgiFormat.R32G32B32A32Float, InputSlot = 0, AlignedByteOffset = 0, InputSlotClass = D3D11InputClassification.PerVertexData, InstanceDataStepRate = 0 } }; this.g_pParticleVertexLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, renderParticlesVSBytecode); this.CreateParticleBuffer(); this.CreateParticlePosVeloBuffers(); // Setup constant buffer this.g_pcbGS = d3dDevice.CreateBuffer(new D3D11BufferDesc(ConstantBufferGS.Size, D3D11BindOptions.ConstantBuffer)); this.g_pcbCS = d3dDevice.CreateBuffer(new D3D11BufferDesc(ConstantBufferCS.Size, D3D11BindOptions.ConstantBuffer)); // Load the Particle Texture DdsDirectX.CreateTexture( "Particle.dds", this.deviceResources.D3DDevice, this.deviceResources.D3DContext, out this.g_pParticleTexRV); D3D11SamplerDesc SamplerDesc = D3D11SamplerDesc.Default; SamplerDesc.AddressU = D3D11TextureAddressMode.Clamp; SamplerDesc.AddressV = D3D11TextureAddressMode.Clamp; SamplerDesc.AddressW = D3D11TextureAddressMode.Clamp; SamplerDesc.Filter = D3D11Filter.MinMagMipLinear; this.g_pSampleStateLinear = d3dDevice.CreateSamplerState(SamplerDesc); D3D11BlendDesc BlendStateDesc = D3D11BlendDesc.Default; D3D11RenderTargetBlendDesc[] BlendStateDescRenderTargets = BlendStateDesc.GetRenderTargets(); BlendStateDescRenderTargets[0].IsBlendEnabled = true; BlendStateDescRenderTargets[0].BlendOperation = D3D11BlendOperation.Add; BlendStateDescRenderTargets[0].SourceBlend = D3D11BlendValue.SourceAlpha; BlendStateDescRenderTargets[0].DestinationBlend = D3D11BlendValue.One; BlendStateDescRenderTargets[0].BlendOperationAlpha = D3D11BlendOperation.Add; BlendStateDescRenderTargets[0].SourceBlendAlpha = D3D11BlendValue.Zero; BlendStateDescRenderTargets[0].DestinationBlendAlpha = D3D11BlendValue.Zero; BlendStateDescRenderTargets[0].RenderTargetWriteMask = D3D11ColorWriteEnables.All; BlendStateDesc.SetRenderTargets(BlendStateDescRenderTargets); this.g_pBlendingStateParticle = d3dDevice.CreateBlendState(BlendStateDesc); D3D11DepthStencilDesc DepthStencilDesc = D3D11DepthStencilDesc.Default; DepthStencilDesc.IsDepthEnabled = false; DepthStencilDesc.DepthWriteMask = D3D11DepthWriteMask.Zero; this.g_pDepthStencilState = d3dDevice.CreateDepthStencilState(DepthStencilDesc); XMFloat3 eye = new XMFloat3(-Spread * 2, Spread * 4, -Spread * 3); XMFloat3 at = new XMFloat3(0.0f, 0.0f, 0.0f); XMFloat3 up = new XMFloat3(0.0f, 1.0f, 0.0f); this.ViewMatrix = XMMatrix.LookAtLH(eye, at, up); }
public SimpleVertex(XMFloat3 pos, XMFloat2 tex) { this.Pos = pos; this.Tex = tex; }
public void SetLightDirection(XMFloat3 vDir) { m_vDefaultDir = vDir; m_vCurrentDir = vDir; }
public void Render() { var context = this.deviceResources.D3DContext; context.OutputMergerSetRenderTargets(new[] { this.deviceResources.D3DRenderTargetView }, this.deviceResources.D3DDepthStencilView); // Clear the render target and depth stencil context.ClearRenderTargetView(this.deviceResources.D3DRenderTargetView, new float[] { 0.0f, 0.25f, 0.25f, 0.55f }); context.ClearDepthStencilView(this.deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth, 1.0f, 0); // Get the projection & view matrix from the camera class XMMatrix mWorld = this.centerMesh * this.WorldMatrix; XMMatrix mView = this.ViewMatrix; XMMatrix mProj = this.ProjectionMatrix; XMMatrix mWorldViewProjection = mWorld * mView * mProj; XMFloat3 vLightDir = this.LightDirection; // Per frame cb update float fAmbient = 0.1f; ConstantBufferPSPerFrame cbPSPerFrame; cbPSPerFrame.m_vLightDirAmbient = new XMFloat4(vLightDir.X, vLightDir.Y, vLightDir.Z, fAmbient); context.UpdateSubresource(this.constantBufferPSPerFrame, 0, null, cbPSPerFrame, 0, 0); context.PixelShaderSetConstantBuffers(ConstantBufferPSPerFrameBind, new[] { this.constantBufferPSPerFrame }); // IA setup context.InputAssemblerSetInputLayout(this.inputLayout); // Set the shaders context.VertexShaderSetShader(this.vertexShader, null); context.PixelShaderSetShader(this.pixelShader, null); // Set the per object constant data // VS Per object ConstantBufferVSPerObject cbVSPerObject; cbVSPerObject.m_WorldViewProj = mWorldViewProjection.Transpose(); cbVSPerObject.m_World = mWorld.Transpose(); context.UpdateSubresource(this.constantBufferVSPerObject, 0, null, cbVSPerObject, 0, 0); context.VertexShaderSetConstantBuffers(ConstantBufferVSPerObjectBind, new[] { this.constantBufferVSPerObject }); // PS Per object ConstantBufferPSPerObject cbPSPerObject; cbPSPerObject.m_vObjectColor = new XMFloat4(1, 1, 1, 1); context.UpdateSubresource(this.constantBufferPSPerObject, 0, null, cbPSPerObject, 0, 0); context.PixelShaderSetConstantBuffers(ConstantBufferPSPerObjectBind, new[] { this.constantBufferPSPerObject }); // Set render resources context.PixelShaderSetSamplers(0, new[] { this.sampler }); // Render //// Get the mesh //context.InputAssemblerSetVertexBuffers( // 0, // new[] { this.mesh.Meshes[0].VertexBuffers[0].Buffer }, // new[] { this.mesh.Meshes[0].VertexBuffers[0].StrideBytes }, // new[] { 0U }); //context.InputAssemblerSetIndexBuffer( // this.mesh.Meshes[0].IndexBuffer.Buffer, // this.mesh.Meshes[0].IndexBuffer.IndexFormat, // 0); //for (int subsetIndex = 0; subsetIndex < this.mesh.Meshes.Count; subsetIndex++) //{ // // Get the subset // SdkMeshSubset subset = this.mesh.Meshes[0].Subsets[subsetIndex]; // context.InputAssemblerSetPrimitiveTopology(subset.PrimitiveTopology); // D3D11ShaderResourceView pDiffuseRV = this.mesh.Materials[subset.MaterialIndex].DiffuseTextureView; // context.PixelShaderSetShaderResources(0, new[] { pDiffuseRV }); // context.DrawIndexed((uint)subset.IndexCount, 0, subset.VertexStart); //} this.mesh.Render(0, -1, -1); }
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)); }