Пример #1
0
        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;
        }
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.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
                }
            };

            this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode);

            byte[] pixelShaderBytecode = File.ReadAllBytes("PixelShader.cso");
            this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            pixelShaderBytecode   = File.ReadAllBytes("PixelShaderSolid.cso");
            this.pixelShaderSolid = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            var vertexBufferDesc = D3D11BufferDesc.From(MainGameComponent.Vertices, D3D11BindOptions.VertexBuffer);

            this.vertexBuffer = this.deviceResources.D3DDevice.CreateBuffer(vertexBufferDesc, MainGameComponent.Vertices, 0, 0);

            var indexBufferDesc = D3D11BufferDesc.From(MainGameComponent.Indices, D3D11BindOptions.IndexBuffer);

            this.indexBuffer = this.deviceResources.D3DDevice.CreateBuffer(indexBufferDesc, MainGameComponent.Indices, 0, 0);

            var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer);

            this.constantBuffer = this.deviceResources.D3DDevice.CreateBuffer(constantBufferDesc);

            this.worldMatrix = XMMatrix.Identity;

            XMVector eye = new XMVector(0.0f, 4.0f, -10.0f, 0.0f);
            XMVector at  = new XMVector(0.0f, 1.0f, 0.0f, 0.0f);
            XMVector up  = new XMVector(0.0f, 1.0f, 0.0f, 0.0f);

            this.viewMatrix = XMMatrix.LookAtLH(eye, at, up);
        }
        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;
        }
Пример #5
0
        public MainGameComponent()
        {
            XMVector eye = new(0.0f, 0.0f, -5.0f, 0.0f);
            XMVector at  = new(0.0f, 0.0f, -0.0f, 0.0f);
            XMVector up  = new(0.0f, 1.0f, 0.0f, 0.0f);

            this.ViewMatrix = XMMatrix.LookAtLH(eye, at, up);

            this.WorldMatrix = XMMatrix.Identity;
        }
Пример #6
0
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            this.scene.CreateDeviceDependentResources(this.deviceResources);
            this.oit.CreateDeviceDependentResources(this.deviceResources);

            this.worldMatrix = XMMatrix.Identity;

            XMVector eye = new XMVector(0.0f, 0.5f, -3.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);

            this.viewMatrix = XMMatrix.LookAtLH(eye, at, up);
        }
        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 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;
        }
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.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
                }
            };

            this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode);

            byte[] pixelShaderBytecode = File.ReadAllBytes("PixelShader.cso");
            this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            var vertexBufferDesc = new D3D11BufferDesc(MaxVertices * 12, D3D11BindOptions.VertexBuffer);

            this.vertexBuffer = this.deviceResources.D3DDevice.CreateBuffer(vertexBufferDesc);

            var indexBufferDesc = new D3D11BufferDesc(MaxIndices * 2, D3D11BindOptions.IndexBuffer);

            this.indexBuffer = this.deviceResources.D3DDevice.CreateBuffer(indexBufferDesc);

            var constantBufferDesc = new D3D11BufferDesc(ConstantBufferData.Size, D3D11BindOptions.ConstantBuffer);

            this.constantBuffer = this.deviceResources.D3DDevice.CreateBuffer(constantBufferDesc);

            this.worldMatrix = XMMatrix.Identity;

            XMVector vecAt  = this.cameraOrigins[(int)this.CollisionGroup];
            XMVector vecEye = new(vecAt.X, vecAt.Y + 20.0f, (this.CollisionGroup == CollisionGroup.Frustum) ? (vecAt.Z + 20.0f) : (vecAt.Z - 20.0f), 0.0f);
            XMVector vecUp  = new(0.0f, 1.0f, 0.0f, 0.0f);

            this.viewMatrix = XMMatrix.LookAtLH(vecEye, vecAt, vecUp);
        }
        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;
        }
        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;
        }
