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); }
// here we update translations, scaling and rotations protected virtual void ApplyMatrix() { if (this.noMatrix) { return; } #if __SHARPDX__ Matrix4 m = Matrix4.Translation(-this.pivot.X, -this.pivot.Y, 0) * Matrix4.Scaling(this.scale.X, this.scale.Y, 1) * Matrix4.RotationZ(this.rotation) * Matrix4.Translation(this.position.X, this.position.Y, 0); #else // WARNING !!! OpenTK uses row-major while OpenGL uses column-major Matrix4 m = Matrix4.CreateTranslation(-this.pivot.X, -this.pivot.Y, 0) * #if !__MOBILE__ Matrix4.CreateScale(this.scale.X, this.scale.Y, 1) * #else Matrix4.Scale(this.scale.X, this.scale.Y, 1) * #endif Matrix4.CreateRotationZ(this.rotation) * // here we do not re-add the pivot, so translation is pivot based too Matrix4.CreateTranslation(this.position.X, this.position.Y, 0); #endif Matrix4 projectionMatrix = Window.Current.ProjectionMatrix; if (this.Camera != null) { m *= this.Camera.Matrix(); if (this.Camera.HasProjection) { projectionMatrix = this.Camera.ProjectionMatrix(); } } else if (Window.Current.CurrentCamera != null) { m *= Window.Current.CurrentCamera.Matrix(); if (Window.Current.CurrentCamera.HasProjection) { projectionMatrix = Window.Current.CurrentCamera.ProjectionMatrix(); } } Matrix4 mvp = m * projectionMatrix; #if __SHARPDX__ // transpose the matrix for DirectX mvp.Transpose(); #endif // pass the matrix to the shader this.shader.SetUniform("mvp", mvp); }
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); }
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); //} }
public virtual void Render(TargetBase target) { if (!Show) { return; } var context2D = target.DeviceManager.ContextDirect2D; context2D.BeginDraw(); if (EnableClear) { context2D.Clear(Color.Black); } var sizeX = (float)target.RenderTargetBounds.Width; var sizeY = (float)target.RenderTargetBounds.Height; var globalScaling = Matrix.Scaling(Math.Min(sizeX, sizeY)); var centerX = (float)(target.RenderTargetBounds.X + sizeX / 2.0f); var centerY = (float)(target.RenderTargetBounds.Y + sizeY / 2.0f); if (textFormat == null) { // Initialize a TextFormat textFormat = new TextFormat(target.DeviceManager.FactoryDirectWrite, "Calibri", 96 * sizeX / 1920) { TextAlignment = TextAlignment.Center, ParagraphAlignment = ParagraphAlignment.Center }; } if (pathGeometry1 == null) { var sizeShape = sizeX / 4.0f; // Creates a random geometry inside a circle pathGeometry1 = new PathGeometry1(target.DeviceManager.FactoryDirect2D); var pathSink = pathGeometry1.Open(); var startingPoint = new Vector2(sizeShape * 0.5f, 0.0f); pathSink.BeginFigure(startingPoint, FigureBegin.Hollow); for (int i = 0; i < 128; i++) { float angle = (float)i / 128.0f * (float)Math.PI * 2.0f; float R = (float)(Math.Cos(angle) * 0.1f + 0.4f); R *= sizeShape; Vector2 point1 = new Vector2(R * (float)Math.Cos(angle), R * (float)Math.Sin(angle)); if ((i & 1) > 0) { R = (float)(Math.Sin(angle * 6.0f) * 0.1f + 0.9f); R *= sizeShape; point1 = new Vector2(R * (float)Math.Cos(angle + Math.PI / 12), R * (float)Math.Sin(angle + Math.PI / 12)); } pathSink.AddLine(point1); } pathSink.EndFigure(FigureEnd.Open); pathSink.Close(); } context2D.TextAntialiasMode = TextAntialiasMode.Grayscale; float t = clock.ElapsedMilliseconds / 1000.0f; context2D.Transform = Matrix.RotationZ((float)(Math.Cos(t * 2.0f * Math.PI * 0.5f))) * Matrix.Translation(centerX, centerY, 0); context2D.DrawText("SharpDX\nDirect2D1\nDirectWrite", textFormat, new RectangleF(-sizeX / 2.0f, -sizeY / 2.0f, +sizeX / 2.0f, sizeY / 2.0f), sceneColorBrush); float scaling = (float)(Math.Cos(t * 2.0 * Math.PI * 0.25) * 0.5f + 0.5f) * 0.5f + 0.5f; context2D.Transform = Matrix.Scaling(scaling) * Matrix.RotationZ(t * 1.5f) * Matrix.Translation(centerX, centerY, 0); context2D.DrawGeometry(pathGeometry1, sceneColorBrush, 2.0f); context2D.EndDraw(); }
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(); }