Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GorgonRectangle"/> class.
        /// </summary>
        /// <param name="gorgon2D">Gorgon interface that owns this renderable.</param>
        /// <param name="name">The name of the rectangle.</param>
        /// <param name="filled">TRUE to draw a filled rectangle, FALSE to draw an outline.</param>
        internal GorgonRectangle(Gorgon2D gorgon2D, string name, bool filled)
            : base(gorgon2D, name)
        {
            _colors = new[]
            {
                GorgonColor.White,
                GorgonColor.White,
                GorgonColor.White,
                GorgonColor.White
            };

            _corners = new[]
            {
                Vector2.Zero,
                Vector2.Zero,
                Vector2.Zero,
                Vector2.Zero
            };

            IsFilled = filled;
            _filled  = gorgon2D.Renderables.CreateSprite("Rectangle.Sprite", new Vector2(1), GorgonColor.White);
            _line    = new GorgonLine(gorgon2D, "Rectangle.Line")
            {
                Color      = GorgonColor.White,
                TextureEnd = new Vector2(1),
                StartPoint = Vector2.Zero,
                EndPoint   = new Vector2(1)
            };
        }
Exemplo n.º 2
0
        /// <summary>
        /// Function called when the effect is being initialized.
        /// </summary>
        /// <remarks>
        /// Use this method to set up the effect upon its creation.  For example, this method could be used to create the required shaders for the effect.
        /// </remarks>
        protected override void OnInitialize()
        {
            base.OnInitialize();

            _xOffsets   = new Vector4[13];
            _yOffsets   = new Vector4[13];
            _kernel     = new float[13];
            _blurBuffer = Graphics.ImmediateContext.Buffers.CreateConstantBuffer("Gorgon2DGaussianBlurEffect Constant Buffer",
                                                                                 new GorgonConstantBufferSettings
            {
                SizeInBytes = DirectAccess.SizeOf <Vector4>() * _xOffsets.Length
            });
            _blurStaticBuffer = Graphics.ImmediateContext.Buffers.CreateConstantBuffer("Gorgon2DGaussianBlurEffect Static Constant Buffer",
                                                                                       new GorgonConstantBufferSettings
            {
                SizeInBytes = DirectAccess.SizeOf <Vector4>() * (_kernel.Length + 1)
            });

            _blurKernelStream = new GorgonDataStream(_blurStaticBuffer.SizeInBytes);

            Passes[0].PixelShader = Graphics.ImmediateContext.Shaders.CreateShader <GorgonPixelShader>("Effect.PS.GaussBlur", "GorgonPixelShaderGaussBlur", "#GorgonInclude \"Gorgon2DShaders\"");

            UpdateKernel();

            _blurSprite = Gorgon2D.Renderables.CreateSprite("Gorgon2DGaussianBlurEffect Sprite", new GorgonSpriteSettings
            {
                Size = BlurRenderTargetsSize
            });

            _blurSprite.BlendingMode  = BlendingMode.None;
            _blurSprite.SmoothingMode = SmoothingMode.Smooth;
            _blurSprite.TextureRegion = new RectangleF(0, 0, 1, 1);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Function called when the effect is being initialized.
        /// </summary>
        /// <remarks>
        /// Use this method to set up the effect upon its creation.  For example, this method could be used to create the required shaders for the effect.
        /// </remarks>
        protected override void OnInitialize()
        {
            base.OnInitialize();

            Passes[1].PixelShader = Graphics.ImmediateContext.Shaders.CreateShader <GorgonPixelShader>("Effect.2D.DisplacementDecoder.PS", "GorgonPixelShaderDisplacementDecoder", "#GorgonInclude \"Gorgon2DShaders\"");

            _displacementBuffer = Graphics.ImmediateContext.Buffers.CreateConstantBuffer("Gorgon2DDisplacementEffect Constant Buffer",
                                                                                         new GorgonConstantBufferSettings
            {
                SizeInBytes = 16
            });
            _displacementSprite = Gorgon2D.Renderables.CreateSprite("Gorgon2DDisplacementEffect Sprite", new GorgonSpriteSettings
            {
                Size = new Vector2(1)
            });
            _displacementSprite.BlendingMode  = BlendingMode.None;
            _displacementSprite.SmoothingMode = SmoothingMode.Smooth;

            // Set the drawing for rendering the displacement map.
            Passes[1].RenderAction = pass => _displacementSprite.Draw();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Gorgon2DPerspectiveCamera"/> class.
        /// </summary>
        /// <param name="gorgon2D">The gorgon 2D interface that owns the camera.</param>
        /// <param name="name">The name of the camera.</param>
        /// <param name="viewDimensions">The view dimensions.</param>
        /// <param name="minDepth">The minimum depth value.</param>
        /// <param name="maximumDepth">The maximum depth of the projection.</param>
        internal Gorgon2DPerspectiveCamera(Gorgon2D gorgon2D, string name, RectangleF viewDimensions, float minDepth, float maximumDepth)
            : base(name)
        {
            Gorgon2D        = gorgon2D;
            _maxDepth       = maximumDepth;
            _minDepth       = minDepth;
            _viewDimensions = viewDimensions;

            _cameraIcon = new GorgonSprite(gorgon2D, "GorgonCamera.PerspIcon")
            {
                Size    = new Vector2(64, 50),
                Texture = gorgon2D.Graphics.GetTrackedObjectsOfType <GorgonTexture2D>()
                          .FirstOrDefault(item =>
                                          item.Name.Equals("Gorgon2D.Icons",
                                                           StringComparison.OrdinalIgnoreCase)) ??
                          gorgon2D.Graphics.Textures.CreateTexture <GorgonTexture2D>("Gorgon2D.Icons",
                                                                                     Resources.Icons),
                TextureRegion = new RectangleF(0, 0, 0.25f, 0.195313f),
                Anchor        = new Vector2(32f, 25),
                Scale         = new Vector2(1.0f),
                Color         = Color.White
            };
        }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Gorgon2D"/> class.
        /// </summary>
        /// <param name="target">The primary render target to use.</param>
        /// <param name="vertexCacheSize">The number of vertices that can be placed in vertex cache.</param>
        /// <param name="autoCreatedTarget">TRUE if Gorgon created the target, FALSE if the user created the target.</param>
        internal Gorgon2D(GorgonRenderTargetView target, int vertexCacheSize, bool autoCreatedTarget)
        {
            _systemCreatedTarget = autoCreatedTarget;

            _cache = new Gorgon2DVertexCache(this, vertexCacheSize.Max(1024));

            IsBlendingEnabled  = true;
            IsAlphaTestEnabled = true;

            TrackedObjects = new GorgonDisposableObjectCollection();
            Graphics       = target.Resource.Graphics;
            DefaultTarget  = target;

            _logoSprite = new GorgonSprite(this, "Gorgon2D.LogoSprite")
            {
                Anchor        = new Vector2(Graphics.Textures.GorgonLogo.Settings.Size),
                Texture       = Graphics.Textures.GorgonLogo,
                TextureRegion = new RectangleF(Vector2.Zero, new Vector2(1)),
                Color         = Color.White,
                Size          = Graphics.Textures.GorgonLogo.Settings.Size
            };
            DefaultCamera = new Gorgon2DOrthoCamera(this,
                                                    "Gorgon.Camera.Default",
                                                    new RectangleF(0, 0, _defaultTarget.Width, _defaultTarget.Height),
                                                    0,
                                                    1.0f)
            {
                AutoUpdate = true
            };

            Renderables = new GorgonRenderables(this, _cache);
            Drawing     = new GorgonDrawing(this);

            // Perform further initialization.
            Initialize();
        }
Exemplo n.º 6
0
        /// <summary>
        /// Function to load a version 1.x Gorgon sprite.
        /// </summary>
        /// <param name="sprite">Sprite to fill in with data.</param>
        /// <param name="reader">Binary reader to use to read in the data.</param>
        public static void LoadSprite(GorgonSprite sprite, GorgonBinaryReader reader)
        {
            Version version;
            string  imageName = string.Empty;

            sprite.IsV1Sprite = true;

            reader.BaseStream.Position = 0;

            string headerVersion = reader.ReadString();

            if ((!headerVersion.StartsWith("GORSPR", StringComparison.OrdinalIgnoreCase)) || (headerVersion.Length < 7))
            {
                throw new GorgonException(GorgonResult.CannotRead, Resources.GOR2D_SPRITE_CANNOT_READ_V1_SPRITE);
            }

            // Get the version information.
            switch (headerVersion.ToUpperInvariant())
            {
            case "GORSPR1":
                version = new Version(1, 0);
                break;

            case "GORSPR1.1":
                version = new Version(1, 1);
                break;

            case "GORSPR1.2":
                version = new Version(1, 2);
                break;

            default:
                throw new GorgonException(GorgonResult.CannotRead, Resources.GOR2D_SPRITE_CANNOT_READ_V1_SPRITE);
            }

            // We don't need the sprite name.
            reader.ReadString();

            // Find out if we have an image.
            if (reader.ReadBoolean())
            {
                bool isRenderTarget = reader.ReadBoolean();

                imageName = reader.ReadString();

                // We won't be supporting reading render targets from sprites in this version.
                if (isRenderTarget)
                {
                    // Skip the target data.
                    reader.ReadInt32();
                    reader.ReadInt32();
                    reader.ReadInt32();
                    reader.ReadBoolean();
                    reader.ReadBoolean();
                }
            }

            // We don't use "inherited" values anymore.  But we need them because
            // the file doesn't include inherited data.
            bool InheritAlphaMaskFunction     = reader.ReadBoolean();
            bool InheritAlphaMaskValue        = reader.ReadBoolean();
            bool InheritBlending              = reader.ReadBoolean();
            bool InheritHorizontalWrapping    = reader.ReadBoolean();
            bool InheritSmoothing             = reader.ReadBoolean();
            bool InheritStencilCompare        = reader.ReadBoolean();
            bool InheritStencilEnabled        = reader.ReadBoolean();
            bool InheritStencilFailOperation  = reader.ReadBoolean();
            bool InheritStencilMask           = reader.ReadBoolean();
            bool InheritStencilPassOperation  = reader.ReadBoolean();
            bool InheritStencilReference      = reader.ReadBoolean();
            bool InheritStencilZFailOperation = reader.ReadBoolean();
            bool InheritVerticalWrapping      = reader.ReadBoolean();
            bool InheritDepthBias             = true;
            bool InheritDepthTestFunction     = true;
            bool InheritDepthWriteEnabled     = true;

            // Get version 1.1 fields.
            if ((version.Major == 1) && (version.Minor >= 1))
            {
                InheritDepthBias         = reader.ReadBoolean();
                InheritDepthTestFunction = reader.ReadBoolean();
                InheritDepthWriteEnabled = reader.ReadBoolean();
            }

            // Get the size of the sprite.
            sprite.Size = new Vector2(reader.ReadSingle(), reader.ReadSingle());

            // Older versions of the sprite object used pixel space for their texture coordinates.  We will have to
            // fix up these coordinates into texture space once we have a texture loaded.  At this point, there's no guarantee
            // that the texture was loaded safely, so we'll have to defer it until later.
            // Also, older versions used the size the determine the area on the texture to cover.  So use the size to
            // get the texture bounds.
            var textureOffset = new Vector2(reader.ReadSingle(), reader.ReadSingle());

            sprite.TextureOffset = textureOffset;
            sprite.TextureSize   = sprite.Size;

            // Read the anchor.
            sprite.Anchor = new Vector2(reader.ReadSingle(), reader.ReadSingle());

            // Get vertex offsets.
            sprite.SetCornerOffset(RectangleCorner.UpperLeft, new Vector2(reader.ReadSingle(), reader.ReadSingle()));
            sprite.SetCornerOffset(RectangleCorner.UpperRight, new Vector2(reader.ReadSingle(), reader.ReadSingle()));
            sprite.SetCornerOffset(RectangleCorner.LowerRight, new Vector2(reader.ReadSingle(), reader.ReadSingle()));
            sprite.SetCornerOffset(RectangleCorner.LowerLeft, new Vector2(reader.ReadSingle(), reader.ReadSingle()));

            // Get vertex colors.
            sprite.SetCornerColor(RectangleCorner.UpperLeft, new GorgonColor(reader.ReadInt32()));
            sprite.SetCornerColor(RectangleCorner.UpperRight, new GorgonColor(reader.ReadInt32()));
            sprite.SetCornerColor(RectangleCorner.LowerLeft, new GorgonColor(reader.ReadInt32()));
            sprite.SetCornerColor(RectangleCorner.LowerRight, new GorgonColor(reader.ReadInt32()));

            // Skip shader information.  Version 1.0 had shader information attached to the sprite.
            if ((version.Major == 1) && (version.Minor < 1))
            {
                if (reader.ReadBoolean())
                {
                    reader.ReadString();
                    reader.ReadBoolean();
                    if (reader.ReadBoolean())
                    {
                        reader.ReadString();
                    }
                }
            }

            // We no longer have an alpha mask function.
            if (!InheritAlphaMaskFunction)
            {
                reader.ReadInt32();
            }

            if (!InheritAlphaMaskValue)
            {
                // Direct 3D 9 used a value from 0..255 for alpha masking, we use
                // a scalar value so convert to a scalar.
                sprite.AlphaTestValues = new GorgonRangeF(0.0f, reader.ReadInt32() / 255.0f);
            }

            // Set the blending mode.
            if (!InheritBlending)
            {
                sprite.Blending.SourceBlend      = ConvertBlendOpToBlendType(reader.ReadInt32());
                sprite.Blending.DestinationBlend = ConvertBlendOpToBlendType(reader.ReadInt32());
                // Skip the blending mode, this gets detected automatically now.
                reader.ReadInt32();
            }

            // Get alpha blending mode.
            if ((version.Major == 1) && (version.Minor >= 2))
            {
                sprite.Blending.DestinationAlphaBlend = ConvertBlendOpToBlendType(reader.ReadInt32());
                sprite.Blending.SourceAlphaBlend      = ConvertBlendOpToBlendType(reader.ReadInt32());
            }

            // Get horizontal wrapping mode.
            if (!InheritHorizontalWrapping)
            {
                sprite.TextureSampler.HorizontalWrapping = ConvertImageAddressToTextureAddress(reader.ReadInt32());
            }

            // Get smoothing mode.
            if (!InheritSmoothing)
            {
                sprite.TextureSampler.TextureFilter = ConvertSmoothingToFilter(reader.ReadInt32());
            }

            // Get stencil stuff.
            if (!InheritStencilCompare)
            {
                sprite.DepthStencil.FrontFace.ComparisonOperator = ConvertCompare(reader.ReadInt32());
            }
            if (!InheritStencilEnabled)
            {
                // We don't enable stencil in the same way anymore, so skip this value.
                reader.ReadBoolean();
            }
            if (!InheritStencilFailOperation)
            {
                sprite.DepthStencil.FrontFace.FailOperation = ConvertStencilOp(reader.ReadInt32());
            }
            if (!InheritStencilMask)
            {
                sprite.DepthStencil.StencilReadMask = (byte)(reader.ReadInt32() & 0xFF);
            }
            if (!InheritStencilPassOperation)
            {
                sprite.DepthStencil.FrontFace.PassOperation = ConvertStencilOp(reader.ReadInt32());
            }
            if (!InheritStencilReference)
            {
                sprite.DepthStencil.StencilReference = reader.ReadInt32();
            }
            if (!InheritStencilZFailOperation)
            {
                sprite.DepthStencil.FrontFace.DepthFailOperation = ConvertStencilOp(reader.ReadInt32());
            }

            // Get vertical wrapping mode.
            if (!InheritVerticalWrapping)
            {
                sprite.TextureSampler.VerticalWrapping = ConvertImageAddressToTextureAddress(reader.ReadInt32());
            }

            // Get depth info.
            if ((version.Major == 1) && (version.Minor >= 1))
            {
                if (!InheritDepthBias)
                {
                    // Depth bias values are quite different on D3D9 than they are on D3D11, so skip this.
                    reader.ReadSingle();
                }
                if (!InheritDepthTestFunction)
                {
                    sprite.DepthStencil.DepthComparison = ConvertCompare(reader.ReadInt32());
                }
                if (!InheritDepthWriteEnabled)
                {
                    sprite.DepthStencil.IsDepthWriteEnabled = reader.ReadBoolean();
                }

                sprite.TextureSampler.BorderColor = new GorgonColor(reader.ReadInt32());
            }

            // Get flipped flags.
            sprite.HorizontalFlip = reader.ReadBoolean();
            sprite.VerticalFlip   = reader.ReadBoolean();

            // Bind the texture (if we have one bound to this sprite) if it's already loaded, otherwise defer it.
            if (string.IsNullOrEmpty(imageName))
            {
                return;
            }

            sprite.DeferredTextureName = imageName;
        }