/// <summary>
        /// Function to update the displacement map render target.
        /// </summary>
        /// <param name="format">Format to use for the displacement target.</param>
        private void UpdateDisplacementMap(BufferFormat format)
        {
            if (_displacementTarget != null)
            {
                _displacementTarget.Dispose();
            }

            _displacementTarget = null;

            if (_backgroundTarget == null)
            {
                return;
            }

            _displacementTarget = Graphics.Output.CreateRenderTarget("Effect.Displacement.RT", new GorgonRenderTarget2DSettings
            {
                Width              = _backgroundTarget.Settings.Width,
                Height             = _backgroundTarget.Settings.Height,
                DepthStencilFormat = BufferFormat.Unknown,
                Format             = format == BufferFormat.Unknown ? _backgroundTarget.Settings.Format : format,
                Multisampling      = GorgonMultisampling.NoMultiSampling
            });

            _displacementSprite.Texture = _backgroundTarget;
            _displacementSprite.Size    = new Size(_backgroundTarget.Settings.Width, _backgroundTarget.Settings.Height);

            _targetFormat = format;
            _isUpdated    = true;
        }
Esempio n. 2
0
        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Form.ResizeEnd" /> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.EventArgs" /> that contains the event data.</param>
        protected override void OnResizeEnd(EventArgs e)
        {
            base.OnResizeEnd(e);

            var currentImageSize = new Size(_backBuffer.Settings.Width, _backBuffer.Settings.Height);

            // Copy the render target texture to a temporary buffer and resize the main buffer.
            // The copy the temporary buffer back to the main buffer.
            _backupImage.CopySubResource(_backBuffer,
                                         new Rectangle(Point.Empty, currentImageSize));

            _backBuffer.Dispose();
            _backBuffer = _graphics.Output.CreateRenderTarget("BackBuffer", new GorgonRenderTarget2DSettings
            {
                Width  = ClientSize.Width,
                Height = ClientSize.Height,
                Format = BufferFormat.R8G8B8A8_UIntNormal
            });
            _backBuffer.Clear(Color.White);
            _backBuffer.CopySubResource(_backupImage,
                                        new Rectangle(0, 0, _backBuffer.Settings.Width, _backBuffer.Settings.Height));

            // Set the mouse range to the new size.
            _mouse.SetPositionRange(0, 0, ClientSize.Width, ClientSize.Height);
        }
        /// <summary>
        /// Function to free any resources allocated by the effect.
        /// </summary>
        public void FreeResources()
        {
            _backgroundTarget = null;

            if (_displacementTarget == null)
            {
                return;
            }

            _displacementTarget.Dispose();
            _displacementTarget = null;
        }
        /// <summary>
        /// Function to free any resources allocated by the effect.
        /// </summary>
        public void FreeResources()
        {
            if (_hTarget != null)
            {
                _hTarget.Dispose();
            }
            if (_vTarget != null)
            {
                _vTarget.Dispose();
            }

            _hTarget = null;
            _vTarget = null;
        }
        /// <summary>
        /// Function to update the render target.
        /// </summary>
        private void UpdateRenderTarget()
        {
            FreeResources();

            var settings = new GorgonRenderTarget2DSettings
            {
                Width              = BlurRenderTargetsSize.Width,
                Height             = BlurRenderTargetsSize.Height,
                Format             = BlurTargetFormat,
                DepthStencilFormat = BufferFormat.Unknown,
                Multisampling      = GorgonMultisampling.NoMultiSampling
            };

            _hTarget            = Graphics.Output.CreateRenderTarget("Effect.GaussBlur.Target_Horizontal", settings);
            _vTarget            = Graphics.Output.CreateRenderTarget("Effect.GaussBlur.Target_Vertical", settings);
            _blurSprite.Texture = _hTarget;
            _blurSprite.Size    = BlurRenderTargetsSize;
            UpdateOffsets();
        }