Пример #12
0
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            byte[] renderSceneVertexShaderBytecode = File.ReadAllBytes("RenderSceneVertexShader.cso");
            this.g_pSceneVS = this.deviceResources.D3DDevice.CreateVertexShader(renderSceneVertexShaderBytecode, 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         = "TEXTURE",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 24,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.g_pSceneVertexLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, renderSceneVertexShaderBytecode);

            byte[] renderScenePixelShaderBytecode = File.ReadAllBytes("RenderScenePixelShader.cso");
            this.g_pScenePS = this.deviceResources.D3DDevice.CreatePixelShader(renderScenePixelShaderBytecode, null);

            byte[] renderSceneShadowMapVertexShaderBytecode = File.ReadAllBytes("RenderSceneShadowMapVertexShader.cso");
            this.g_pShadowMapVS = this.deviceResources.D3DDevice.CreateVertexShader(renderSceneShadowMapVertexShaderBytecode, null);

            this.g_SceneMesh = SdkMeshFile.FromFile(this.deviceResources.D3DDevice, this.deviceResources.D3DContext, @"ColumnScene\scene.sdkmesh");
            this.g_Poles     = SdkMeshFile.FromFile(this.deviceResources.D3DDevice, this.deviceResources.D3DContext, @"ColumnScene\poles.sdkmesh");

            this.g_pcbConstants = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(ConstantBufferConstants.Size, D3D11BindOptions.ConstantBuffer));

            D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc(
                D3D11Filter.ComparisonMinMagMipPoint,
                D3D11TextureAddressMode.Border,
                D3D11TextureAddressMode.Border,
                D3D11TextureAddressMode.Border,
                0.0f,
                1,
                D3D11ComparisonFunction.LessEqual,
                new float[] { 1.0f, 1.0f, 1.0f, 1.0f },
                0.0f,
                float.MaxValue);

            // PointCmp
            this.g_pSamplePointCmp = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc);

            // Point
            samplerDesc.Filter             = D3D11Filter.MinMagMipPoint;
            samplerDesc.ComparisonFunction = D3D11ComparisonFunction.Always;
            this.g_pSamplePoint            = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc);

            // Linear
            samplerDesc.Filter   = D3D11Filter.MinMagMipLinear;
            samplerDesc.AddressU = D3D11TextureAddressMode.Wrap;
            samplerDesc.AddressV = D3D11TextureAddressMode.Wrap;
            samplerDesc.AddressW = D3D11TextureAddressMode.Wrap;
            this.g_pSampleLinear = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc);

            // Create a blend state to disable alpha blending
            D3D11BlendDesc blendDesc = D3D11BlendDesc.Default;

            blendDesc.IsIndependentBlendEnabled = false;
            D3D11RenderTargetBlendDesc[] blendDescRenderTargets = blendDesc.GetRenderTargets();
            blendDescRenderTargets[0].IsBlendEnabled = false;

            blendDescRenderTargets[0].RenderTargetWriteMask = D3D11ColorWriteEnables.All;
            blendDesc.SetRenderTargets(blendDescRenderTargets);
            this.g_pBlendStateNoBlend = this.deviceResources.D3DDevice.CreateBlendState(blendDesc);

            blendDescRenderTargets[0].RenderTargetWriteMask = D3D11ColorWriteEnables.None;
            blendDesc.SetRenderTargets(blendDescRenderTargets);
            this.g_pBlendStateColorWritesOff = this.deviceResources.D3DDevice.CreateBlendState(blendDesc);

            // textures / rts
            D3D11Texture2DDesc TDesc = new D3D11Texture2DDesc(
                DxgiFormat.R16Typeless,
                (uint)g_fShadowMapWidth,
                (uint)g_fShadowMapHeight,
                1,
                1,
                D3D11BindOptions.DepthStencil | D3D11BindOptions.ShaderResource);

            this.g_pShadowMapDepthStencilTexture = this.deviceResources.D3DDevice.CreateTexture2D(TDesc);

            D3D11ShaderResourceViewDesc SRVDesc = new D3D11ShaderResourceViewDesc
            {
                Format        = DxgiFormat.R16UNorm,
                ViewDimension = D3D11SrvDimension.Texture2D,
                Texture2D     = new D3D11Texture2DSrv
                {
                    MipLevels       = 1,
                    MostDetailedMip = 0
                }
            };

            this.g_pDepthTextureSRV = this.deviceResources.D3DDevice.CreateShaderResourceView(this.g_pShadowMapDepthStencilTexture, SRVDesc);

            D3D11DepthStencilViewDesc DSVDesc = new D3D11DepthStencilViewDesc
            {
                Format        = DxgiFormat.D16UNorm,
                ViewDimension = D3D11DsvDimension.Texture2D,
                Options       = D3D11DepthStencilViewOptions.None,
                Texture2D     = new D3D11Texture2DDsv
                {
                    MipSlice = 0
                }
            };

            this.g_pDepthStencilTextureDSV = this.deviceResources.D3DDevice.CreateDepthStencilView(this.g_pShadowMapDepthStencilTexture, DSVDesc);

            XMFloat3 vecEye = new XMFloat3(0.95f, 5.83f, -14.48f);
            XMFloat3 vecAt  = new XMFloat3(0.90f, 5.44f, -13.56f);
            XMFloat3 vecUp  = new XMFloat3(0.0f, 1.0f, 0.0f);

            this.ViewMatrix  = XMMatrix.LookAtLH(vecEye, vecAt, vecUp);
            this.WorldMatrix = XMMatrix.Identity;

            XMFloat3 vecEyeL = new XMFloat3(0, 0, 0);
            XMFloat3 vecAtL  = new XMFloat3(0, -0.5f, 1);
            XMFloat3 vecUUpL = new XMFloat3(0.0f, 1.0f, 0.0f);

            this.LightViewMatrix  = XMMatrix.LookAtLH(vecEyeL, vecAtL, vecUUpL);
            this.LightWorldMatrix = XMMatrix.Identity;
        }
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.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         = "TEXCOORD",
                    SemanticIndex        = 0,
                    Format               = DxgiFormat.R32G32Float,
                    InputSlot            = 0,
                    AlignedByteOffset    = 12,
                    InputSlotClass       = D3D11InputClassification.PerVertexData,
                    InstanceDataStepRate = 0
                }
            };

            this.inputLayout = this.deviceResources.D3DDevice.CreateInputLayout(layoutDesc, vertexShaderBytecode);

            byte[] pixelShaderBytecode = File.ReadAllBytes("PixelShader.cso");
            this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            var vertexBufferDesc = D3D11BufferDesc.From(MainGameComponent.Vertices, D3D11BindOptions.VertexBuffer);

            this.vertexBuffer = this.deviceResources.D3DDevice.CreateBuffer(vertexBufferDesc, MainGameComponent.Vertices, 0, 0);

            var indexBufferDesc = D3D11BufferDesc.From(MainGameComponent.Indices, D3D11BindOptions.IndexBuffer);

            this.indexBuffer = this.deviceResources.D3DDevice.CreateBuffer(indexBufferDesc, MainGameComponent.Indices, 0, 0);

            this.constantBufferNeverChanges = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(ConstantBufferNeverChangesData.Size, D3D11BindOptions.ConstantBuffer));

            this.constantBufferChangesOnResize = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(ConstantBufferChangesOnResizeData.Size, D3D11BindOptions.ConstantBuffer));

            this.constantBufferChangesEveryFrame = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(ConstantBufferChangesEveryFrameData.Size, D3D11BindOptions.ConstantBuffer));

            DdsDirectX.CreateTexture(
                "seafloor.dds",
                this.deviceResources.D3DDevice,
                this.deviceResources.D3DContext,
                out this.textureView);

            D3D11SamplerDesc samplerDesc = new D3D11SamplerDesc(
                D3D11Filter.MinMagMipLinear,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                D3D11TextureAddressMode.Wrap,
                0.0f,
                0,
                D3D11ComparisonFunction.Never,
                new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
                0.0f,
                float.MaxValue);

            this.sampler = this.deviceResources.D3DDevice.CreateSamplerState(samplerDesc);

            this.worldMatrix = XMMatrix.Identity;

            XMVector eye = new XMVector(0.0f, 3.0f, -6.0f, 0.0f);
            XMVector at  = new XMVector(0.0f, 1.0f, 0.0f, 0.0f);
            XMVector up  = new XMVector(0.0f, 1.0f, 0.0f, 0.0f);

            this.viewMatrix = XMMatrix.LookAtLH(eye, at, up);

            ConstantBufferNeverChangesData cbNeverChanges;

            cbNeverChanges.View = this.viewMatrix.Transpose();
            this.deviceResources.D3DContext.UpdateSubresource(this.constantBufferNeverChanges, 0, null, cbNeverChanges, 0, 0);
        }
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            byte[] vertexShaderBytecode = File.ReadAllBytes("VertexShader.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("PixelShader.cso");
            this.pixelShader = this.deviceResources.D3DDevice.CreatePixelShader(pixelShaderBytecode, null);

            this.constantBufferPerObject = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(ConstantBufferPerObject.Size, D3D11BindOptions.ConstantBuffer));

            this.constantBufferPerFrame = this.deviceResources.D3DDevice.CreateBuffer(
                new D3D11BufferDesc(ConstantBufferPerFrame.Size, D3D11BindOptions.ConstantBuffer));

            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);

            DdsDirectX.CreateTexture(
                "seafloor.dds",
                this.deviceResources.D3DDevice,
                this.deviceResources.D3DContext,
                out this.textureView);

            this.mesh = SdkMeshFile.FromFile(
                this.deviceResources.D3DDevice,
                this.deviceResources.D3DContext,
                "ball.sdkmesh");

            XMVector eye = new XMVector(0.0f, 0.0f, -5.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);

            this.ViewMatrix  = XMMatrix.LookAtLH(eye, at, up);
            this.WorldMatrix = XMMatrix.Identity;
        }
