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();
        }
        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);
        }
Beispiel #3
0
        private void ControlOnCustomRender()
        {
            var info = new DrawInfo();

            for (int i = 0; i < 3; i++)
            {
                info.World = Matrix.RotationY((float)(DateTime.Now - start).TotalSeconds * 0.2f) *
                             Matrix.Translation(i * 3 - 3, 0, 0);
                info.Projection     = camera.Projection;
                info.View           = camera.View;
                info.DiffuseMap     = texture[i];
                info.NormalMap      = normalmap[i];
                info.MetallicMap    = metallicmap[i];
                info.RoughnessMap   = roughnessmap[i];
                info.CameraPosition = camera.Position;
                info.CubeMap        = cubemap;

                Renderer.Current.DrawModel3D(model, info);
            }
            info.World        = Matrix.Scaling(10) * Matrix.Translation(0, -1.7f, 0);
            info.DiffuseMap   = texture[3];
            info.NormalMap    = normalmap[3];
            info.MetallicMap  = metallicmap[3];
            info.RoughnessMap = roughnessmap[3];
            Renderer.Current.DrawModel3D(floor, info);
        }
        private void TransformModelsButton_OnClick(object sender, RoutedEventArgs e)
        {
            if (_redPyramidObjectNode != null)
            {
                // Add translation to _redPyramidObjectNode
                if (_redPyramidObjectNode.Transform == null)
                {
                    // First set the Transform object
                    _redPyramidMatrix = Matrix.Translation(0, 10, 0);
                    _redPyramidObjectNode.Transform = new Transformation(ref _redPyramidMatrix);
                }
                else
                {
                    // After Transform is set, we can update the transformation matrix
                    _redPyramidMatrix.M42 += 10; // M42 == OffsetY
                    _redPyramidObjectNode.Transform.SetMatrix(ref _redPyramidMatrix);
                }

                // We need to inform the engine about the the changes:
                _redPyramidObjectNode.NotifySceneNodeChange(SceneNode.SceneNodeDirtyFlags.TransformChanged);
            }


            if (_orangePyramidObjectNode != null)
            {
                // Orange pyramid will be scaled and rotated
                if (_orangePyramidObjectNode.Transform == null)
                {
                    _orangePyramidYScale  = 1.2f;
                    _orangePyramidYRotate = 10;

                    // To combine multiple transformation, we need to multiply their matrices one after another (note that order of matrices is important)
                    var transformMatrix = Matrix.Scaling(1, _orangePyramidYScale, 1) *
                                          Matrix.RotationY(MathUtil.DegreesToRadians(_orangePyramidYRotate)) *
                                          Matrix.Translation(0, -_orangePyramidYRotate, 0);

                    _orangePyramidObjectNode.Transform = new Transformation(ref transformMatrix);
                }
                else
                {
                    _orangePyramidYScale  *= 1.2f;
                    _orangePyramidYRotate += 10;

                    var transformMatrix = Matrix.Scaling(1, _orangePyramidYScale, 1) *
                                          Matrix.RotationY(MathUtil.DegreesToRadians(_orangePyramidYRotate)) *
                                          Matrix.Translation(0, -_orangePyramidYRotate, 0);

                    _orangePyramidObjectNode.Transform.SetMatrix(transformMatrix);
                }

                // We need to inform the engine about the the changes:
                _orangePyramidObjectNode.NotifySceneNodeChange(SceneNode.SceneNodeDirtyFlags.TransformChanged);
            }
        }
Beispiel #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();
        }
Beispiel #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);
        }