Пример #1
0
        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);
        }
Пример #2
0
        void RenderScene()
        {
            lock (syncObject)
            {
                if (device == null)
                {
                    CreateDeviceResources();
                }

                if (!pause)
                {
                    if (lastSavedDelta != 0)
                    {
                        startTime      = Environment.TickCount - lastSavedDelta;
                        lastSavedDelta = 0;
                    }
                    currentTimeVariation = (Environment.TickCount - startTime) / 6000.0f;
                    worldMatrix          = MatrixMath.MatrixTranslate(0, 0, currentTimeVariation);
                    textBrush.Transform  = Matrix3x2F.Translation(0, (4096f / 16f) * currentTimeVariation);
                }

                device.ClearDepthStencilView(
                    depthStencilView,
                    ClearOptions.Depth,
                    1,
                    0
                    );

                // Clear the back buffer
                device.ClearRenderTargetView(renderTargetView, backColor);

                diffuseVariable.Resource = null;

                technique.GetPassByIndex(0).Apply();

                // Draw the D2D content into our D3D surface
                RenderD2DContentIntoSurface();

                diffuseVariable.Resource = textureResourceView;

                // Update variables
                worldMatrixVariable.Matrix = worldMatrix;

                // Set index buffer
                device.IA.IndexBuffer = new IndexBuffer(facesIndexBuffer, Format.R16UInt, 0);

                // Draw the scene
                technique.GetPassByIndex(0).Apply();

                device.DrawIndexed((uint)Marshal.SizeOf(VertexArray.VerticesInstance), 0, 0);

                swapChain.Present(0, Microsoft.WindowsAPICodePack.DirectX.Graphics.PresentOptions.None);
            }
        }
        /// <summary>
        /// Draw the planet.
        /// </summary>
        /// <param name="planet"></param>
        private void DrawPlanet(Ellipse planet)
        {
            // Get the size of the RenderTarget.
            float rtWidth  = this._renderTarget.Size.Width;
            float rtHeight = this._renderTarget.Size.Height;

            this._renderTarget.Transform = Matrix3x2F.Translation(10 + this._animateTranslateX, rtHeight / 2 - 100);
            this._renderTarget.FillEllipse(planet, this._planetBackgroundBrush);
            this._renderTarget.Transform = Matrix3x2F.Translation(23 + this._animateTranslateX, rtHeight / 2 - 121);
            this._renderTarget.FillGeometry(this._planetUpPath, this._continentBrush);
            this._renderTarget.Transform = Matrix3x2F.Translation(15 + this._animateTranslateX, rtHeight / 2 + 1);
            this._renderTarget.FillGeometry(this._planetDownPath, this._continentBrush);
        }
Пример #4
0
        void RenderD2DContentIntoTexture()
        {
            SizeF rtSize = textureRenderTarget.Size;

            textureRenderTarget.BeginDraw();

            textureRenderTarget.Transform = Matrix3x2F.Identity;
            textureRenderTarget.Clear(new ColorF(GetColorValues(System.Windows.Media.Colors.White)));

            textureRenderTarget.FillRectangle(
                new RectF(0.0f, 0.0f, rtSize.Width, rtSize.Height),
                gridPatternBitmapBrush);

            SizeF size = d2dBitmap.Size;

            textureRenderTarget.DrawBitmap(
                d2dBitmap, 1.0f, BitmapInterpolationMode.Linear,
                new RectF(
                    0.0f,
                    0.0f,
                    size.Width,
                    size.Height)
                );

            // Draw the bitmap at the bottom corner of the window
            textureRenderTarget.DrawBitmap(
                d2dBitmap, 1.0f, BitmapInterpolationMode.Linear,
                new RectF(
                    rtSize.Width - size.Width,
                    rtSize.Height - size.Height,
                    rtSize.Width,
                    rtSize.Height));

            // Set the world transform to rotatate the drawing around the center of the render target
            // and write "Hello World"
            float angle = 0.1f * Environment.TickCount;

            textureRenderTarget.Transform
                = Matrix3x2F.Rotation(
                      angle,
                      new Point2F(
                          rtSize.Width / 2,
                          rtSize.Height / 2
                          ));

            textureRenderTarget.DrawText(
                HelloWorldText,
                textFormat,
                new RectF(
                    0,
                    0,
                    rtSize.Width,
                    rtSize.Height
                    ),
                blackBrush
                );

            // Reset back to the identity transform
            textureRenderTarget.Transform
                = Matrix3x2F.Translation(
                      0,
                      rtSize.Height - 200
                      );

            textureRenderTarget.FillGeometry(
                pathGeometry,
                linearGradientBrush);

            textureRenderTarget.Transform =
                Matrix3x2F.Translation(
                    rtSize.Width - 200,
                    0
                    );

            textureRenderTarget.FillGeometry(
                pathGeometry,
                linearGradientBrush
                );

            textureRenderTarget.EndDraw();
        }
        /// <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();
        }