Example #1
0
 public SimpleVertex(XMFloat3 pos, XMFloat4 color)
 {
     this.Pos   = pos;
     this.Color = color;
 }
Example #2
0
        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;
            }
        }
Example #3
0
 public SceneVertex(XMFloat4 pos, XMFloat4 color)
 {
     this.Pos   = pos;
     this.Color = color;
 }
        public void CreateDeviceDependentResources(DeviceResources resources)
        {
            this.deviceResources = resources;

            var d3dDevice = this.deviceResources.D3DDevice;

            this.tessellator.CreateDeviceDependentResources(this.deviceResources);

            XMFloat4[] initData;

            // Parse the .obj file. Both triangle faces and quad faces are supported.
            // Only v and f tags are processed, other tags like vn, vt etc are ignored.
            {
                var initFile = ObjFile.FromFile("BaseMesh.obj");
                var data     = new List <XMFloat4>();
                var v        = new List <XMFloat4>();

                for (int i = 0; i < initFile.Vertices.Count; i++)
                {
                    ObjVector4 objPosition = initFile.Vertices[i].Position;
                    XMFloat4   pos         = new XMFloat4(
                        objPosition.X,
                        objPosition.Y,
                        objPosition.Z,
                        1.0f);

                    v.Add(pos);
                }

                foreach (ObjFace face in initFile.Faces)
                {
                    if (face.Vertices.Count < 3)
                    {
                        continue;
                    }

                    data.Add(v[face.Vertices[0].Vertex - 1]);
                    data.Add(v[face.Vertices[1].Vertex - 1]);
                    data.Add(v[face.Vertices[2].Vertex - 1]);

                    if (face.Vertices.Count >= 4)
                    {
                        data.Add(v[face.Vertices[2].Vertex - 1]);
                        data.Add(v[face.Vertices[3].Vertex - 1]);
                        data.Add(v[face.Vertices[0].Vertex - 1]);
                    }
                }

                initData = data.ToArray();
            }

            this.g_pBaseVB = d3dDevice.CreateBuffer(
                D3D11BufferDesc.From(initData, D3D11BindOptions.ShaderResource | D3D11BindOptions.VertexBuffer),
                initData,
                0,
                0);

            this.tessellator.SetBaseMesh((uint)initData.Length, this.g_pBaseVB);

            this.g_pVS = d3dDevice.CreateVertexShader(File.ReadAllBytes("RenderVertexShader.cso"), null);

            {
                byte[] shaderBytecode = File.ReadAllBytes("RenderBaseVertexShader.cso");
                this.g_pBaseVS = d3dDevice.CreateVertexShader(shaderBytecode, null);

                D3D11InputElementDesc[] layoutDesc = new[]
                {
                    new D3D11InputElementDesc(
                        "POSITION",
                        0,
                        DxgiFormat.R32G32B32A32Float,
                        0,
                        0,
                        D3D11InputClassification.PerVertexData,
                        0)
                };

                this.g_pBaseVBLayout = d3dDevice.CreateInputLayout(layoutDesc, shaderBytecode);
            }

            this.g_pPS = d3dDevice.CreatePixelShader(File.ReadAllBytes("RenderPixelShader.cso"), null);

            // Setup constant buffer
            this.g_pVSCB = d3dDevice.CreateBuffer(new D3D11BufferDesc
            {
                BindOptions = D3D11BindOptions.ConstantBuffer,
                ByteWidth   = 4 * 16
            });

            // Rasterizer state
            this.g_pRasWireFrame = d3dDevice.CreateRasterizerState(new D3D11RasterizerDesc
            {
                CullMode = D3D11CullMode.None,
                FillMode = D3D11FillMode.WireFrame
            });
        }
Example #5
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);
        }