Пример #1
0
        /// <summary>
        /// Function to create the mouse device.
        /// </summary>
        private void CreateMouse()
        {
            // Create the device from the factory.
            _mouse = _factory.CreatePointingDevice(this);

            // Set up the mouse for use.
            _mouse.Enabled = true;

            // Set the mouse as exclusively owned by this window.
            // This way all the mouse input will go to this window when it's got focus.
            _mouse.Exclusive = true;

            // Assign an event to notify us when the mouse is moving.
            _mouse.PointingDeviceMove += _mouse_PointingDeviceMove;

            // Assign another event to notify us when a mouse button was clicked.
            _mouse.PointingDeviceDown += _mouse_PointingDeviceDown;
            _mouse.PointingDeviceUp   += _mouse_PointingDeviceUp;

            // Limit the mouse position to the client area of the window.
            _mouse.PositionRange = new RectangleF(0, 0, panelDisplay.ClientSize.Width, panelDisplay.ClientSize.Height);

            UpdateMouseLabel(_mouse.Position, PointingDeviceButtons.None);
        }
Пример #2
0
        /// <summary>
        /// Function to initialize the application.
        /// </summary>
        private static void Initialize()
        {
            _form = new FormMain();

            _graphics  = new GorgonGraphics();
            _swapChain = _graphics.Output.CreateSwapChain("Swap",
                                                          new GorgonSwapChainSettings
            {
                Window             = _form,
                IsWindowed         = true,
                DepthStencilFormat = BufferFormat.D24_UIntNormal_S8_UInt,
                Format             = BufferFormat.R8G8B8A8_UIntNormal
            });

            _renderer2D = _graphics.Output.Create2DRenderer(_swapChain);

            _font = _graphics.Fonts.CreateFont("AppFont",
                                               new GorgonFontSettings
            {
                FontFamilyName   = "Calibri",
                FontStyle        = FontStyle.Bold,
                FontHeightMode   = FontHeightMode.Pixels,
                AntiAliasingMode = FontAntiAliasMode.AntiAlias,
                OutlineSize      = 1,
                OutlineColor1    = Color.Black,
                Size             = 16.0f
            });

            _vertexShader       = _graphics.Shaders.CreateShader <GorgonVertexShader>("VertexShader", "PrimVS", Resources.Shaders);
            _pixelShader        = _graphics.Shaders.CreateShader <GorgonPixelShader>("PixelShader", "PrimPS", Resources.Shaders);
            _bumpShader         = _graphics.Shaders.CreateShader <GorgonPixelShader>("PixelShader", "PrimPSBump", Resources.Shaders);
            _waterShader        = _graphics.Shaders.CreateShader <GorgonPixelShader>("PixelShader", "PrimPSWaterBump", Resources.Shaders);
            _normalVertexShader = _graphics.Shaders.CreateShader <GorgonVertexShader>("NormalVertexShader", "NormalVS", Resources.Shaders);
            _normalPixelShader  = _graphics.Shaders.CreateShader <GorgonPixelShader>("NormalPixelShader", "NormalPS", Resources.Shaders);
            _vertexLayout       = _graphics.Input.CreateInputLayout("Vertex3D", typeof(Vertex3D), _vertexShader);
            _normalVertexLayout = _graphics.Input.CreateInputLayout("NormalVertex",
                                                                    new[]
            {
                new GorgonInputElement("SV_POSITION",
                                       BufferFormat.R32G32B32A32_Float,
                                       0,
                                       0,
                                       0,
                                       false,
                                       0),
            },
                                                                    _normalVertexShader);

            _graphics.Shaders.VertexShader.Current = _vertexShader;
            _graphics.Shaders.PixelShader.Current  = _pixelShader;
            _graphics.Input.Layout        = _vertexLayout;
            _graphics.Input.PrimitiveType = PrimitiveType.TriangleList;

            _texture       = _graphics.Textures.CreateTexture <GorgonTexture2D>("UVTexture", Resources.UV);
            _earf          = _graphics.Textures.CreateTexture <GorgonTexture2D>("Earf", Resources.earthmap1k);
            _normalMap     = _graphics.Textures.FromMemory <GorgonTexture2D>("RainNRM", Resources.Rain_Height_NRM, new GorgonCodecDDS());
            _normalEarfMap = _graphics.Textures.FromMemory <GorgonTexture2D>("EarfNRM", Resources.earthbump1k_NRM, new GorgonCodecDDS());
            _specMap       = _graphics.Textures.FromMemory <GorgonTexture2D>("RainSPC", Resources.Rain_Height_SPEC, new GorgonCodecDDS());
            _specEarfMap   = _graphics.Textures.CreateTexture <GorgonTexture2D>("EarfSPC", Resources.earthspec1k);
            _cloudMap      = _graphics.Textures.CreateTexture <GorgonTexture2D>("EarfClouds", Resources.earthcloudmap);
            _gorgNrm       = _graphics.Textures.CreateTexture <GorgonTexture2D>("EarfClouds", Resources.normalmap);

            var depth = new GorgonDepthStencilStates
            {
                DepthComparison     = ComparisonOperator.LessEqual,
                IsDepthEnabled      = true,
                IsDepthWriteEnabled = true
            };

            _graphics.Output.DepthStencilState.States = depth;
            _graphics.Output.SetRenderTarget(_swapChain, _swapChain.DepthStencilBuffer);
            _graphics.Rasterizer.States = GorgonRasterizerStates.CullBackFace;
            _graphics.Rasterizer.SetViewport(new GorgonViewport(0, 0, _form.ClientSize.Width, _form.ClientSize.Height, 0, 1.0f));
            _graphics.Shaders.PixelShader.TextureSamplers[0] = GorgonTextureSamplerStates.LinearFilter;

            _wvp = new WorldViewProjection(_graphics);
            _wvp.UpdateProjection(75.0f, _form.ClientSize.Width, _form.ClientSize.Height);

            // When we resize, update the projection and viewport to match our client size.
            _form.Resize += (sender, args) =>
            {
                _graphics.Rasterizer.SetViewport(new GorgonViewport(0, 0, _form.ClientSize.Width, _form.ClientSize.Height, 0, 1.0f));
                _wvp.UpdateProjection(75.0f, _form.ClientSize.Width, _form.ClientSize.Height);
            };

            var     fnU = new Vector3(0.5f, 1.0f, 0);
            var     fnV = new Vector3(1.0f, 1.0f, 0);
            Vector3 faceNormal;

            Vector3.Cross(ref fnU, ref fnV, out faceNormal);
            faceNormal.Normalize();

            _triangle = new Triangle(_graphics, new Vertex3D
            {
                Position = new Vector4(-12.5f, -1.5f, 12.5f, 1),
                Normal   = faceNormal,
                UV       = new Vector2(0, 1.0f)
            }, new Vertex3D
            {
                Position = new Vector4(0, 24.5f, 12.5f, 1),
                Normal   = faceNormal,
                UV       = new Vector2(0.5f, 0.0f)
            }, new Vertex3D
            {
                Position = new Vector4(12.5f, -1.5f, 12.5f, 1),
                Normal   = faceNormal,
                UV       = new Vector2(1.0f, 1.0f)
            })
            {
                Texture  = _texture,
                Position = new Vector3(0, 0, 1.0f)
            };

            _plane = new Plane(_graphics, new Vector2(25.0f, 25.0f), new RectangleF(0, 0, 1.0f, 1.0f), new Vector3(90, 0, 0), 32, 32)
            {
                Position = new Vector3(0, -1.5f, 1.0f),
                Texture  = _texture
            };

            _cube = new Cube(_graphics, new Vector3(1, 1, 1), new RectangleF(0, 0, 1.0f, 1.0f), new Vector3(45.0f, 0, 0), 1, 1)
            {
                Position = new Vector3(0, 0, 1.5f),
                Texture  = _texture
            };

            _sphere = new Sphere(_graphics, 1.0f, new RectangleF(0.0f, 0.0f, 1.0f, 1.0f), Vector3.Zero, 64, 64)
            {
                Position = new Vector3(-2.0f, 1.0f, 0.75f),
                Texture  = _earf
            };

            _clouds = new Sphere(_graphics, 5.175f, new RectangleF(0.0f, 0.0f, 1.0f, 1.0f), Vector3.Zero, 16, 16)
            {
                Position = new Vector3(10, 2, 9.5f),
                Texture  = _cloudMap
            };

            _icoSphere = new IcoSphere(_graphics, 5.0f, new RectangleF(0, 0, 1, 1), Vector3.Zero, 3)
            {
                Rotation = new Vector3(0, -45.0f, 0),
                Position = new Vector3(10, 2, 9.5f),
                Texture  = _earf
            };

            _graphics.Shaders.PixelShader.TextureSamplers[0] = new GorgonTextureSamplerStates
            {
                TextureFilter        = TextureFilter.Linear,
                HorizontalAddressing = TextureAddressing.Wrap,
                VerticalAddressing   = TextureAddressing.Wrap,
                DepthAddressing      = TextureAddressing.Wrap,
                ComparisonFunction   = ComparisonOperator.Always
            };
            _graphics.Shaders.PixelShader.TextureSamplers[2] = new GorgonTextureSamplerStates
            {
                TextureFilter        = TextureFilter.Linear,
                HorizontalAddressing = TextureAddressing.Wrap,
                VerticalAddressing   = TextureAddressing.Wrap,
                DepthAddressing      = TextureAddressing.Wrap,
                ComparisonFunction   = ComparisonOperator.Always
            };

            _graphics.Shaders.PixelShader.TextureSamplers[1] = new GorgonTextureSamplerStates
            {
                TextureFilter        = TextureFilter.Linear,
                HorizontalAddressing = TextureAddressing.Wrap,
                VerticalAddressing   = TextureAddressing.Wrap,
                DepthAddressing      = TextureAddressing.Wrap,
                ComparisonFunction   = ComparisonOperator.Always
            };

            _material = new Material
            {
                UVOffset      = Vector2.Zero,
                SpecularPower = 1.0f
            };

            _materialBuffer = _graphics.Buffers.CreateConstantBuffer("uvOffset", ref _material, BufferUsage.Default);

            _graphics.Shaders.PixelShader.ConstantBuffers[2] = _materialBuffer;

            _light = new Light(_graphics);
            var lightPosition = new Vector3(1.0f, 1.0f, -1.0f);

            _light.UpdateLightPosition(ref lightPosition, 0);
            GorgonColor color = GorgonColor.White;

            _light.UpdateSpecular(ref color, 256.0f, 0);

            lightPosition = new Vector3(-5.0f, 5.0f, 8.0f);
            _light.UpdateLightPosition(ref lightPosition, 1);
            color = Color.Yellow;
            _light.UpdateColor(ref color, 1);
            _light.UpdateSpecular(ref color, 2048.0f, 1);
            _light.UpdateAttenuation(10.0f, 1);

            lightPosition = new Vector3(5.0f, 3.0f, 10.0f);
            _light.UpdateLightPosition(ref lightPosition, 2);
            color = Color.Red;
            _light.UpdateColor(ref color, 2);
            _light.UpdateAttenuation(16.0f, 2);

            var eye    = Vector3.Zero;
            var lookAt = Vector3.UnitZ;
            var up     = Vector3.UnitY;

            _wvp.UpdateViewMatrix(ref eye, ref lookAt, ref up);

            _cameraRotation = Vector2.Zero;

            Gorgon.PlugIns.LoadPlugInAssembly(Application.StartupPath + @"\Gorgon.Input.Raw.dll");

            _input    = GorgonInputFactory.CreateInputFactory("GorgonLibrary.Input.GorgonRawPlugIn");
            _keyboard = _input.CreateKeyboard(_form);
            _mouse    = _input.CreatePointingDevice(_form);

            _keyboard.KeyDown += (sender, args) =>
            {
                if (args.Key == KeyboardKeys.L)
                {
                    _lock = !_lock;
                }
            };

            _mouse.PointingDeviceDown      += Mouse_Down;
            _mouse.PointingDeviceUp        += Mouse_Up;
            _mouse.PointingDeviceWheelMove += (sender, args) =>
            {
                if (args.WheelDelta < 0)
                {
                    _sensitivity -= 0.05f;

                    if (_sensitivity < 0.05f)
                    {
                        _sensitivity = 0.05f;
                    }
                }
                else if (args.WheelDelta > 0)
                {
                    _sensitivity += 0.05f;

                    if (_sensitivity > 2.0f)
                    {
                        _sensitivity = 2.0f;
                    }
                }
            };
            _mouse.PointingDeviceMove += (sender, args) =>
            {
                if (!_mouse.Exclusive)
                {
                    return;
                }

                var delta = args.RelativePosition;
                _cameraRotation.Y      += delta.Y * _sensitivity;                                        //((360.0f * 0.002f) * delta.Y.Sign());
                _cameraRotation.X      += delta.X * _sensitivity;                                        //((360.0f * 0.002f) * delta.X.Sign());
                _mouseStart             = _mouse.Position;
                _mouse.RelativePosition = PointF.Empty;
            };
        }
Пример #3
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();
            }
        }