Пример #15
0
        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;
        }
Пример #16
0
        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);
        }
Пример #17
0
        private void RenderShadowMap(out XMFloat4X4 mViewProjLight, out XMFloat3 vLightDir)
        {
            var context = this.deviceResources.D3DContext;

            D3D11Rect[]     oldRects = context.RasterizerStageGetScissorRects();
            D3D11Viewport[] oldVp    = context.RasterizerStageGetViewports();

            D3D11Rect[] rects = new D3D11Rect[1] {
                new D3D11Rect(0, (int)g_fShadowMapWidth, 0, (int)g_fShadowMapHeight)
            };
            context.RasterizerStageSetScissorRects(rects);

            D3D11Viewport[] vp = new D3D11Viewport[1] {
                new D3D11Viewport(0, 0, g_fShadowMapWidth, g_fShadowMapHeight, 0.0f, 1.0f)
            };
            context.RasterizerStageSetViewports(vp);

            // Set our scene render target & keep original depth buffer
            D3D11RenderTargetView[] pRTVs = new D3D11RenderTargetView[2];
            context.OutputMergerSetRenderTargets(pRTVs, this.g_pDepthStencilTextureDSV);

            // Clear the render target
            context.ClearDepthStencilView(this.g_pDepthStencilTextureDSV, D3D11ClearOptions.Depth | D3D11ClearOptions.Stencil, 1.0f, 0);

            // Get the projection & view matrix from the camera class
            XMFloat3 up           = new XMFloat3(0, 1, 0);
            XMFloat4 vLight       = new XMFloat4(0.0f, 0.0f, 0.0f, 1.0f);
            XMFloat4 vLightLookAt = new XMFloat4(0.0f, -0.5f, 1.0f, 0.0f);

            vLightLookAt = vLight.ToVector() + vLightLookAt.ToVector();
            vLight       = XMVector4.Transform(vLight, this.LightWorldMatrix);
            vLightLookAt = XMVector4.Transform(vLightLookAt, this.LightWorldMatrix);

            vLightDir = XMVector.Subtract(vLightLookAt, vLight);

            XMMatrix mProj = XMMatrix.OrthographicOffCenterLH(-8.5f, 9, -15, 11, -20, 20);
            XMMatrix mView = XMMatrix.LookAtLH(vLight, vLightLookAt, up);

            mViewProjLight = mView * mProj;

            // Setup the constant buffer for the scene vertex shader
            ConstantBufferConstants pConstants = new ConstantBufferConstants
            {
                WorldViewProjection = mViewProjLight.ToMatrix().Transpose(),
                WorldViewProjLight  = mViewProjLight.ToMatrix().Transpose(),
                ShadowMapDimensions = new XMFloat4(
                    g_fShadowMapWidth,
                    g_fShadowMapHeight,
                    1.0f / g_fShadowMapWidth,
                    1.0f / g_fShadowMapHeight),
                LightDir = new XMFloat4(vLightDir.X, vLightDir.Y, vLightDir.Z, 0.0f),
                SunWidth = this.SunWidth
            };

            context.UpdateSubresource(this.g_pcbConstants, 0, null, pConstants, 0, 0);
            context.VertexShaderSetConstantBuffers(g_iConstantsConstantBufferBind, new[] { this.g_pcbConstants });
            context.PixelShaderSetConstantBuffers(g_iConstantsConstantBufferBind, new[] { this.g_pcbConstants });

            // Set the shaders
            context.VertexShaderSetShader(this.g_pShadowMapVS, null);
            context.PixelShaderSetShader(null, null);

            // Set the vertex buffer format
            context.InputAssemblerSetInputLayout(this.g_pSceneVertexLayout);

            // Render the scene
            this.g_Poles.Render(0, -1, -1);

            // reset the old viewport etc.
            context.RasterizerStageSetScissorRects(oldRects);
            context.RasterizerStageSetViewports(oldVp);
        }