protected internal Matrix3x2F RandomMatrix3x2() { var which = Random.NextDouble(); //return Matrix3x2F.Skew(90, 0); //check for bug 730701 Matrix3x2F ret; if (which < 0.5) { ret = new Matrix3x2F( 1.0f - (float)Random.NextDouble() * (float)Random.NextDouble(), (float)Random.NextDouble() * (float)Random.NextDouble(), (float)Random.NextDouble() * (float)Random.NextDouble(), 1.0f - (float)Random.NextDouble() * (float)Random.NextDouble(), (float)Random.NextDouble() * (float)Random.NextDouble(), (float)Random.NextDouble() * (float)Random.NextDouble() ); TraceMatrix(ret); return(ret); } if (which < 0.8) { ret = Matrix3x2F.Identity; TraceMatrix(ret); return(ret); } if (which < 0.85) { ret = Matrix3x2F.Translation( Random.Next(-20, 20), Random.Next(-20, 20)); TraceMatrix(ret); return(ret); } if (which < 0.90) { ret = Matrix3x2F.Skew( (float)(Random.NextDouble() * Random.NextDouble() * 89), (float)(Random.NextDouble() * Random.NextDouble() * 89), CoinFlip ? new Point2F(0, 0) : RandomPoint()); TraceMatrix(ret); return(ret); } if (which < 0.95) { ret = Matrix3x2F.Scale( 1 + (float)((Random.NextDouble() - 0.5) * Random.NextDouble()), 1 + (float)((Random.NextDouble() - 0.5) * Random.NextDouble()), CoinFlip ? new Point2F(0, 0) : RandomPoint()); TraceMatrix(ret); return(ret); } ret = Matrix3x2F.Rotation( (float)((Random.NextDouble() - 0.5) * Random.NextDouble() * 720), CoinFlip ? new Point2F(0, 0) : RandomPoint()); TraceMatrix(ret); return(ret); }
private void InitializeDisplayerState() { var rtps = new RenderTargetProperties(); var hrtp = new HwndRenderTargetProperties(Panel.Handle, new SizeU((uint)Panel.Width, (uint)Panel.Height), PresentOptions.None); Factory = D2DFactory.CreateFactory(D2DFactoryType.Multithreaded); //创建工厂 rt = Factory.CreateHwndRenderTarget(rtps, hrtp); rt.AntiAliasMode = AntiAliasMode.Aliased; rt.TextAntiAliasMode = TextAntiAliasMode.Aliased; (rt as HwndRenderTarget).Resize(new SizeU((uint)Panel.Width, (uint)Panel.Height)); rt.Transform = Matrix3x2F.Scale(rt.Size.Width / Panel.Width, rt.Size.Height / Panel.Height); }
private void Pb_SizeChanged(object sender, EventArgs e) { lock (Locker) { if (Panel.Width < 10 && Panel.Height < 10) //卫语句,窗口最小化时会触发sizechanged事件,此时width和height都是0,会触发ValueInterval的范围过小异常 { return; } Mapper.SetScreenArea(0, Panel.Width, 0, Panel.Height); (rt as HwndRenderTarget).Resize(new SizeU((uint)Panel.Width, (uint)Panel.Height)); rt.Transform = Matrix3x2F.Scale(rt.Size.Width / Panel.Width, rt.Size.Height / Panel.Height); Redraw = true; } }
void RenderScene() { lock (syncObject) { //initialize D3D device and D2D render targets the first time we get here if (device == null) { CreateDeviceResources(); } //tick count is used to control animation and calculate FPS int currentTime = Environment.TickCount; if (!startTime.HasValue) { startTime = currentTime; } currentTicks = currentTime - startTime.GetValueOrDefault(); float a = (currentTicks * 360.0f) * ((float)Math.PI / 180.0f) * 0.0001f; worldMatrix = MatrixMath.MatrixRotationY(a); // Swap chain will tell us how big the back buffer is SwapChainDescription swapDesc = swapChain.Description; uint nWidth = swapDesc.BufferDescription.Width; uint nHeight = swapDesc.BufferDescription.Height; device.ClearDepthStencilView( depthStencilView, ClearOptions.Depth, 1, 0 ); // Draw a gradient background before we draw the cube if (backBufferRenderTarget != null) { backBufferRenderTarget.BeginDraw(); backBufferGradientBrush.Transform = Matrix3x2F.Scale( backBufferRenderTarget.Size, new Point2F(0.0f, 0.0f)); RectF rect = new RectF( 0.0f, 0.0f, nWidth, nHeight); backBufferRenderTarget.FillRectangle(rect, backBufferGradientBrush); backBufferRenderTarget.EndDraw(); } diffuseVariable.Resource = null; technique.GetPassByIndex(0).Apply(); // Draw the D2D content into a D3D surface. RenderD2DContentIntoTexture(); // Pass the updated texture to the pixel shader diffuseVariable.Resource = textureResourceView; // Update variables that change once per frame. worldVariable.Matrix = worldMatrix; // Set the index buffer. device.IA.IndexBuffer = new IndexBuffer(facesIndexBuffer, Format.R16UInt, 0); // Render the scene technique.GetPassByIndex(0).Apply(); device.DrawIndexed(vertexArray.s_FacesIndexArray.Length, 0, 0); // Update fps currentTime = Environment.TickCount; // Get the ticks again currentTicks = currentTime - startTime.GetValueOrDefault(); if ((currentTime - lastTicks) > 250) { fps = (swapChain.LastPresentCount) / (currentTicks / 1000f); lastTicks = currentTime; } backBufferRenderTarget.BeginDraw(); // Draw fps backBufferRenderTarget.DrawText( String.Format("Average FPS: {0:F1}", fps), textFormatFps, new RectF( 10f, nHeight - 32f, nWidth, nHeight ), backBufferTextBrush ); backBufferRenderTarget.EndDraw(); swapChain.Present(0, Microsoft.WindowsAPICodePack.DirectX.Graphics.PresentOptions.None); } }
/// <summary> /// The main rendering method. /// </summary> private void Render() { this.CreateDeviceResource(); this._renderTarget.BeginDraw(); // Do some clearing. this._renderTarget.Transform = Matrix3x2F.Identity; this._renderTarget.Clear(new ColorF(Colors.Black)); SizeF size = this._renderTarget.Size; RectF rectBackground = new RectF(0f, 0f, size.Width, size.Height); // Get the size of the RenderTarget. float rtWidth = this._renderTarget.Size.Width; float rtHeight = this._renderTarget.Size.Height; // Draw some small stars for (int i = 0; i < 300; i++) { float x = (float)(this._random.NextDouble()) * rtWidth; float y = (float)(this._random.NextDouble()) * rtHeight; Ellipse smallStar = new Ellipse(new Point2F(x, y), 1f, 1f); this._renderTarget.FillEllipse(smallStar, this._smallStarBrush); } Ellipse planet = new Ellipse(new Point2F(100f, 100f), 100f, 100f); // When animating from right to left, draw the planet afte the star so it has a smaller z-index, and will be covered by the star. if (!this._animateToRight) { this.DrawPlanet(planet); } // Draw the star. Ellipse star = new Ellipse(new Point2F(95f, 95f), 75f, 75f); // Scale the star, and translate it to the center of the screen. Note if translation is performed before scaling, you'll get different result. Matrix3x2F scaleMatrix = Matrix3x2F.Scale(2f, 2f, new Point2F(95f, 95f)); Matrix3x2F translationMatrix = Matrix3x2F.Translation(rtWidth / 2 - 95, rtHeight / 2 - 95); // Since the managed counter part of Matrix3x2F does not expose the multiply operaion, let's convert them to WPF matrixes to do the multiplication. System.Windows.Media.Matrix wpfScaleMatrix = new System.Windows.Media.Matrix(scaleMatrix.M11, scaleMatrix.M12, scaleMatrix.M21, scaleMatrix.M22, scaleMatrix.M31, scaleMatrix.M32); System.Windows.Media.Matrix wpfTranslateMatrix = new System.Windows.Media.Matrix(translationMatrix.M11, translationMatrix.M12, translationMatrix.M21, translationMatrix.M22, translationMatrix.M31, translationMatrix.M32); System.Windows.Media.Matrix wpfResultMatrix = wpfScaleMatrix * wpfTranslateMatrix; this._renderTarget.Transform = new Matrix3x2F((float)wpfResultMatrix.M11, (float)wpfResultMatrix.M12, (float)wpfResultMatrix.M21, (float)wpfResultMatrix.M22, (float)wpfResultMatrix.OffsetX, (float)wpfResultMatrix.OffsetY); this._renderTarget.FillGeometry(this._starOutline, this._starOutlineBrush); // The transform matrix will be apllied to all rendered elements, until it is reset. So we don't need to set the matrix for the ellipse again. this._renderTarget.FillEllipse(star, this._starBrush); // By default, or when animating from left to right, draw the planet afte the star so it has a larger z-index. if (this._animateToRight) { this.DrawPlanet(planet); } if (this._animate) { // Perform a hit test. If the user clicked the planet, let's animate it to make it move around the star. EllipseGeometry hitTestEllipse = this._d2DFactory.CreateEllipseGeometry(planet); Point2F point = new Point2F(this._clickedPointX, this._clickedPointY); Matrix3x2F matrix = Matrix3x2F.Translation(10f, rtHeight / 2 - 100); bool hit = hitTestEllipse.FillContainsPoint(point, 0f, matrix); if (!hit) { this._animate = false; } else { // When moving from left to right, translate transform becomes larger and lager. if (this._animateToRight) { this._animateTranslateX++; if (this._animateTranslateX > rtWidth - 220) { this._animateToRight = false; } } else { // When moving from right to left, translate transform becomes smaller and smaller. this._animateTranslateX--; if (this._animateTranslateX <= 0) { this._animateToRight = true; this._animateTranslateX = 0; this._animate = false; } } } } // Finish drawing. this._renderTarget.EndDraw(); }