Exemple #1
0
        public void Render()
        {
            // Clear the views
            GraphicsDevice.Instance.ClearAndSetMainRenderTarget();
            GraphicsDevice.Instance.SetRenderTargetViews();

            Camera.BackBufferResolution = new Vector2(GraphicsDevice.Instance.m_viewport.Width, GraphicsDevice.Instance.m_viewport.Height);

            Camera.SetMatrices();

            DeviceContext deviceContext = GraphicsDevice.Instance.Context;

            PerRenderConstantBuffer perRenderConstantBufferData = new PerRenderConstantBuffer();

            perRenderConstantBufferData.ViewProject    = Matrix.Multiply(Camera.ViewMatrix, Camera.ProjectionMatrix);
            perRenderConstantBufferData.ViewPosition.X = Camera.CameraPosition.X;
            perRenderConstantBufferData.ViewPosition.Y = Camera.CameraPosition.Y;
            perRenderConstantBufferData.ViewPosition.Z = Camera.CameraPosition.Z;

            perRenderConstantBufferData.ViewProject.Transpose();
            perRenderConstantBufferData.LightColour    = LightSettings.LightColour;
            perRenderConstantBufferData.LightDirection = LightSettings.LightDirection;
            deviceContext.UpdateSubresource(ref perRenderConstantBufferData, perRenderConstantBuffer);

            deviceContext.InputAssembler.InputLayout       = inputLayout;
            deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            deviceContext.VertexShader.SetConstantBuffer(0, perRenderConstantBuffer);
            deviceContext.VertexShader.SetConstantBuffer(1, perObjectConstantBuffer);
            deviceContext.VertexShader.Set(vertexShader);

            deviceContext.PixelShader.SetConstantBuffer(0, perRenderConstantBuffer);
            deviceContext.PixelShader.SetConstantBuffer(1, perObjectConstantBuffer);
            deviceContext.PixelShader.Set(pixelShader);

            foreach (RenderInstance instance in InstanceList)
            {
                Mesh nextMesh = Meshes.GetMeshInstanceAt(instance.MeshId);
                deviceContext.InputAssembler.SetVertexBuffers(0, nextMesh.vertexBufferBinding);

                switch (instance.Fill)
                {
                case RenderInstance.FillMode.eWireFrame:
                    deviceContext.Rasterizer.State = rasterizerStateWireFrame;
                    break;

                case RenderInstance.FillMode.eFill:
                default:
                    deviceContext.Rasterizer.State = rasterizerStateFill;
                    break;
                }

                // Update world matrix
                PerObjectConstantbuffer constantBuffer = new PerObjectConstantbuffer();
                constantBuffer.worldMatrix = instance.WorldMatrix;
                constantBuffer.worldMatrix.Transpose();
                constantBuffer.ColourTint            = instance.Material.ColourTint;
                constantBuffer.AmbientLightStrength  = instance.Material.AmbientLightStrength;
                constantBuffer.DiffuseLightStrength  = instance.Material.DiffuseLightStrength;
                constantBuffer.SpecularLightStrength = instance.Material.SpecularLightStrength;
                constantBuffer.SpecularShininess     = instance.Material.SpecularShininess;
                constantBuffer.userDataValue         = instance.UserDataValue;

                deviceContext.UpdateSubresource(ref constantBuffer, perObjectConstantBuffer);

                // Draw the mesh
                deviceContext.Draw(nextMesh.numberOfVertices, 0);
            }

            GraphicsDevice.Instance.Present();
        }