public override void Render()
        {
            if (!resourcesReady)
            {
                return;
            }

            var context = this.DeviceManager.Direct3DContext;

            var vs = this.vertexShader;

            // OutputMerger targets must be set for every frame in Windows Store apps
            context.OutputMerger.SetTargets(this.DepthStencilView, this.RenderTargetView);

            context.ClearDepthStencilView(this.DepthStencilView, SharpDX.Direct3D11.DepthStencilClearFlags.Depth | SharpDX.Direct3D11.DepthStencilClearFlags.Stencil, 1.0f, 0);
            context.ClearRenderTargetView(this.RenderTargetView, SharpDX.Color.LightBlue);

            // Set the context pipeline state
            SetContextState(context);

            ViewMatrix = Matrix.LookAtRH(Camera.Position, Camera.Position + Camera.LookAtDir, Vector3.UnitY);

            var viewProjection = ViewMatrix * ProjectionMatrix;
            // Extract camera position from view
            var camPosition = Matrix.Transpose(Matrix.Invert(ViewMatrix)).Column4;

            Camera.Position = new Vector3(camPosition.X, camPosition.Y, camPosition.Z);

            var perFrame = new ConstantBuffers.PerFrame();

            perFrame.CameraPosition = Camera.Position;
            context.UpdateSubresource(ref perFrame, perFrameBuffer);

            var perMaterial = new ConstantBuffers.PerMaterial();

            perMaterial.Ambient       = new Color4(0.2f);
            perMaterial.Diffuse       = SharpDX.Color.White;
            perMaterial.Emissive      = new Color4(0);
            perMaterial.Specular      = SharpDX.Color.White;
            perMaterial.SpecularPower = 20f;
            context.UpdateSubresource(ref perMaterial, perMaterialBuffer);

            var worldRotation = Matrix.Identity;// Matrix.RotationAxis(Vector3.UnitY, clock.ElapsedMilliseconds / 1000.0f);
            var perObject     = new ConstantBuffers.PerObject();

            perObject.World = cubeRenderer.World * WorldMatrix * worldRotation;
            perObject.WorldInverseTranspose = Matrix.Transpose(Matrix.Invert(perObject.World));
            perObject.WorldViewProjection   = perObject.World * viewProjection;
            perObject.Transpose();
            context.UpdateSubresource(ref perObject, perObjectBuffer);

            cubeRenderer.Render();

            textRenderer.Text = "Hello CoreWindow!";
            textRenderer.Render();

            fpsRenderer.Render();

            // Present the render result
            Present();
        }
