Пример #1
0
        public void Render()
        {
            var context = this.deviceResources.D3DContext;

            // Clear the render target and the depth stencil
            context.OutputMergerSetRenderTargets(new[] { this.deviceResources.D3DRenderTargetView }, this.deviceResources.D3DDepthStencilView);
            context.ClearRenderTargetView(this.deviceResources.D3DRenderTargetView, new float[] { 0.176f, 0.196f, 0.667f, 1.0f });
            context.ClearDepthStencilView(this.deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth, 1.0f, 0);

            // Get the projection & view matrix from the camera class
            XMMatrix mWorld = this.WorldMatrix;
            XMMatrix mView  = this.ViewMatrix;
            XMMatrix mProj  = this.ProjectionMatrix;
            XMMatrix mWorldViewProjection = mWorld * mView * mProj;

            // Set the constant buffers
            PerFrameConstantBuffer pVSPerFrame;

            pVSPerFrame.LightDir     = new XMFloat3(0.0f, 0.707f, -0.707f);
            pVSPerFrame.Time         = (float)this.totalSeconds;
            pVSPerFrame.LightDiffuse = new XMFloat4(1.0f, 1.0f, 1.0f, 1.0f);
            context.UpdateSubresource(this.perFrameConstantBuffer, 0, null, pVSPerFrame, 0, 0);

            PerObjectConstantBuffer pVSPerObject;

            pVSPerObject.WorldViewProjection = mWorldViewProjection.Transpose();
            pVSPerObject.World = mWorld.Transpose();
            pVSPerObject.MaterialAmbientColor = new XMFloat4(0.3f, 0.3f, 0.3f, 1.0f);
            pVSPerObject.MaterialDiffuseColor = new XMFloat4(0.7f, 0.7f, 0.7f, 1.0f);
            context.UpdateSubresource(this.perObjectConstantBuffer, 0, null, pVSPerObject, 0, 0);

            context.VertexShaderSetConstantBuffers(0, new[] { this.perObjectConstantBuffer, this.perFrameConstantBuffer });

            // Set render resources
            context.InputAssemblerSetInputLayout(this.inputLayout);
            context.VertexShaderSetShader(this.vertexShader, null);
            context.PixelShaderSetShader(this.pixelShader, null);
            context.PixelShaderSetSamplers(0, new[] { this.samplerLinear });

            // Render objects here...
        }
        public void Render()
        {
            var context = this.deviceResources.D3DContext;

            context.OutputMergerSetRenderTargets(new[] { this.deviceResources.D3DRenderTargetView }, this.deviceResources.D3DDepthStencilView);

            // Clear the depth stencil
            context.ClearRenderTargetView(this.deviceResources.D3DRenderTargetView, new float[] { 0.627f, 0.627f, 0.980f, 1.0f });
            context.ClearDepthStencilView(this.deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth, 1.0f, 0);

            // Get the projection & view matrix from the camera class
            XMMatrix mWorld = this.WorldMatrix;
            XMMatrix mView  = this.ViewMatrix;
            XMMatrix mProj  = this.ProjectionMatrix;
            XMMatrix mWorldViewProjection = mWorld * mView * mProj;

            // Set the constant buffers
            ConstantBufferPerObject cbPerObject;

            cbPerObject.m_mWorldViewProjection = mWorldViewProjection.Transpose();
            cbPerObject.m_mWorld = mWorld;
            context.UpdateSubresource(this.constantBufferPerObject, 0, null, cbPerObject, 0, 0);

            ConstantBufferPerFrame cbPerFrame;

            cbPerFrame.m_vLightDir = new XMFloat4(0.0f, 0.707f, -0.707f, 0.0f);
            context.UpdateSubresource(this.constantBufferPerFrame, 0, null, cbPerFrame, 0, 0);

            context.VertexShaderSetConstantBuffers(0, new[] { this.constantBufferPerObject, this.constantBufferPerFrame });

            // Set render resources
            context.InputAssemblerSetInputLayout(this.inputLayout);
            context.VertexShaderSetShader(this.vertexShader, null);
            context.PixelShaderSetShader(this.pixelShader, null);
            context.PixelShaderSetShaderResources(0, new[] { this.textureView });
            context.PixelShaderSetSamplers(0, new[] { this.sampler });

            this.mesh.Render(-1, -1, -1);
        }
        public void Render()
        {
            var context = this.deviceResources.D3DContext;

            context.OutputMergerSetRenderTargets(new[] { this.deviceResources.D3DRenderTargetView }, this.deviceResources.D3DDepthStencilView);
            context.ClearRenderTargetView(this.deviceResources.D3DRenderTargetView, new float[] { 0.0f, 0.125f, 0.3f, 1.0f });
            context.ClearDepthStencilView(this.deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth, 1.0f, 0);

            context.InputAssemblerSetInputLayout(this.inputLayout);

            context.InputAssemblerSetVertexBuffers(
                0,
                new[] { this.vertexBuffer },
                new uint[] { SimpleVertex.Size },
                new uint[] { 0 });

            context.InputAssemblerSetIndexBuffer(this.indexBuffer, DxgiFormat.R16UInt, 0);
            context.InputAssemblerSetPrimitiveTopology(D3D11PrimitiveTopology.TriangleList);

            // Update matrix variables and lighting variables
            ConstantBufferData cb;

            cb.World      = this.worldMatrix.Transpose();
            cb.View       = this.viewMatrix.Transpose();
            cb.Projection = this.projectionMatrix.Transpose();

            cb.LightDir   = new XMFloat4[2];
            cb.LightColor = new XMFloat4[2];

            for (int i = 0; i < 2; i++)
            {
                cb.LightDir[i]   = this.lightDirs[i];
                cb.LightColor[i] = this.lightColors[i];
            }

            //cb.LightDir0 = this.lightDirs[0];
            //cb.LightDir1 = this.lightDirs[1];
            //cb.LightColor0 = this.lightColors[0];
            //cb.LightColor1 = this.lightColors[1];

            cb.OutputColor = new XMFloat4(0, 0, 0, 0);
            context.UpdateSubresource(this.constantBuffer, 0, null, cb, 0, 0);

            // Render the cube
            context.VertexShaderSetShader(this.vertexShader, null);
            context.VertexShaderSetConstantBuffers(0, new[] { this.constantBuffer });
            context.PixelShaderSetShader(this.pixelShader, null);
            context.PixelShaderSetConstantBuffers(0, new[] { this.constantBuffer });
            context.DrawIndexed((uint)MainGameComponent.Indices.Length, 0, 0);

            // Render each light
            context.PixelShaderSetShader(this.pixelShaderSolid, null);

            for (int m = 0; m < 2; m++)
            {
                XMMatrix light      = XMMatrix.TranslationFromVector(5.0f * this.lightDirs[m].ToVector());
                XMMatrix lightScale = XMMatrix.Scaling(0.2f, 0.2f, 0.2f);
                light = lightScale * light;

                // Update the world variable to reflect the current light
                cb.World       = light.Transpose();
                cb.OutputColor = this.lightColors[m];
                context.UpdateSubresource(this.constantBuffer, 0, null, cb, 0, 0);

                context.DrawIndexed((uint)MainGameComponent.Indices.Length, 0, 0);
            }
        }
        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 Render()
        {
            var context = this.deviceResources.D3DContext;

            // WVP
            XMMatrix mProj           = this.ProjectionMatrix;
            XMMatrix mView           = this.ViewMatrix;
            XMMatrix mViewProjection = mView * mProj;

            // Update per-frame variables
            ConstantBufferConstants cb;

            cb.ViewProjection     = mViewProjection.Transpose();
            cb.CameraPosWorld     = this.EyePt;
            cb.TessellationFactor = this.Subdivs;
            context.UpdateSubresource(this.g_pcbPerFrame, 0, null, cb, 0, 0);

            // Clear the render target and depth stencil
            context.OutputMergerSetRenderTargets(new[] { this.deviceResources.D3DRenderTargetView }, this.deviceResources.D3DDepthStencilView);
            context.ClearRenderTargetView(this.deviceResources.D3DRenderTargetView, new float[] { 0.05f, 0.05f, 0.05f, 1.0f });
            context.ClearDepthStencilView(this.deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth, 1.0f, 0);

            // Set state for solid rendering
            context.RasterizerStageSetState(this.g_pRasterizerStateSolid);

            // Render the meshes
            // Bind all of the CBs
            context.VertexShaderSetConstantBuffers(g_iBindPerFrame, new[] { this.g_pcbPerFrame });
            context.HullShaderSetConstantBuffers(g_iBindPerFrame, new[] { this.g_pcbPerFrame });
            context.DomainShaderSetConstantBuffers(g_iBindPerFrame, new[] { this.g_pcbPerFrame });
            context.PixelShaderSetConstantBuffers(g_iBindPerFrame, new[] { this.g_pcbPerFrame });

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

            // For this sample, choose either the "integer", "fractional_even",
            // or "fractional_odd" hull shader
            switch (this.PartitionMode)
            {
            case PartitionMode.Integer:
            default:
                context.HullShaderSetShader(this.g_pHullShaderInteger, null);
                break;

            case PartitionMode.FractionalEven:
                context.HullShaderSetShader(this.g_pHullShaderFracEven, null);
                break;

            case PartitionMode.FractionalOdd:
                context.HullShaderSetShader(this.g_pHullShaderFracOdd, null);
                break;
            }

            context.DomainShaderSetShader(this.g_pDomainShader, null);
            context.GeometryShaderSetShader(null, null);
            context.PixelShaderSetShader(this.g_pPixelShader, null);

            // Optionally draw the wireframe
            if (this.DrawWires)
            {
                context.PixelShaderSetShader(this.g_pSolidColorPS, null);
                context.RasterizerStageSetState(this.g_pRasterizerStateWireframe);
            }

            // Set the input assembler
            // This sample uses patches with 16 control points each
            // Although the Mobius strip only needs to use a vertex buffer,
            // you can use an index buffer as well by calling IASetIndexBuffer().
            context.InputAssemblerSetInputLayout(this.g_pPatchLayout);
            context.InputAssemblerSetVertexBuffers(0, new[] { this.g_pControlPointVB }, new[] { BezierControlPoint.Size }, new[] { 0U });
            context.InputAssemblerSetPrimitiveTopology(D3D11PrimitiveTopology.PatchList16ControlPoint);

            // Draw the mesh
            context.Draw((uint)MobiusStrip.Points.Length, 0);

            context.RasterizerStageSetState(this.g_pRasterizerStateSolid);
        }
