Ejemplo n.º 1
0
        public void Run()
        {
            using (var form = new Form())
            {
                _form           = form;
                form.Text       = "ImGui.NET on LightDx";
                form.ClientSize = new Size(800, 600);
                form.KeyDown   += OnKeyDown;
                form.KeyUp     += OnKeyUp;

                using (var device = LightDevice.Create(form))
                {
                    _device = device;

                    var target = new RenderTarget(device.GetDefaultTarget());
                    target.Apply();

                    Pipeline pipeline = device.CompilePipeline(InputTopology.Triangle,
                                                               ShaderSource.FromResource("Shader.fx", ShaderType.Vertex | ShaderType.Pixel));;
                    _pipeline = pipeline;

                    pipeline.SetResource(0, CreateFontTexture());
                    pipeline.SetBlender(Blender.AlphaBlender);

                    pipeline.Apply();

                    var input = pipeline.CreateVertexDataProcessor <Vertex>();
                    _inputDataProcessor = input;

                    var constant = pipeline.CreateConstantBuffer <VSConstant>();
                    pipeline.SetConstant(ShaderType.Vertex, 0, constant);

                    void UpdateWindowSize()
                    {
                        constant.Value.Width  = device.ScreenWidth;
                        constant.Value.Height = device.ScreenHeight;
                        constant.Update();
                    }

                    device.ResolutionChanged += (sender, e) => UpdateWindowSize();
                    UpdateWindowSize();

                    form.Show();
                    device.RunLoop(delegate()
                    {
                        target.ClearAll();

                        RenderFrame();

                        device.Present(true);
                    });
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var form = new Form
            {
                ClientSize = new Size(800, 600),
                Text       = "Tutorial 4: Buffers, Shaders, and HLSL",
            };

            using (var device = LightDevice.Create(form))
            {
                //---------------------
                // Target & Pipeline
                //---------------------

                var target = new RenderTarget(device.GetDefaultTarget());
                target.Apply();

                Pipeline pipeline = device.CompilePipeline(InputTopology.Triangle,
                                                           ShaderSource.FromResource("Color_vs.fx", ShaderType.Vertex),
                                                           ShaderSource.FromResource("Color_ps.fx", ShaderType.Pixel));
                pipeline.Apply();

                //---------------------
                // Vertex buffer
                //---------------------

                var vertexDataProcessor = pipeline.CreateVertexDataProcessor <Vertex>();
                var vertexBuffer        = vertexDataProcessor.CreateImmutableBuffer(new[] {
                    new Vertex {
                        Position = new Vector4(-1, -1, 0, 1), Color = new Vector4(0, 1, 0, 1)
                    },
                    new Vertex {
                        Position = new Vector4(0, 1, 0, 1), Color = new Vector4(0, 1, 0, 1)
                    },
                    new Vertex {
                        Position = new Vector4(1, -1, 0, 1), Color = new Vector4(0, 1, 0, 1)
                    },
                });

                //---------------------
                // Index buffer
                //---------------------

                var indexBuffer = pipeline.CreateImmutableIndexBuffer(new uint[] { 0, 1, 2 });

                //---------------------
                // Constant buffer (VS)
                //---------------------

                var constantBuffer = pipeline.CreateConstantBuffer <Constants>();
                pipeline.SetConstant(ShaderType.Vertex, 0, constantBuffer);

                void SetupProjMatrix()
                {
                    constantBuffer.Value.Projection =
                        device.CreatePerspectiveFieldOfView((float)Math.PI / 4).Transpose();
                }

                device.ResolutionChanged += (sender, e) => SetupProjMatrix();

                constantBuffer.Value.World = Matrix4x4.Identity.Transpose();
                SetupProjMatrix();

                //---------------------
                // Camera
                //---------------------

                var camera = new Camera();

                //---------------------
                // Start main loop
                //---------------------

                form.Show();

                device.RunMultithreadLoop(delegate()
                {
                    // Update matrix

                    constantBuffer.Value.View = camera.GetViewMatrix().Transpose();
                    constantBuffer.Update();

                    // Clear and draw

                    target.ClearAll();
                    indexBuffer.DrawAll(vertexBuffer);

                    device.Present(true);
                });
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var form = new Form
            {
                ClientSize = new Size(800, 600),
                Text       = "Tutorial 7: 3D Model Rendering",
            };

            using (var device = LightDevice.Create(form))
            {
                //---------------------
                // Target & Pipeline
                //---------------------

                var target = new RenderTarget(device.GetDefaultTarget(),
                                              device.CreateDefaultDepthStencilTarget());
                target.Apply();

                Pipeline pipeline = device.CompilePipeline(InputTopology.Triangle,
                                                           ShaderSource.FromResource("Light_vs.fx", ShaderType.Vertex),
                                                           ShaderSource.FromResource("Light_ps.fx", ShaderType.Pixel));
                pipeline.Apply();

                //---------------------
                // Vertex buffer
                //---------------------

                var          vertexDataProcessor = pipeline.CreateVertexDataProcessor <ModelVertex>();
                VertexBuffer vertexBuffer;
                using (var stream = Assembly.GetEntryAssembly().GetManifestResourceStream("Tutorial07.Cube.txt"))
                {
                    vertexBuffer = vertexDataProcessor.CreateImmutableBuffer(Model.ReadModelFile(stream));
                }

                //---------------------
                // Constant buffer (VS)
                //---------------------

                var constantBufferVS = pipeline.CreateConstantBuffer <VSConstants>();
                pipeline.SetConstant(ShaderType.Vertex, 0, constantBufferVS);

                void SetupProjMatrix()
                {
                    constantBufferVS.Value.Projection =
                        device.CreatePerspectiveFieldOfView((float)Math.PI / 4).Transpose();
                }

                device.ResolutionChanged += (sender, e) => SetupProjMatrix();

                constantBufferVS.Value.World = Matrix4x4.Identity.Transpose();
                SetupProjMatrix();

                //---------------------
                // Constant buffer (PS)
                //---------------------

                var constantBufferPS = pipeline.CreateConstantBuffer <PSConstants>();
                pipeline.SetConstant(ShaderType.Pixel, 0, constantBufferPS);

                constantBufferPS.Value.Diffuse  = Color.White.WithAlpha(1);
                constantBufferPS.Value.LightDir = Vector3.Normalize(new Vector3(-3f, -4f, 6f));
                constantBufferPS.Update();

                //---------------------
                // Texture
                //---------------------

                Texture2D tex;
                using (var stream = Assembly.GetEntryAssembly().GetManifestResourceStream("Tutorial07.seafloor.dds"))
                {
                    tex = device.CreateTexture2D(stream);
                }
                pipeline.SetResource(0, tex);

                //---------------------
                // Camera
                //---------------------

                var camera = new Camera();

                //---------------------
                // Start main loop
                //---------------------

                form.Show();

                var frameCounter = new FrameCounter();
                frameCounter.Start();

                device.RunMultithreadLoop(delegate()
                {
                    // Update matrix

                    var time         = frameCounter.NextFrame() / 1000;
                    Matrix4x4 rotate = Matrix4x4.CreateRotationX(time * 3) *
                                       Matrix4x4.CreateRotationY(time * 6) *
                                       Matrix4x4.CreateRotationZ(time * 4);

                    constantBufferVS.Value.World *= rotate;
                    constantBufferVS.Value.View   = camera.GetViewMatrix().Transpose();
                    constantBufferVS.Update();

                    // Clear and draw
                    target.ClearAll();
                    vertexBuffer.DrawAll();

                    device.Present(true);
                });
            }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var form = new Form
            {
                ClientSize = new Size(800, 600),
                Text       = "Tutorial 21: Specular Mapping",
            };

            using (var device = LightDevice.Create(form))
            {
                //---------------------
                // Target & Pipeline
                //---------------------

                var target = new RenderTarget(device.GetDefaultTarget(),
                                              device.CreateDefaultDepthStencilTarget());
                target.Apply();

                Pipeline pipeline = device.CompilePipeline(InputTopology.Triangle,
                                                           ShaderSource.FromResource("SpecMap_vs.fx", ShaderType.Vertex),
                                                           ShaderSource.FromResource("SpecMap_ps.fx", ShaderType.Pixel));
                pipeline.Apply();

                //---------------------
                // Vertex buffer
                //---------------------

                var          vertexDataProcessor = pipeline.CreateVertexDataProcessor <ModelVertex>();
                VertexBuffer vertexBuffer;
                using (var stream = Assembly.GetEntryAssembly().GetManifestResourceStream("Tutorial21.cube.txt"))
                {
                    vertexBuffer = vertexDataProcessor.CreateImmutableBuffer(Model.ReadModelFile(stream));
                }

                //---------------------
                // Constant buffer (Matrix, VS)
                //---------------------

                var matrixBuffer = pipeline.CreateConstantBuffer <MatrixBuffer>();
                pipeline.SetConstant(ShaderType.Vertex, 0, matrixBuffer);

                void SetupProjMatrix()
                {
                    matrixBuffer.Value.Projection =
                        device.CreatePerspectiveFieldOfView((float)Math.PI / 4).Transpose();
                }

                device.ResolutionChanged += (sender, e) => SetupProjMatrix();

                matrixBuffer.Value.World = Matrix4x4.Identity.Transpose();
                SetupProjMatrix();

                //---------------------
                // Constant buffer (Camera, VS)
                //---------------------

                var cameraBuffer = pipeline.CreateConstantBuffer <CameraBuffer>();
                pipeline.SetConstant(ShaderType.Vertex, 1, cameraBuffer);

                //---------------------
                // Constant buffer (Light, PS)
                //---------------------

                var lightBuffer = pipeline.CreateConstantBuffer <LightBuffer>();
                pipeline.SetConstant(ShaderType.Pixel, 0, lightBuffer);

                lightBuffer.Value.DiffuseColor   = Color.White.WithAlpha(1);
                lightBuffer.Value.LightDirection = Vector3.Normalize(new Vector3(0, 0, 1));
                lightBuffer.Value.SpecularColor  = Color.White.WithAlpha(1);
                lightBuffer.Value.SpecularPower  = 16;
                lightBuffer.Update();

                //---------------------
                // Texture
                //---------------------

                Texture2D CreateTextureFromResource(string name)
                {
                    using (var stream = Assembly.GetEntryAssembly().GetManifestResourceStream($"Tutorial21.{name}.dds"))
                    {
                        return(device.CreateTexture2D(stream));
                    }
                }

                var tex1 = CreateTextureFromResource("stone02");
                var tex2 = CreateTextureFromResource("bump02");
                var tex3 = CreateTextureFromResource("spec02");
                pipeline.SetResource(0, tex1);
                pipeline.SetResource(1, tex2);
                pipeline.SetResource(2, tex3);

                //---------------------
                // Camera
                //---------------------

                var camera = new Camera();

                //---------------------
                // Start main loop
                //---------------------

                form.Show();

                var frameCounter = new FrameCounter();
                frameCounter.Start();

                device.RunMultithreadLoop(delegate()
                {
                    // Update matrix buffer

                    var time         = frameCounter.NextFrame() / 1000;
                    Matrix4x4 rotate = Matrix4x4.CreateRotationX(time * 3) *
                                       Matrix4x4.CreateRotationY(time * 6) *
                                       Matrix4x4.CreateRotationZ(time * 4);

                    matrixBuffer.Value.World *= rotate;
                    matrixBuffer.Value.View   = camera.GetViewMatrix().Transpose();
                    matrixBuffer.Update();

                    // Update camera buffer

                    cameraBuffer.Value.CameraPosition = camera.Position;
                    cameraBuffer.Update();

                    // Clear and draw

                    target.ClearAll();
                    vertexBuffer.DrawAll();

                    device.Present(true);
                });
            }
        }
Ejemplo n.º 5
0
 // Free the graphics library
 internal static void Shutdown()
 {
     Texture.ClearAll();
     Shader.ClearAll();
     RenderTarget.ClearAll();
 }