Exemplo n.º 1
0
        private void Draw()
        {
            _manager.Clear(Color.Black);


            var context = _manager.Device.ImmediateContext;

            var time     = (float)_clock.Elapsed.TotalSeconds;
            var viewProj = Matrix.Multiply(_view, _proj);

            var world =
                Matrix.RotationX(time) *
                Matrix.RotationY(time * 1.5f) *
                Matrix.RotationZ(time * .9f) *
                Matrix.Translation(0, 0, 0);

            var worldViewProj = world * viewProj;

            Vector3 lightDirection = new Vector3(-0.3f, 0.3f, +1);

            lightDirection.Normalize();

            _cbuf.WorldViewProj = worldViewProj;
            _cbuf.World         = world;
            _cbuf.LightDir      = new Vector4(lightDirection, 1);
            _cbuf.Light         = (CheckBoxLight.IsChecked ?? false) ? 1 : 0;

            context.UpdateSubresource(ref _cbuf, _constantBuffer);

            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_sphereVertBuffer, Utilities.SizeOf <Vector4>() * 3, 0));
            context.InputAssembler.SetIndexBuffer(_sphereIndBuffer, SharpDX.DXGI.Format.R16_UInt, 0);
            context.DrawIndexed(_sphere.Indices.Length, 0, 0);

            _manager.Present();
        }
Exemplo n.º 2
0
        protected override void HandlePaint(Rect rect)
        {
            var viewProj = Matrix.Multiply(_view, _proj);
            var context  = Direct2D1Platform.Direct3D11Device.ImmediateContext;

            // Clear views
            context.ClearDepthStencilView(_depthView, DepthStencilClearFlags.Depth, 1.0f, 0);
            context.ClearRenderTargetView(_renderView, Color.White);

            // Update WorldViewProj Matrix
            var worldViewProj = Matrix.RotationX((float)_model.RotationX) * Matrix.RotationY((float)_model.RotationY)
                                * Matrix.RotationZ((float)_model.RotationZ)
                                * Matrix.Scaling((float)_model.Zoom)
                                * viewProj;

            worldViewProj.Transpose();
            context.UpdateSubresource(ref worldViewProj, _contantBuffer);

            // Draw the cube
            context.Draw(36, 0);
            base.HandlePaint(rect);

            // Present!
            _swapChain.Present(0, PresentFlags.None);
        }
Exemplo n.º 3
0
        public Matrix CreateViewMatrix(double w, double h)
        {
            Matrix worldMatrix = Matrix.Identity;
            Matrix view        = Matrix.Identity;
            Matrix proj        = Matrix.Translation((float)(-w - .5f) / 2f, (float)(-h - .5f) / 2f, 0) * Matrix.Scaling(2 / (float)w, -2 / (float)h, 1);

            return(Matrix.Multiply(view, proj));
        }
Exemplo n.º 4
0
        public Matrix CreateViewMatrix2(double w, double h)
        {
            // Prepare matrices

            Matrix worldMatrix = Matrix.Identity;
            var    view        = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY);
            var    proj        = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)w / (float)h, 0.1f, 100.0f);

            return(Matrix.Multiply(view, proj));
        }
Exemplo n.º 5
0
        public override void Update(float timeTotal, float timeDelta)
        {
            //timeDelta; // Unused parameter.
            int width  = (int)_renderTargetSize.Width;
            int height = (int)_renderTargetSize.Height;

            _view     = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY);
            _proj     = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, width / (float)height, 0.1f, 100.0f);
            _viewProj = Matrix.Multiply(_view, _proj);

            _worldViewProj = Matrix.Scaling(1.0f) * Matrix.RotationX(timeTotal) * Matrix.RotationY(timeTotal * 2.0f) * Matrix.RotationZ(timeTotal * .7f) * _viewProj;
            _worldViewProj.Transpose();
        }
