Esempio n. 1
0
        /// <summary>
        /// Draws a stretched sub-region of a texture.
        /// </summary>
        /// <param name="material">The material used when rendering. The material must implement ITextureMaterial.</param>
        /// <param name="dstRect">Rectangle where the texture object will be drawn.</param>
        /// <param name="srcRect">Sub-region of the texture that will be applied over the <paramref name="dstRect"/>.</param>
        /// <param name="flipMode">Any flipping to be done of the source texture.</param>
        public static void BitmapStretchSR(RenderMaterial material, RectangleF dstRect, RectangleF srcRect, BitmapFlip flipMode)
        {
            // setup render state
            GUICanvas.Instance.RenderState.World.LoadIdentity();
            GUICanvas.Instance.RenderState.View = Matrix.Identity;
            GUICanvas.Instance.RenderState.Projection = _clipMatrix;

            // setup the material
            material.SetupEffect(GUICanvas.Instance.RenderState, null);

            _texLeft = srcRect.X / ((Texture2D)((ITextureMaterial)material).Texture.Instance).Width;
            _texRight = (srcRect.X + srcRect.Width) / ((Texture2D)((ITextureMaterial)material).Texture.Instance).Width;
            _texTop = srcRect.Y / ((Texture2D)((ITextureMaterial)material).Texture.Instance).Height;
            _texBottom = (srcRect.Y + srcRect.Height) / ((Texture2D)((ITextureMaterial)material).Texture.Instance).Height;

            _screenLeft = dstRect.X;
            _screenRight = dstRect.X + dstRect.Width;
            _screenTop = dstRect.Y;
            _screenBottom = dstRect.Y + dstRect.Height;

            // flip x
            if ((flipMode & BitmapFlip.FlipX) != 0)
            {
                float temp = _texLeft;
                _texLeft = _texRight;
                _texRight = temp;
            }

            // flip y
            if ((flipMode & BitmapFlip.FlipY) != 0)
            {
                float temp = _texTop;
                _texTop = _texBottom;
                _texBottom = temp;
            }

            color = _bitmapModulation;

            _vertexSet4[0].Position = new Vector3(_screenLeft - 0.5f, _screenTop - 0.5f, 0.0f);
            _vertexSet4[0].TextureCoordinate = new Vector2(_texLeft, _texTop);
            _vertexSet4[0].Color = color;

            _vertexSet4[1].Position = new Vector3(_screenRight - 0.5f, _screenTop - 0.5f, 0.0f);
            _vertexSet4[1].TextureCoordinate = new Vector2(_texRight, _texTop);
            _vertexSet4[1].Color = color;

            _vertexSet4[2].Position = new Vector3(_screenLeft - 0.5f, _screenBottom - 0.5f, 0.0f);
            _vertexSet4[2].TextureCoordinate = new Vector2(_texLeft, _texBottom);
            _vertexSet4[2].Color = color;

            _vertexSet4[3].Position = new Vector3(_screenRight - 0.5f, _screenBottom - 0.5f, 0.0f);
            _vertexSet4[3].TextureCoordinate = new Vector2(_texRight, _texBottom);
            _vertexSet4[3].Color = color;

            // adltodo: hacks
            _workingRenderInstance = SceneRenderer.RenderManager.AllocateInstance();
            _workingRenderInstance.ObjectTransform = Matrix.Identity;

            GUICanvas.Instance.RenderState.Gfx.Device.VertexDeclaration = GFXVertexFormat.GetVertexDeclaration(GUICanvas.Instance.RenderState.Gfx.Device);

            // draw the vertices
            while (material.SetupPass())
            {
                material.SetupObject(_workingRenderInstance, GUICanvas.Instance.RenderState);

                GUICanvas.Instance.RenderState.Gfx.Device.DrawUserPrimitives<GFXVertexFormat.PCTTBN>(PrimitiveType.TriangleStrip, _vertexSet4, 0, 2);
            }

            // cleanup the material
            material.CleanupEffect();
            SceneRenderer.RenderManager.FreeInstance(_workingRenderInstance);
        }