Esempio n. 6
0
        /// <summary>
        /// Function to initialize the application.
        /// </summary>
        private static void Initialize()
        {
            _form = new MainForm();
            _form.Show();

            // Create the graphics interface.
            _graphics = new GorgonGraphics();

            // Create the primary swap chain.
            _mainScreen = _graphics.Output.CreateSwapChain("MainScreen", new GorgonSwapChainSettings
            {
                Width      = Settings.Default.ScreenWidth,
                Height     = Settings.Default.ScreenHeight,
                Format     = BufferFormat.R8G8B8A8_UIntNormal,
                Window     = _form,
                IsWindowed = Settings.Default.Windowed
            });

            // Center the display.
            if (_mainScreen.Settings.IsWindowed)
            {
                _form.Location =
                    new Point(
                        _mainScreen.VideoOutput.OutputBounds.Width / 2 - _form.Width / 2 + _mainScreen.VideoOutput.OutputBounds.Left,
                        _mainScreen.VideoOutput.OutputBounds.Height / 2 - _form.Height / 2 + _mainScreen.VideoOutput.OutputBounds.Top);
            }

            // Load the ball texture.
            _ballTexture = _graphics.Textures.FromFile <GorgonTexture2D>("BallTexture", GetResourcePath(@"Textures\Balls\BallsTexture.dds"), new GorgonCodecDDS());

            // Create the 2D interface.
            _2D = _graphics.Output.Create2DRenderer(_mainScreen);
            _2D.IsLogoVisible = true;

            // Set our drawing code to use modulated blending.
            _2D.Drawing.BlendingMode = BlendingMode.Modulate;
            _2D.Drawing.Blending.DestinationAlphaBlend = BlendType.InverseSourceAlpha;

            // Create the wall sprite.
            _wall = _2D.Renderables.CreateSprite("Wall", new Vector2(63, 63), _ballTexture);
            _wall.BlendingMode = BlendingMode.None;

            // Create the ball sprite.
            _ball = _2D.Renderables.CreateSprite("Ball", new Vector2(64, 64), _ballTexture, new Vector2(0.5f, 0.5f));
            _ball.SmoothingMode = SmoothingMode.Smooth;
            _ball.Anchor        = new Vector2(32, 32);
            _ball.Blending.DestinationAlphaBlend = BlendType.InverseSourceAlpha;

            // Create the ball render target.
            _ballTarget = _graphics.Output.CreateRenderTarget("BallTarget", new GorgonRenderTarget2DSettings
            {
                DepthStencilFormat = BufferFormat.Unknown,
                Width         = Settings.Default.ScreenWidth,
                Height        = Settings.Default.ScreenHeight,
                Format        = BufferFormat.R8G8B8A8_UIntNormal,
                Multisampling = GorgonMultisampling.NoMultiSampling
            });
            _2D.Effects.GaussianBlur.BlurRenderTargetsSize = new Size(512, 512);
            _2D.Effects.GaussianBlur.BlurAmount            = 10.0f;

            // Ensure that our secondary camera gets updated.
            _mainScreen.AfterSwapChainResized += (sender, args) =>
            {
                // Fix any objects caught outside of the main target.
                for (int i = 0; i < _ballCount; i++)
                {
                    _ballList[i].Position.X = _ballList[i].Position.X.Max(0).Min(args.Width);
                    _ballList[i].Position.Y = _ballList[i].Position.Y.Max(0).Min(args.Height);
                }

                _ballTarget.Dispose();

                _ballTarget = _graphics.Output.CreateRenderTarget("BallTarget", new GorgonRenderTarget2DSettings
                {
                    DepthStencilFormat = BufferFormat.Unknown,
                    Width         = args.Width,
                    Height        = args.Height,
                    Format        = BufferFormat.R8G8B8A8_UIntNormal,
                    Multisampling = GorgonMultisampling.NoMultiSampling
                });

                Vector2 newTargetSize;
                newTargetSize.X = (512.0f * (args.Width / (float)Settings.Default.ScreenWidth)).Min(512);
                newTargetSize.Y = (512.0f * (args.Height / (float)Settings.Default.ScreenHeight)).Min(512);

                _2D.Effects.GaussianBlur.BlurRenderTargetsSize = (Size)newTargetSize;
                _2D.Effects.Displacement.BackgroundImage       = _ballTarget;
            };

            // Generate the ball list.
            GenerateBalls(Settings.Default.BallCount);

            // Assign event handlers.
            _form.KeyDown += _form_KeyDown;

            // Create statistics render target.
            _statsTarget = _graphics.Output.CreateRenderTarget("Statistics", new GorgonRenderTarget2DSettings
            {
                Width  = 160,
                Height = 66,
                Format = BufferFormat.R8G8B8A8_UIntNormal
            });

            // Draw our stats window frame.
            _2D.Target = _statsTarget;
            _2D.Clear(new GorgonColor(0, 0, 0, 0.5f));
            _2D.Drawing.DrawRectangle(new RectangleF(0, 0, _statsTarget.Settings.Width - 1, _statsTarget.Settings.Height - 1),
                                      new GorgonColor(0.86667f, 0.84314f, 0.7451f, 1.0f));
            _2D.Target = null;

            // Create our font.
            _ballFont = _graphics.Fonts.CreateFont("Arial 9pt Bold", new GorgonFontSettings
            {
                AntiAliasingMode = FontAntiAliasMode.AntiAlias,
                FontStyle        = FontStyle.Bold,
                FontFamilyName   = "Arial",
                FontHeightMode   = FontHeightMode.Points,
                Characters       = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890()_.-+:\u2191\u2193",
                Size             = 9.0f,
                OutlineColor1    = GorgonColor.Black,
                OutlineSize      = 1
            });

            // Statistics text buffer.
            _fpsText  = new StringBuilder(64);
            _helpText = new StringBuilder();
            _helpText.AppendFormat(Resources.HelpText,
                                   _graphics.VideoDevice.Name,
                                   _graphics.VideoDevice.SupportedFeatureLevel,
                                   _graphics.VideoDevice.DedicatedVideoMemory.FormatMemory());

            // Create a static text block.  This will perform MUCH better than drawing the text
            // every frame with DrawString.
            _helpTextSprite          = _2D.Renderables.CreateText("Help Text", _ballFont, _helpText.ToString(), Color.Yellow);
            _helpTextSprite.Position = new Vector2(3, 72);
            _helpTextSprite.Blending.DestinationAlphaBlend = BlendType.InverseSourceAlpha;
        }