Exemplo n.º 6
0
        public override void Render()
        {
            int width  = (int)_renderTargetSize.Width;
            int height = (int)_renderTargetSize.Height;

            // Prepare matrices
            var view     = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY);
            var proj     = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, width / (float)height, 0.1f, 100.0f);
            var viewProj = Matrix.Multiply(view, proj);

            var time = (float)(_clock.ElapsedMilliseconds / 1000.0);


            // Set targets (This is mandatory in the loop)
            _deviceContext.OutputMerger.SetTargets(_depthStencilView, _renderTargetview);

            _deviceContext.ClearDepthStencilView(_depthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0);

            // Clear the views
            _deviceContext.ClearRenderTargetView(_renderTargetview, Color.CornflowerBlue);

            //if (ShowCube)
            //{
            // Calculate WorldViewProj
            var worldViewProj = Matrix.Scaling(1.0f) * Matrix.RotationX(time) * Matrix.RotationY(time * 2.0f) * Matrix.RotationZ(time * .7f) * viewProj;

            worldViewProj.Transpose();

            // Setup the pipeline
            _deviceContext.InputAssembler.SetVertexBuffers(0, _vertexBufferBinding);
            _deviceContext.InputAssembler.InputLayout       = _vertexLayout;
            _deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            _deviceContext.VertexShader.SetConstantBuffer(0, _constantBuffer);
            _deviceContext.VertexShader.Set(_vertexShader);
            _deviceContext.PixelShader.Set(_pixelShader);

            // Update Constant Buffer
            _deviceContext.UpdateSubresource(ref worldViewProj, _constantBuffer, 0);

            // Draw the cube
            _deviceContext.Draw(36, 0);
            //}
        }
        private void SharpDXRenderingAction(RenderingContext renderingContext)
        {
            // Just in case the data were already disposed
            if (_constantBuffer == null)
            {
                return;
            }


            var context = renderingContext.DXDevice.ImmediateContext;

            // NOTE:
            // Because DXEngine by default uses different culling mode than SharpDX,
            // we first need to set the culling mode to the one that is used in the ShardDX sample - CounterClockwise culling
            // The SharpDX way to do that is (we are using the already defined CullCounterClockwise rasterizer state):
            //context.Rasterizer.State = renderingContext.DXDevice.CommonStates.CullCounterClockwise;

            // But it is better to do that in the DXEngine's way with ImmediateContextStatesManager
            // That prevents to many state changes with checking the current state and also ensures that any DXEngine's objects that would be rendered after
            // SharpDX objects would be rendered correctly:
            renderingContext.DXDevice.ImmediateContextStatesManager.RasterizerState = renderingContext.DXDevice.CommonStates.CullCounterClockwise;


            // Prepare All the stages
            context.InputAssembler.InputLayout       = _layout;
            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_vertices, SharpDX.Utilities.SizeOf <Vector4>() * 2, 0));
            context.VertexShader.SetConstantBuffer(0, _constantBuffer);
            context.VertexShader.Set(_vertexShader);
            context.PixelShader.Set(_pixelShader);


            // Clear views - this is done by DXEngine in PrepareRenderTargetsRenderingStep
            //context.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1.0f, 0);
            //context.ClearRenderTargetView(renderView, Color.Black);


            // Prepare matrices
            var view = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY);

            // Setup new projection matrix with correct aspect ratio
            var proj = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)renderingContext.DXScene.Width / (float)renderingContext.DXScene.Height, 0.1f, 100.0f);

            var viewProj = Matrix.Multiply(view, proj);


            var time = _clock.ElapsedMilliseconds / 1000.0f;

            // Update WorldViewProj Matrix
            var worldViewProj = Matrix.RotationX(time) * Matrix.RotationY(time * 2) * Matrix.RotationZ(time * .7f) * viewProj;

            // If the matrixes in the shaders are written in default format, then we need to transpose them.
            // To remove the need for transposal we can define the matrixes in HLSL as row_major (this is used in DXEngine's shaders, but here the original shader from SharpDX is preserved).
            worldViewProj.Transpose();

            // Write the new world view projection matrix to the constant buffer
            context.UpdateSubresource(ref worldViewProj, _constantBuffer);

            // Draw the cube
            context.Draw(36, 0);

            // Present is called by DXEngine in CompleteRenderingStep
            //swapChain.Present(0, PresentFlags.None);
        }