Пример #6
0
        public void Render()
        {
            var context = this.deviceResources.D3DContext;

            D3D11RenderTargetView[]   pRTV = new D3D11RenderTargetView[2];
            D3D11ShaderResourceView[] pSRV = new D3D11ShaderResourceView[8];

            // Array of our samplers
            D3D11SamplerState[] ppSamplerStates = new D3D11SamplerState[3] {
                this.g_pSamplePoint, this.g_pSampleLinear, this.g_pSamplePointCmp
            };
            context.PixelShaderSetSamplers(0, ppSamplerStates);

            // Store off original render target, this is the back buffer of the swap chain
            D3D11RenderTargetView pOrigRTV = this.deviceResources.D3DRenderTargetView;
            D3D11DepthStencilView pOrigDSV = this.deviceResources.D3DDepthStencilView;

            // Clear the render target
            float[] ClearColor = { 0.0f, 0.25f, 0.25f, 1.0f };
            context.ClearRenderTargetView(this.deviceResources.D3DRenderTargetView, ClearColor);
            context.ClearDepthStencilView(this.deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth | D3D11ClearOptions.Stencil, 1.0f, 0);

            // disable color writes
            context.OutputMergerSetBlendState(this.g_pBlendStateColorWritesOff, null, 0xffffffff);

            this.RenderShadowMap(out XMFloat4X4 mViewProjLight, out XMFloat3 vLightDir);

            // enable color writes
            context.OutputMergerSetBlendState(this.g_pBlendStateNoBlend, null, 0xffffffff);

            // Get the projection & view matrix from the camera class
            XMMatrix mView = this.ViewMatrix;
            XMMatrix mProj = this.ProjectionMatrix;
            XMMatrix mWorldViewProjection = mView * mProj;

            // Setup the constant buffer for the scene vertex shader
            ConstantBufferConstants pConstants = new ConstantBufferConstants
            {
                WorldViewProjection = mWorldViewProjection.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_pSceneVS, null);
            context.PixelShaderSetShader(this.g_pScenePS, null);

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

            // Rebind to original back buffer and depth buffer
            pRTV[0] = pOrigRTV;
            context.OutputMergerSetRenderTargets(pRTV, pOrigDSV);

            // set the shadow map
            context.PixelShaderSetShaderResources(1, new[] { this.g_pDepthTextureSRV });

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

            // restore resources
            context.PixelShaderSetShaderResources(0, pSRV);
        }