Exemplo n.º 2
0
        public override void Run()
        {
            // Create and Initialize the axis lines renderer
            var axisLines = ToDispose(new AxisLinesRenderer());

            axisLines.Initialize(this);

            // Create and Initialize the axis lines renderer
            var triangle = ToDispose(new TriangleRenderer());

            triangle.Initialize(this);

            //// Create and Initialize the axis lines renderer
            var quad = ToDispose(new QuadRenderer());

            quad.Initialize(this);

            //// Create and Initialize the axis lines renderer
            var sphere = ToDispose(new SphereRenderer(Vector3.Zero, .25f));

            sphere.Initialize(this);

            //// FPS renderer
            FpsRenderer fps = null;

            if (ShowFPS)
            {
                fps = ToDispose(new Common.FpsRenderer("Calibri", Color.CornflowerBlue, new Point(8, 8), 16));
                fps.Initialize(this);
            }

            //// Text renderer
            Common.TextRenderer textRenderer = null;
            if (ShowText)
            {
                textRenderer = ToDispose(new Common.TextRenderer("Calibri", Color.CornflowerBlue, new Point(8, 30), 12));
                textRenderer.Initialize(this);

                UpdateText = () =>
                {
                    textRenderer.Text =
                        String.Format("World rotation ({0}) (Up/Down Left/Right Wheel+-)\nView ({1}) (A/D, W/S, Shift+Wheel+-)"
                                      + "\nPress X to reinitialize the device and resources (device ptr: {2})"
                                      + "\nPress Z to show/hide depth buffer",
                                      rotation,
                                      V.TranslationVector,
                                      DeviceManager.Direct3DDevice.NativePointer);
                };

                UpdateText();
            }

            InitializeMatricies();
            Window.Resize     += Window_Resize;
            Window.KeyDown    += Window_KeyDown;
            Window.KeyUp      += Window_KeyUp;
            Window.MouseWheel += Window_MouseWheel;
            RenderLoop.Run(Window, () =>
            {
                // Clear DSV
                DeviceManager.Direct3DContext.ClearDepthStencilView(DepthStencilView,
                                                                    DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil,
                                                                    1.0f, 0);
                // Clear RTV
                DeviceManager.Direct3DContext.ClearRenderTargetView(RenderTargetView, Color.White);

                // VP matrix
                var VP = Matrix.Multiply(V, P);

                // MVP
                var MVP = M * VP;

                // Must transpose to use in HLSL
                MVP.Transpose();

                // Write MVP to constant buffer
                DeviceManager.Direct3DContext.UpdateSubresource(ref MVP, mvpBuffer);

                // Render our primitives
                axisLines.Render();
                quad.Render();
                MVP = sphere.M * VP;
                MVP.Transpose();
                DeviceManager.Direct3DContext.UpdateSubresource(ref MVP, mvpBuffer);
                sphere.Render();
                var v = sphere.RotationAngles;
                v.Y  += 0.016f;
                sphere.RotationAngles = v;
                triangle.Render();

                // FPS renderer
                if (fps != null)
                {
                    fps.Render();
                }

                // Text renderer
                if (textRenderer != null)
                {
                    textRenderer.Render();
                }

                Present();
            });
        }
        public override void Render()
        {
            var context = this.DeviceManager.Direct3DContext;

            var vs = this.vertexShader;

            // OutputMerger targets must be set for every frame in Windows Store apps
            context.OutputMerger.SetTargets(this.DepthStencilView, this.RenderTargetView);

            context.ClearDepthStencilView(this.DepthStencilView, SharpDX.Direct3D11.DepthStencilClearFlags.Depth | SharpDX.Direct3D11.DepthStencilClearFlags.Stencil, 1.0f, 0);
            context.ClearRenderTargetView(this.RenderTargetView, new SharpDX.Color(Color.R, Color.G, Color.B, 1));

            // Set the context pipeline state
            SetContextState(context);

            ViewMatrix = Matrix.LookAtRH(Camera.Position, Camera.Position + Camera.LookAtDir, Vector3.UnitY);

            var viewProjection = ViewMatrix * ProjectionMatrix;
            // Extract camera position from view
            var camPosition = Matrix.Transpose(Matrix.Invert(ViewMatrix)).Column4;

            Camera.Position = new Vector3(camPosition.X, camPosition.Y, camPosition.Z);

            var perFrame = new ConstantBuffers.PerFrame();

            perFrame.CameraPosition = Camera.Position;
            context.UpdateSubresource(ref perFrame, perFrameBuffer);

            var perMaterial = new ConstantBuffers.PerMaterial();

            perMaterial.Ambient       = new Color4(0.2f);
            perMaterial.Diffuse       = SharpDX.Color.White;
            perMaterial.Emissive      = new Color4(0);
            perMaterial.Specular      = SharpDX.Color.White;
            perMaterial.SpecularPower = 20f;
            context.UpdateSubresource(ref perMaterial, perMaterialBuffer);

            //var scale = this.SwapChainPanel.CompositionScaleX;
            var worldRotation = Matrix.Identity * Matrix.RotationAxis(Vector3.UnitY, clock.ElapsedMilliseconds / 1000.0f);
            var perObject     = new ConstantBuffers.PerObject();

            perObject.World = cubeRenderer.World * WorldMatrix * worldRotation;
            perObject.WorldInverseTranspose = Matrix.Transpose(Matrix.Invert(perObject.World));
            perObject.WorldViewProjection   = perObject.World * viewProjection;
            perObject.Transpose();
            context.UpdateSubresource(ref perObject, perObjectBuffer);

            cubeRenderer.Render();

            textRenderer.Text = "Hello SwapChainPanel!";
            textRenderer.Render();

            fpsRenderer.Render();

            //// Draw a circle.
            //var d2dContext = DeviceManager.Direct2DContext;
            //float radiusX = this.RenderTargetSize.Width / 3.0f;
            //float radiusY = this.RenderTargetSize.Height / 3.0f;

            //float strokeWidth = 10.0f *this.SwapChainPanel.CompositionScaleX;
            //var brush = new SharpDX.Direct2D1.SolidColorBrush(d2dContext, SharpDX.Color.Black);
            //d2dContext.BeginDraw();
            //using (var sc2 = SwapChain.QueryInterface<SwapChain2>())
            //{
            //    d2dContext.Transform = sc2.MatrixTransform;
            //}
            //d2dContext.DrawEllipse(new SharpDX.Direct2D1.Ellipse(new Vector2(this.RenderTargetSize.Width / 2.0f, this.RenderTargetSize.Height / 2.0f), radiusX, radiusY), brush, strokeWidth );
            //d2dContext.EndDraw();

            // Present the render result
            Present();
        }