コード例 #1
0
ファイル: GraphicsFramework.cs プロジェクト: tmp7701/Gorgon
        /// <summary>
        /// Initializes a new instance of the <see cref="GraphicsFramework"/> class.
        /// </summary>
        public GraphicsFramework()
        {
            ClearScreen = true;
            GorgonVideoDeviceEnumerator.Enumerate(true, true);

            _form = new TestForm();
            //Graphics = new GorgonGraphics(GorgonVideoDeviceEnumerator.VideoDevices.Single(item => item.VideoDeviceType == VideoDeviceType.ReferenceRasterizer));
            Graphics = new GorgonGraphics();
        }
コード例 #2
0
        public void CleanUp()
        {
            if (_graphics != null)
            {
                _graphics.Dispose();
            }

            _graphics = null;

            if (_form != null)
            {
                _form.Dispose();
            }

            _form = null;
        }
コード例 #3
0
        public void Init()
        {
            _form = new TestForm
            {
                ShowTestPanel = true,
                ClientSize    = new Size(1280, 800)
            };
            _form.WindowState = FormWindowState.Minimized;
            _form.Show();
            _form.WindowState = FormWindowState.Normal;

            _graphics = new GorgonGraphics();
            _swap     = _graphics.Output.CreateSwapChain("Screen", new GorgonSwapChainSettings()
            {
                Window             = _form.panelDisplay,
                DepthStencilFormat = BufferFormat.D24_UIntNormal_S8_UInt
            });

            _swap.AfterSwapChainResized += (sender, args) =>
            {
                var currentMatrix = new MatrixBuffer();

                _graphics.Rasterizer.SetViewport(_swap.Viewport);
                _aspect = (_swap.Settings.VideoMode.Width) / (float)(_swap.Settings.VideoMode.Height);
                currentMatrix.Projection = Matrix.PerspectiveFovLH(100.39f.Radians(), _aspect, 0.1f, 1000.0f);
                currentMatrix.View       = Matrix.LookAtLH(new Vector3(0, 0, -0.75f), new Vector3(0, 0, 1.0f), Vector3.UnitY);

                _graphics.Output.SetRenderTarget(_swap, _swap.DepthStencilBuffer);

                pvw = currentMatrix.View * currentMatrix.Projection;
            };

            _swap.AfterStateTransition += (sender, args) =>
            {
                var currentMatrix = new MatrixBuffer();

                _graphics.Rasterizer.SetViewport(_swap.Viewport);
                _aspect = (_swap.Settings.VideoMode.Width) / (float)(_swap.Settings.VideoMode.Height);
                currentMatrix.Projection = Matrix.PerspectiveFovLH(100.39f.Radians(), _aspect, 0.1f, 1000.0f);
                currentMatrix.View       = Matrix.LookAtLH(new Vector3(0, 0, -0.75f), new Vector3(0, 0, 1.0f), Vector3.UnitY);

                _graphics.Output.SetRenderTarget(_swap, _swap.DepthStencilBuffer);

                pvw = currentMatrix.View * currentMatrix.Projection;
            };

            var button = new Button()
            {
                Text     = "3D",
                Location = new Point(90, 3)
            };

            button.Click += (sender, args) =>
            {
                _3d = !_3d;
                Matrix currentMatrix = Matrix.LookAtLH(new Vector3(0, 0, _camPos), new Vector3(0, 0, 1.0f), Vector3.UnitY);
                Matrix projection    = Matrix.PerspectiveFovLH(100.39f.Radians(), _aspect, 0.1f,
                                                               1000.0f);
                pvw = currentMatrix * projection;
            };

            _form.panelInput.Controls.Add(button);

            _sprite = new vertex[Count * 4];

            for (int i = 0; i < Count; i++)
            {
                _balls[i].Scale       = 1.0f;
                _balls[i].ScaleDelta  = (GorgonRandom.RandomSingle() * 1.5f) + 0.25f;
                _balls[i].AlphaBounce = _balls[i].ScaleBouce = false;
                _balls[i].XBounce     = GorgonRandom.RandomInt32(0, 100) > 50;
                _balls[i].YBounce     = GorgonRandom.RandomInt32(0, 100) > 50;
                _balls[i].ZBounce     = GorgonRandom.RandomInt32(0, 100) > 50;
                _balls[i].Velocity    = new Vector3((GorgonRandom.RandomSingle() * 0.5f), (GorgonRandom.RandomSingle() * 0.5f), (GorgonRandom.RandomSingle() * 0.5f));
                _balls[i].Angle       = 0.0f;
                _balls[i].AngleDelta  = 1.0f;
                _balls[i].Color       = new Vector4(1.0f);
                _balls[i].AlphaDelta  = GorgonRandom.RandomSingle() * 0.5f;
                _balls[i].Checkered   = true;          // GorgonRandom.RandomInt32(0, 100) > 50;
                _balls[i].Position    = new Vector3((GorgonRandom.RandomSingle() * 2.0f) - 1.0f, (GorgonRandom.RandomSingle() * 2.0f) - 1.0f, GorgonRandom.RandomSingle());
            }

            _vs = _graphics.Shaders.CreateShader <GorgonVertexShader>("TestVShader", "VS", _shader, null, true);
            _ps = _graphics.Shaders.CreateShader <GorgonPixelShader>("TestPShader", "PS", _shader, null, true);

            _layout = _graphics.Input.CreateInputLayout("Input", typeof(vertex), _vs);

            int vertexSize = _layout.Size;
            int index      = 0;
            var indices    = new int[Count * 6 * sizeof(int)];

            for (int i = 0; i < indices.Length; i += 6)
            {
                indices[i]     = index;
                indices[i + 1] = index + 1;
                indices[i + 2] = index + 2;
                indices[i + 3] = index + 1;
                indices[i + 4] = index + 3;
                indices[i + 5] = index + 2;
                index         += 4;
            }

            _vertices = _graphics.Buffers.CreateVertexBuffer("Vertex", new GorgonBufferSettings()
            {
                SizeInBytes = 4 * vertexSize * Count,
                Usage       = BufferUsage.Dynamic
            });
            _index = _graphics.Buffers.CreateIndexBuffer("Index", indices, BufferUsage.Immutable);

            _texture  = _graphics.Textures.FromFile <GorgonTexture2D>("Balls", @"..\..\..\..\Resources\BallDemo\BallDemo.png", new GorgonCodecPNG());
            _texture2 = _graphics.Textures.FromFile <GorgonTexture2D>("VBBack", @"..\..\..\..\Resources\Images\VBback.jpg", new GorgonCodecJPEG());

            var matrix = new MatrixBuffer();

            _aspect = _swap.Settings.VideoMode.Width / (float)(_swap.Settings.VideoMode.Height);

            matrix.Projection = Matrix.PerspectiveFovLH(100.39f.Radians(), _aspect, 0.1f,
                                                        1000.0f);
            matrix.View      = Matrix.LookAtLH(new Vector3(0, 0, _camPos), new Vector3(0, 0, 1.0f), Vector3.UnitY);
            matrix.Array     = new Vector4[3];
            matrix.Array[0]  = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
            matrix.Array[1]  = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
            matrix.Array[2]  = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
            matrix.valueType = new TEMPGUY
            {
                value2    = matrix.View,
                tempArray = new Vector4[3]
            };

            _depthStateAlpha.IsDepthEnabled      = false;
            _depthStateAlpha.IsDepthWriteEnabled = false;

            _graphics.Input.Layout = _layout;
            _graphics.Shaders.VertexShader.Current = _vs;
            _graphics.Shaders.PixelShader.Current  = _ps;

            _graphics.Shaders.PixelShader.TextureSamplers.SetRange(0, new[] { GorgonTextureSamplerStates.LinearFilter, GorgonTextureSamplerStates.LinearFilter });

            _graphics.Rasterizer.SetViewport(_swap.Viewport);
            _graphics.Output.DepthStencilState.States = _depthStateAlpha;
            _graphics.Output.SetRenderTarget(_swap, _swap.DepthStencilBuffer);
            _graphics.Input.VertexBuffers[0] = new GorgonVertexBufferBinding(_vertices, vertexSize);
            _graphics.Input.IndexBuffer      = _index;
            _graphics.Shaders.PixelShader.Resources.SetRange(0, new GorgonShaderView[] { _texture, _texture2 });
            _graphics.Output.BlendingState.States = GorgonBlendStates.ModulatedBlending;
            pvw = matrix.valueType.value2 * matrix.Projection;

            _tempStream = new GorgonDataStream(_sprite.Length * vertexSize);
        }
コード例 #4
0
ファイル: GraphicsFramework.cs プロジェクト: tmp7701/Gorgon
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (!disposing)
            {
                return;
            }

            if (Screen != null)
            {
                Screen.Dispose();
                Screen = null;
            }

            if (Indices != null)
            {
                Indices.Dispose();
                Indices = null;
            }

            if (Vertices != null)
            {
                Vertices.Dispose();
                Vertices = null;
            }

            if (Layout != null)
            {
                Layout.Dispose();
                Layout = null;
            }

            if (PixelShader != null)
            {
                PixelShader.Dispose();
                PixelShader = null;
            }

            if (VertexShader != null)
            {
                VertexShader.Dispose();
                VertexShader = null;
            }

            if (Graphics != null)
            {
                Graphics.Dispose();
                Graphics = null;
            }

            if (_form != null)
            {
                _form.Dispose();
                _form = null;
            }

            _disposed = true;
        }