Esempio n. 7
0
        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Form.Load"></see> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            try
            {
                // Load the plug-in assembly.
                Gorgon.PlugIns.LoadPlugInAssembly(Program.PlugInPath + "Gorgon.Input.Raw.DLL");

                // Create the factory.
                _input = GorgonInputFactory.CreateInputFactory("GorgonLibrary.Input.GorgonRawPlugIn");

                // Create mouse, keyboard and joystick interfaces.
                _keyboard     = _input.CreateKeyboard(this);
                _mouse        = _input.CreatePointingDevice(this);
                _joystickList = new GorgonJoystick[_input.JoystickDevices.Count];

                // Create each joystick interface.
                for (int i = 0; i < _joystickList.Length; i++)
                {
                    _joystickList[i] = _input.CreateJoystick(this, _input.JoystickDevices[i].Name);
                }

                // Create the graphics interface.
                _graphics = new GorgonGraphics();
                _screen   = _graphics.Output.CreateSwapChain("Screen", new GorgonSwapChainSettings
                {
                    Size       = Settings.Default.Resolution,
                    Format     = BufferFormat.R8G8B8A8_UIntNormal,
                    IsWindowed = Settings.Default.IsWindowed
                });

                // For the backup image. Used to make it as large as the monitor that we're on.
                Screen currentScreen = Screen.FromHandle(Handle);

                // Relocate the window to the center of the screen.
                Location = new Point(currentScreen.Bounds.Left + (currentScreen.WorkingArea.Width / 2) - ClientSize.Width / 2,
                                     currentScreen.Bounds.Top + (currentScreen.WorkingArea.Height / 2) - ClientSize.Height / 2);


                // Create the 2D renderer.
                _2D = _graphics.Output.Create2DRenderer(_screen);

                // Create the text font.
                _font = _graphics.Fonts.CreateFont("Arial_9pt", new GorgonFontSettings
                {
                    FontFamilyName   = "Arial",
                    FontStyle        = FontStyle.Bold,
                    AntiAliasingMode = FontAntiAliasMode.AntiAlias,
                    FontHeightMode   = FontHeightMode.Points,
                    Size             = 9.0f
                });

                // Enable the mouse.
                Cursor                          = Cursors.Cross;
                _mouse.Enabled                  = true;
                _mouse.Exclusive                = false;
                _mouse.PointingDeviceDown      += MouseInput;
                _mouse.PointingDeviceMove      += MouseInput;
                _mouse.PointingDeviceWheelMove += _mouse_PointingDeviceWheelMove;

                // Enable the keyboard.
                _keyboard.Enabled   = true;
                _keyboard.Exclusive = false;
                _keyboard.KeyDown  += _keyboard_KeyDown;

                // Create text sprite.
                _messageSprite       = _2D.Renderables.CreateText("Message", _font, "Using mouse and keyboard.");
                _messageSprite.Color = Color.Black;

                // Create a back buffer.
                _backBuffer = _graphics.Output.CreateRenderTarget("BackBuffer", new GorgonRenderTarget2DSettings
                {
                    Width  = _screen.Settings.Width,
                    Height = _screen.Settings.Height,
                    Format = BufferFormat.R8G8B8A8_UIntNormal
                });
                _backBuffer.Clear(Color.White);

                var settings = new GorgonTexture2DSettings
                {
                    Width  = currentScreen.Bounds.Width,
                    Height = currentScreen.Bounds.Height,
                    Format = BufferFormat.R8G8B8A8_UIntNormal,
                    Usage  = BufferUsage.Staging
                };

                // Clear our backup image to white to match our primary screen.
                _backupImage = _graphics.Textures.CreateTexture("Backup", settings);
                using (var textureData = _backupImage.Lock(BufferLockFlags.Write))
                {
                    textureData.Data.Fill(0xFF);
                }

                // Set the mouse range and position.
                Cursor.Position = PointToScreen(new Point(Settings.Default.Resolution.Width / 2, Settings.Default.Resolution.Height / 2));
                _mouse.SetPosition(Settings.Default.Resolution.Width / 2, Settings.Default.Resolution.Height / 2);
                _mouse.SetPositionRange(0, 0, Settings.Default.Resolution.Width, Settings.Default.Resolution.Height);

                // Set gorgon events.
                _screen.AfterStateTransition += (sender, args) =>
                {
                    OnResizeEnd(EventArgs.Empty);

                    // Reposition after a state change.
                    if (!args.IsWindowed)
                    {
                        return;
                    }

                    Screen monitor = Screen.FromHandle(Handle);
                    Location = new Point(monitor.Bounds.Left + (monitor.WorkingArea.Width / 2) - args.Width / 2,
                                         monitor.Bounds.Top + (monitor.WorkingArea.Height / 2) - args.Height / 2);
                    Cursor.Position = PointToScreen(Point.Round(_mouse.Position));
                };
                Gorgon.ApplicationIdleLoopMethod = Gorgon_Idle;
            }
            catch (Exception ex)
            {
                GorgonException.Catch(ex, () => GorgonDialogs.ErrorBox(this, ex));
                Gorgon.Quit();
            }
        }