Пример #1
0
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            int[] indices = new int[]
            {
                0, 1, 2, 3, 4, 5
            };

            Vector3[] vertices = new Vector3[] {
                new Vector3(-50, 0, 50),
                new Vector3(50, 0, 50),
                new Vector3(50, 0, -50),
                new Vector3(-50, 0, 50),
                new Vector3(50, 0, -50),
                new Vector3(-50, 0, -50)
            };


            //render form
            RenderForm form = new RenderForm();

            form.Text = "Tutorial 11: Tesselation";
            SharpFPS fpsCounter = new SharpFPS();


            using (SharpDevice device = new SharpDevice(form))
            {
                //Init mesh
                SharpMesh mesh = SharpMesh.Create <Vector3>(device, vertices, indices);

                //Init shader
                SharpShader shader = new SharpShader(device, "../../HLSL.txt",
                                                     new SharpShaderDescription()
                {
                    VertexShaderFunction   = "VS",
                    PixelShaderFunction    = "PS",
                    GeometryShaderFunction = "GS",
                    HullShaderFunction     = "HS",
                    DomainShaderFunction   = "DS"
                },
                                                     new InputElement[] {
                    new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                });

                //create constant buffer
                Buffer11 buffer = shader.CreateBuffer <Data>();

                fpsCounter.Reset();

                //tessellation value
                int nFactor = 1;
                form.KeyDown += (sender, e) =>
                {
                    if (e.KeyCode == Keys.Up)
                    {
                        nFactor++;
                    }
                    if (e.KeyCode == Keys.Down && nFactor > 1)
                    {
                        nFactor--;
                    }
                    if (e.KeyCode == Keys.W)
                    {
                        device.SetWireframeRasterState();
                    }
                    if (e.KeyCode == Keys.S)
                    {
                        device.SetDefaultRasterState();
                    }
                };

                //main loop
                RenderLoop.Run(form, () =>
                {
                    //Resizing
                    if (device.MustResize)
                    {
                        device.Resize();
                    }

                    //clear color
                    device.Clear(Color.CornflowerBlue);
                    device.UpdateAllStates();

                    float ratio       = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;
                    Matrix projection = Matrix.PerspectiveFovLH(MathUtil.Pi / 3.0F, ratio, 1, 1000);
                    Matrix view       = Matrix.LookAtLH(new Vector3(0, 30, -80), new Vector3(), Vector3.UnitY);
                    Matrix world      = Matrix.RotationY(Environment.TickCount / 2000.0F);
                    Matrix WVP        = world * view * projection;

                    device.UpdateData <Data>(buffer, new Data()
                    {
                        world    = world,
                        viewProj = view * projection,
                        factor   = new Vector4(nFactor, nFactor, 0, 0)
                    });
                    device.DeviceContext.VertexShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.PixelShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.GeometryShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.HullShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.DomainShader.SetConstantBuffer(0, buffer);

                    shader.Apply();

                    //draw mesh as patch
                    mesh.DrawPatch(SharpDX.Direct3D.PrimitiveTopology.PatchListWith3ControlPoints);

                    //remove unused shader
                    shader.Clear();

                    //begin drawing text
                    device.Font.Begin();

                    //draw string
                    fpsCounter.Update();
                    device.Font.DrawString("FPS: " + fpsCounter.FPS, 0, 0);
                    device.Font.DrawString("Tessellation Factor: " + nFactor, 0, 30);
                    device.Font.DrawString("Press Up And Down to change Tessellation Factor,W and S to switch to wireframe ", 0, 60);

                    //flush text to view
                    device.Font.End();
                    //present
                    device.Present();
                });


                //release resource
                mesh.Dispose();
                buffer.Dispose();
                shader.Dispose();
            }
        }
Пример #2
0
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            //render form
            RenderForm form = new RenderForm();

            form.Text = "Tutorial 6: Rasterizer & Alphablending";
            //frame rate counter
            SharpFPS fpsCounter = new SharpFPS();


            using (SharpDevice device = new SharpDevice(form))
            {
                //load font
                SharpBatch font = new SharpBatch(device, "textfont.dds");

                //init mesh
                int[] indices = new int[]
                {
                    0, 1, 2, 0, 2, 3,
                    4, 6, 5, 4, 7, 6,
                    8, 9, 10, 8, 10, 11,
                    12, 14, 13, 12, 15, 14,
                    16, 18, 17, 16, 19, 18,
                    20, 21, 22, 20, 22, 23
                };

                TexturedVertex[] vertices = new[]
                {
                    ////TOP
                    new TexturedVertex(new Vector3(-5, 5, 5), new Vector2(1, 1)),
                    new TexturedVertex(new Vector3(5, 5, 5), new Vector2(0, 1)),
                    new TexturedVertex(new Vector3(5, 5, -5), new Vector2(0, 0)),
                    new TexturedVertex(new Vector3(-5, 5, -5), new Vector2(1, 0)),
                    //BOTTOM
                    new TexturedVertex(new Vector3(-5, -5, 5), new Vector2(1, 1)),
                    new TexturedVertex(new Vector3(5, -5, 5), new Vector2(0, 1)),
                    new TexturedVertex(new Vector3(5, -5, -5), new Vector2(0, 0)),
                    new TexturedVertex(new Vector3(-5, -5, -5), new Vector2(1, 0)),
                    //LEFT
                    new TexturedVertex(new Vector3(-5, -5, 5), new Vector2(0, 1)),
                    new TexturedVertex(new Vector3(-5, 5, 5), new Vector2(0, 0)),
                    new TexturedVertex(new Vector3(-5, 5, -5), new Vector2(1, 0)),
                    new TexturedVertex(new Vector3(-5, -5, -5), new Vector2(1, 1)),
                    //RIGHT
                    new TexturedVertex(new Vector3(5, -5, 5), new Vector2(1, 1)),
                    new TexturedVertex(new Vector3(5, 5, 5), new Vector2(1, 0)),
                    new TexturedVertex(new Vector3(5, 5, -5), new Vector2(0, 0)),
                    new TexturedVertex(new Vector3(5, -5, -5), new Vector2(0, 1)),
                    //FRONT
                    new TexturedVertex(new Vector3(-5, 5, 5), new Vector2(1, 0)),
                    new TexturedVertex(new Vector3(5, 5, 5), new Vector2(0, 0)),
                    new TexturedVertex(new Vector3(5, -5, 5), new Vector2(0, 1)),
                    new TexturedVertex(new Vector3(-5, -5, 5), new Vector2(1, 1)),
                    //BACK
                    new TexturedVertex(new Vector3(-5, 5, -5), new Vector2(0, 0)),
                    new TexturedVertex(new Vector3(5, 5, -5), new Vector2(1, 0)),
                    new TexturedVertex(new Vector3(5, -5, -5), new Vector2(1, 1)),
                    new TexturedVertex(new Vector3(-5, -5, -5), new Vector2(0, 1))
                };

                SharpMesh mesh = SharpMesh.Create <TexturedVertex>(device, vertices, indices);

                //init shader
                SharpShader shader = new SharpShader(device, "../../HLSL.txt",
                                                     new SharpShaderDescription()
                {
                    VertexShaderFunction = "VS", PixelShaderFunction = "PS"
                },
                                                     new InputElement[] {
                    new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                    new InputElement("TEXCOORD", 0, Format.R32G32_Float, 12, 0)
                });

                //init constant buffer
                Buffer11 buffer = shader.CreateBuffer <Matrix>();

                //load Shader Resouce View from file
                //it contains texture for using inside shaders
                ShaderResourceView texture = ShaderResourceView.FromFile(device.Device, "../../texture.dds");

                //init frame rate counter
                fpsCounter.Reset();

                //keyboard event
                //change depth and rasterizer state
                form.KeyDown += (sender, e) =>
                {
                    switch (e.KeyCode)
                    {
                    case Keys.W:
                        device.SetWireframeRasterState();
                        device.SetDefaultBlendState();
                        break;

                    case Keys.S:
                        device.SetDefaultRasterState();
                        break;

                    case Keys.D1:
                        device.SetDefaultBlendState();
                        break;

                    case Keys.D2:
                        device.SetBlend(BlendOperation.Add, BlendOption.InverseSourceAlpha, BlendOption.SourceAlpha);
                        break;

                    case Keys.D3:
                        device.SetBlend(BlendOperation.Add, BlendOption.SourceAlpha, BlendOption.InverseSourceAlpha);
                        break;

                    case Keys.D4:
                        device.SetBlend(BlendOperation.Add, BlendOption.SourceColor, BlendOption.InverseSourceColor);
                        break;

                    case Keys.D5:
                        device.SetBlend(BlendOperation.Add, BlendOption.SourceColor, BlendOption.DestinationColor);
                        break;
                    }
                };

                //main loop
                RenderLoop.Run(form, () =>
                {
                    //Resizing
                    if (device.MustResize)
                    {
                        device.Resize();
                        font.Resize();
                    }


                    //apply state
                    device.UpdateAllStates();

                    //clear color
                    device.Clear(Color.CornflowerBlue);

                    //apply shader
                    shader.Apply();

                    //apply constant buffer to shader
                    device.DeviceContext.VertexShader.SetConstantBuffer(0, buffer);

                    //set texture
                    device.DeviceContext.PixelShader.SetShaderResource(0, texture);

                    //set transformation matrix
                    float ratio = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;

                    //projection matrix
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1, 1000);

                    //view matrix (camera)
                    Matrix view = Matrix.LookAtLH(new Vector3(0, 10, -40), new Vector3(), Vector3.UnitY);

                    //world matrix
                    Matrix world = Matrix.RotationY(Environment.TickCount / 1000.0F);


                    Matrix worldViewProjection = world * view * projection;

                    //set world view projection matrix inside constant buffer
                    device.UpdateData <Matrix>(buffer, worldViewProjection);

                    //draw mesh
                    mesh.Draw();

                    //Second Mesh
                    world = Matrix.RotationY(Environment.TickCount / 1000.0F) * Matrix.Translation(5, 0, -15);
                    worldViewProjection = world * view * projection;
                    //refresh constant buffer
                    device.UpdateData <Matrix>(buffer, worldViewProjection);

                    //draw second mesh
                    mesh.Draw();

                    //Third Mesh
                    world = Matrix.RotationY(Environment.TickCount / 1000.0F) * Matrix.Translation(-5, 0, -15);
                    worldViewProjection = world * view * projection;
                    //refresh constant buffer
                    device.UpdateData <Matrix>(buffer, worldViewProjection);

                    //draw third mesh
                    mesh.Draw();


                    //begin drawing text
                    font.Begin();

                    //draw string
                    fpsCounter.Update();
                    font.DrawString("FPS: " + fpsCounter.FPS, 0, 0, Color.White);
                    font.DrawString("Press W for wireframe, S for solid", 0, 30, Color.White);
                    font.DrawString("Press From 1 to 5 for Alphablending", 0, 60, Color.White);

                    //flush text to view
                    font.End();
                    //present
                    device.Present();
                });


                //release resource
                font.Dispose();
                mesh.Dispose();
                buffer.Dispose();
                texture.Dispose();
            }
        }
Пример #3
0
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            RenderForm form = new RenderForm();

            form.Text = "Tutorial 19: Adaptive Tesselation";
            SharpFPS fpsCounter = new SharpFPS();

            using (SharpDevice device = new SharpDevice(form))
            {
                InputElement[] description = new InputElement[]
                {
                    new InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0, 0),
                    new InputElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float, 12, 0),
                };

                SharpShader shader = new SharpShader(device, "../../Shader.hlsl",
                                                     new SharpShaderDescription()
                {
                    VertexShaderFunction = "VSMain",
                    PixelShaderFunction  = "PSMain",
                    DomainShaderFunction = "DSMain",
                    HullShaderFunction   = "HSMain"
                }, description);



                int   seqX  = 64;
                int   seqY  = 64;
                float size  = 100;
                float sizeW = seqX * size;
                float sizeH = seqY * size;

                TessellationVertex[] vertices = new TessellationVertex[4356];
                int[] indices = new int[63504];
                int   k       = 0;
                for (int y = -1; y < seqY + 1; y++)
                {
                    for (int x = -1; x < seqX + 1; x++)
                    {
                        float vX = x * size - (seqX / 2.0F) * size;
                        float vY = y * size - (seqY / 2.0F) * size;
                        float vZ = 0;

                        TessellationVertex v = new TessellationVertex(vX, vY, vZ, x / 4.0F, y / 4.0F);
                        vertices[k] = v;
                        k++;
                    }
                }

                //indici
                k = 0;
                for (int y = 0; y < seqY - 1; y++)
                {
                    for (int x = 0; x < seqX - 1; x++)
                    {
                        int startX = x + 1;
                        int startY = y + 1;

                        for (int j = -1; j < 3; j++)
                        {
                            for (int i = -1; i < 3; i++)
                            {
                                indices[k] = (i + startX + (seqX + 2) * (j + startY));
                                k++;
                            }
                        }
                    }
                }

                SharpMesh mesh = SharpMesh.Create <TessellationVertex>(device, vertices, indices);

                string path = @"../../../Models/adaptive_tess/";

                ShaderResourceView diffuseMap = device.LoadTextureFromFile(path + "D.dds");
                ShaderResourceView normalMap  = device.LoadTextureFromFile(path + "N.dds");
                ShaderResourceView heightMap  = device.LoadTextureFromFile(path + "H.dds");

                Buffer11 buffer = shader.CreateBuffer <SceneData>();

                fpsCounter.Reset();

                float angle     = 3.14F;
                float distance  = 1200;
                float heightPos = 500;


                form.KeyDown += (sender, e) =>
                {
                    switch (e.KeyCode)
                    {
                    case Keys.A:
                        distance -= 5;
                        if (distance < 100)
                        {
                            distance = 100;
                        }
                        break;

                    case Keys.Z:
                        distance += 5;
                        if (distance > 2000)
                        {
                            distance = 2000;
                        }
                        break;

                    case Keys.Up:
                        heightPos--;
                        if (heightPos < 50)
                        {
                            heightPos = 50;
                        }
                        break;

                    case Keys.Down:
                        heightPos++;
                        if (heightPos > 800)
                        {
                            heightPos = 800;
                        }
                        break;

                    case Keys.Left:
                        angle -= 0.01F;
                        break;

                    case Keys.Right:
                        angle += 0.01F;
                        break;

                    case Keys.W:
                        device.SetWireframeRasterState();
                        break;

                    case Keys.S:
                        device.SetDefaultRasterState();
                        break;
                    }
                };

                //main loop
                RenderLoop.Run(form, () =>
                {
                    //Resizing
                    if (device.MustResize)
                    {
                        device.Resize();
                    }

                    //apply states
                    device.UpdateAllStates();

                    //clear color
                    device.Clear(Color.CornflowerBlue);

                    //Set matrices
                    float ratio = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;

                    Vector3 vAt = new Vector3((float)Math.Cos(angle) * distance, (float)Math.Sin(angle) * distance, heightPos);
                    Vector3 vTo = new Vector3(0, 0, 0);
                    Vector3 vUp = new Vector3(0, 0, 1);

                    Matrix view = Matrix.LookAtLH(vAt, vTo, vUp);
                    Matrix proj = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1.0F, 50000);

                    Vector3 lightDir = new Vector3(1, 0, -2);
                    lightDir.Normalize();

                    Matrix WVP = view * proj;
                    WVP.Transpose();

                    //update constant buffer
                    SceneData data = new SceneData()
                    {
                        Transform      = WVP,
                        LightDirection = new Vector4(lightDir, 0),
                        ViewAt         = new Vector4(vAt, 0)
                    };
                    device.UpdateData <SceneData>(buffer, data);

                    //pass constant buffer to shader
                    device.DeviceContext.VertexShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.PixelShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.HullShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.DomainShader.SetConstantBuffer(0, buffer);

                    //set map to shader
                    device.DeviceContext.DomainShader.SetShaderResource(0, heightMap);
                    device.DeviceContext.PixelShader.SetShaderResource(0, diffuseMap);
                    device.DeviceContext.PixelShader.SetShaderResource(1, normalMap);

                    //apply shader
                    shader.Apply();

                    //draw mesh
                    mesh.DrawPatch(SharpDX.Direct3D.PrimitiveTopology.PatchListWith16ControlPoints);

                    //begin drawing text
                    device.Font.Begin();

                    //draw string
                    fpsCounter.Update();
                    device.Font.DrawString("FPS: " + fpsCounter.FPS, 0, 0);

                    device.Font.DrawString("Presso Up,Down,Left,Right,A,Z to move camera", 0, 20);
                    device.Font.DrawString("Presso W and S to Switch to Wireframe", 0, 40);


                    //flush text to view
                    device.Font.End();
                    //present
                    device.Present();
                });
            }
        }
Пример #4
0
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            int[] indices = new int[]
            {
                0,1,2,3,4,5
            };

            Vector3[] vertices = new Vector3[]{
                new Vector3(-50, 0, 50),
                new Vector3(50, 0, 50),
                new Vector3(50, 0, -50),
                new Vector3(-50, 0, 50),
                new Vector3(50, 0, -50),
                new Vector3(-50, 0, -50)
            };

            //render form
            RenderForm form = new RenderForm();
            form.Text = "Tutorial 11: Tesselation";
            SharpFPS fpsCounter = new SharpFPS();

            using (SharpDevice device = new SharpDevice(form))
            {

                //Init mesh
                SharpMesh mesh = SharpMesh.Create<Vector3>(device, vertices, indices);

                //Init shader
                SharpShader shader = new SharpShader(device, "../../HLSL.txt",
                    new SharpShaderDescription()
                    {
                        VertexShaderFunction = "VS",
                        PixelShaderFunction = "PS",
                        GeometryShaderFunction = "GS",
                        HullShaderFunction = "HS",
                        DomainShaderFunction = "DS"
                    },
                    new InputElement[] {
                        new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                    });

                //create constant buffer
                Buffer11 buffer = shader.CreateBuffer<Data>();

                fpsCounter.Reset();

                //tessellation value
                int nFactor = 1;
                form.KeyDown += (sender, e) =>
                {
                    if (e.KeyCode == Keys.Up)
                        nFactor++;
                    if (e.KeyCode == Keys.Down && nFactor > 1)
                        nFactor--;
                    if (e.KeyCode == Keys.W)
                        device.SetWireframeRasterState();
                    if (e.KeyCode == Keys.S)
                        device.SetDefaultRasterState();
                };

                //main loop
                RenderLoop.Run(form, () =>
                {
                    //Resizing
                    if (device.MustResize)
                    {
                        device.Resize();
                    }

                    //clear color
                    device.Clear(Color.CornflowerBlue);
                    device.UpdateAllStates();

                    float ratio = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;
                    Matrix projection = Matrix.PerspectiveFovLH(MathUtil.Pi / 3.0F, ratio, 1, 1000);
                    Matrix view = Matrix.LookAtLH(new Vector3(0, 30, -80), new Vector3(), Vector3.UnitY);
                    Matrix world = Matrix.RotationY(Environment.TickCount / 2000.0F);
                    Matrix WVP = world * view * projection;

                    device.UpdateData<Data>(buffer, new Data()
                    {
                        world = world,
                        viewProj = view * projection,
                        factor = new Vector4(nFactor, nFactor, 0, 0)
                    });
                    device.DeviceContext.VertexShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.PixelShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.GeometryShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.HullShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.DomainShader.SetConstantBuffer(0, buffer);

                    shader.Apply();

                    //draw mesh as patch
                    mesh.DrawPatch(SharpDX.Direct3D.PrimitiveTopology.PatchListWith3ControlPoints);

                    //remove unused shader
                    shader.Clear();

                    //begin drawing text
                    device.Font.Begin();

                    //draw string
                    fpsCounter.Update();
                    device.Font.DrawString("FPS: " + fpsCounter.FPS, 0, 0);
                    device.Font.DrawString("Tessellation Factor: " + nFactor, 0, 30);
                    device.Font.DrawString("Press Up And Down to change Tessellation Factor,W and S to switch to wireframe ", 0, 60);

                    //flush text to view
                    device.Font.End();
                    //present
                    device.Present();
                });

                //release resource
                mesh.Dispose();
                buffer.Dispose();
                shader.Dispose();
            }
        }
Пример #5
0
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            //render form
            RenderForm form = new RenderForm();
            form.Text = "Tutorial 6: Rasterizer & Alphablending";
            //frame rate counter
            SharpFPS fpsCounter = new SharpFPS();

            using (SharpDevice device = new SharpDevice(form))
            {
                //init mesh
                int[] indices = new int[]
                {
                    0,1,2,0,2,3,
                    4,6,5,4,7,6,
                    8,9,10,8,10,11,
                    12,14,13,12,15,14,
                    16,18,17,16,19,18,
                    20,21,22,20,22,23
                };

                TexturedVertex[] vertices = new[]
                {
                    ////TOP
                    new TexturedVertex(new Vector3(-5,5,5),new Vector2(1,1)),
                    new TexturedVertex(new Vector3(5,5,5),new Vector2(0,1)),
                    new TexturedVertex(new Vector3(5,5,-5),new Vector2(0,0)),
                    new TexturedVertex(new Vector3(-5,5,-5),new Vector2(1,0)),
                    //BOTTOM
                    new TexturedVertex(new Vector3(-5,-5,5),new Vector2(1,1)),
                    new TexturedVertex(new Vector3(5,-5,5),new Vector2(0,1)),
                    new TexturedVertex(new Vector3(5,-5,-5),new Vector2(0,0)),
                    new TexturedVertex(new Vector3(-5,-5,-5),new Vector2(1,0)),
                    //LEFT
                    new TexturedVertex(new Vector3(-5,-5,5),new Vector2(0,1)),
                    new TexturedVertex(new Vector3(-5,5,5),new Vector2(0,0)),
                    new TexturedVertex(new Vector3(-5,5,-5),new Vector2(1,0)),
                    new TexturedVertex(new Vector3(-5,-5,-5),new Vector2(1,1)),
                    //RIGHT
                    new TexturedVertex(new Vector3(5,-5,5),new Vector2(1,1)),
                    new TexturedVertex(new Vector3(5,5,5),new Vector2(1,0)),
                    new TexturedVertex(new Vector3(5,5,-5),new Vector2(0,0)),
                    new TexturedVertex(new Vector3(5,-5,-5),new Vector2(0,1)),
                    //FRONT
                    new TexturedVertex(new Vector3(-5,5,5),new Vector2(1,0)),
                    new TexturedVertex(new Vector3(5,5,5),new Vector2(0,0)),
                    new TexturedVertex(new Vector3(5,-5,5),new Vector2(0,1)),
                    new TexturedVertex(new Vector3(-5,-5,5),new Vector2(1,1)),
                    //BACK
                    new TexturedVertex(new Vector3(-5,5,-5),new Vector2(0,0)),
                    new TexturedVertex(new Vector3(5,5,-5),new Vector2(1,0)),
                    new TexturedVertex(new Vector3(5,-5,-5),new Vector2(1,1)),
                    new TexturedVertex(new Vector3(-5,-5,-5),new Vector2(0,1))
                };

                SharpMesh mesh = SharpMesh.Create<TexturedVertex>(device, vertices, indices);

                //init shader
                SharpShader shader = new SharpShader(device, "../../HLSL.txt",
                    new SharpShaderDescription() { VertexShaderFunction = "VS", PixelShaderFunction = "PS" },
                    new InputElement[] {
                        new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                        new InputElement("TEXCOORD", 0, Format.R32G32_Float, 12, 0)
                    });

                //init constant buffer
                Buffer11 buffer = shader.CreateBuffer<Matrix>();

                //load Shader Resouce View from file
                //it contains texture for using inside shaders
                ShaderResourceView texture = device.LoadTextureFromFile("../../texture.dds");

                //init frame rate counter
                fpsCounter.Reset();

                //keyboard event
                //change depth and rasterizer state
                form.KeyDown += (sender, e) =>
                {
                    switch (e.KeyCode)
                    {
                        case Keys.W:
                            device.SetWireframeRasterState();
                            device.SetDefaultBlendState();
                            break;
                        case Keys.S:
                            device.SetDefaultRasterState();
                            break;
                        case Keys.D1:
                            device.SetDefaultBlendState();
                            break;
                        case Keys.D2:
                            device.SetBlend(BlendOperation.Add, BlendOption.InverseSourceAlpha, BlendOption.SourceAlpha);
                            break;
                        case Keys.D3:
                            device.SetBlend(BlendOperation.Add, BlendOption.SourceAlpha, BlendOption.InverseSourceAlpha);
                            break;
                        case Keys.D4:
                            device.SetBlend(BlendOperation.Add, BlendOption.SourceColor, BlendOption.InverseSourceColor);
                            break;
                        case Keys.D5:
                            device.SetBlend(BlendOperation.Add, BlendOption.SourceColor, BlendOption.DestinationColor);
                            break;
                    }
                };

                //main loop
                RenderLoop.Run(form, () =>
                {
                    //Resizing
                    if (device.MustResize)
                    {
                        device.Resize();
                    }

                    //apply state
                    device.UpdateAllStates();

                    //clear color
                    device.Clear(Color.CornflowerBlue);

                    //apply shader
                    shader.Apply();

                    //apply constant buffer to shader
                    device.DeviceContext.VertexShader.SetConstantBuffer(0, buffer);

                    //set texture
                    device.DeviceContext.PixelShader.SetShaderResource(0, texture);

                    //set transformation matrix
                    float ratio = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;

                    //projection matrix
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1, 1000);

                    //view matrix (camera)
                    Matrix view = Matrix.LookAtLH(new Vector3(0, 10, -40), new Vector3(), Vector3.UnitY);

                    //world matrix
                    Matrix world = Matrix.RotationY(Environment.TickCount / 1000.0F);

                    Matrix worldViewProjection = world * view * projection;

                    //set world view projection matrix inside constant buffer
                    device.UpdateData<Matrix>(buffer, worldViewProjection);

                    //draw mesh
                    mesh.Draw();

                    //Second Mesh
                    world = Matrix.RotationY(Environment.TickCount / 1000.0F) * Matrix.Translation(5, 0, -15);
                    worldViewProjection = world * view * projection;
                    //refresh constant buffer
                    device.UpdateData<Matrix>(buffer, worldViewProjection);

                    //draw second mesh
                    mesh.Draw();

                    //Third Mesh
                    world = Matrix.RotationY(Environment.TickCount / 1000.0F) * Matrix.Translation(-5, 0, -15);
                    worldViewProjection = world * view * projection;
                    //refresh constant buffer
                    device.UpdateData<Matrix>(buffer, worldViewProjection);

                    //draw third mesh
                    mesh.Draw();

                    //begin drawing text
                    device.Font.Begin();

                    //draw string
                    fpsCounter.Update();
                    device.Font.DrawString("FPS: " + fpsCounter.FPS, 0, 0);
                    device.Font.DrawString("Press W for wireframe, S for solid", 0, 30);
                    device.Font.DrawString("Press From 1 to 5 for Alphablending", 0, 60);

                    //flush text to view
                    device.Font.End();
                    //present
                    device.Present();

                });

                //release resource
                mesh.Dispose();
                buffer.Dispose();
                texture.Dispose();
            }
        }
Пример #6
0
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            RenderForm form = new RenderForm();
            form.Text = "Tutorial 19: Adaptive Tesselation";
            SharpFPS fpsCounter = new SharpFPS();

            using (SharpDevice device = new SharpDevice(form))
            {
                SharpBatch font = new SharpBatch(device, "textfont.dds");

                InputElement[] description = new InputElement[]
                {
                    new InputElement("POSITION",0,SharpDX.DXGI.Format.R32G32B32_Float,0,0),
                    new InputElement("TEXCOORD",0,SharpDX.DXGI.Format.R32G32_Float,12,0),
                };

                SharpShader shader = new SharpShader(device, "../../Shader.hlsl",
                    new SharpShaderDescription()
                    {
                        VertexShaderFunction = "VSMain",
                        PixelShaderFunction = "PSMain",
                        DomainShaderFunction = "DSMain",
                        HullShaderFunction = "HSMain"
                    }, description);

                int seqX = 64;
                int seqY = 64;
                float size = 100;
                float sizeW = seqX * size;
                float sizeH = seqY * size;

                TessellationVertex[] vertices = new TessellationVertex[4356];
                int[] indices = new int[63504];
                int k = 0;
                for (int y = -1; y < seqY + 1; y++)
                {
                    for (int x = -1; x < seqX + 1; x++)
                    {
                        float vX = x * size - (seqX / 2.0F) * size;
                        float vY = y * size - (seqY / 2.0F) * size;
                        float vZ = 0;

                        TessellationVertex v = new TessellationVertex(vX, vY, vZ, x / 4.0F, y / 4.0F);
                        vertices[k] = v;
                        k++;
                    }
                }

                //indici
                k = 0;
                for (int y = 0; y < seqY - 1; y++)
                {
                    for (int x = 0; x < seqX - 1; x++)
                    {
                        int startX = x + 1;
                        int startY = y + 1;

                        for (int j = -1; j < 3; j++)
                        {
                            for (int i = -1; i < 3; i++)
                            {
                                indices[k] = (i + startX + (seqX + 2) * (j + startY));
                                k++;
                            }
                        }
                    }
                }

                SharpMesh mesh = SharpMesh.Create<TessellationVertex>(device, vertices, indices);

                string path = @"../../../Models/adaptive_tess/";

                ShaderResourceView diffuseMap = ShaderResourceView.FromFile(device.Device, path + "D.dds");
                ShaderResourceView normalMap = ShaderResourceView.FromFile(device.Device, path + "N.dds");
                ShaderResourceView heightMap = ShaderResourceView.FromFile(device.Device, path + "H.dds");

                Buffer11 buffer = shader.CreateBuffer<SceneData>();

                fpsCounter.Reset();

                float angle = 3.14F;
                float distance = 1200;
                float heightPos = 500;

                form.KeyDown += (sender, e) =>
                {
                    switch (e.KeyCode)
                    {
                        case Keys.A:
                            distance -= 5;
                            if (distance < 100) distance = 100;
                            break;
                        case Keys.Z:
                            distance += 5;
                            if (distance > 2000) distance = 2000;
                            break;
                        case Keys.Up:
                            heightPos--;
                            if (heightPos < 50) heightPos = 50;
                            break;
                        case Keys.Down:
                            heightPos++;
                            if (heightPos > 800) heightPos = 800;
                            break;
                        case Keys.Left:
                            angle -= 0.01F;
                            break;
                        case Keys.Right:
                            angle += 0.01F;
                            break;
                        case Keys.W:
                            device.SetWireframeRasterState();
                            break;
                        case Keys.S:
                            device.SetDefaultRasterState();
                            break;
                    }
                };

                //main loop
                RenderLoop.Run(form, () =>
                {
                    //Resizing
                    if (device.MustResize)
                    {
                        device.Resize();
                        font.Resize();
                    }

                    //apply states
                    device.UpdateAllStates();

                    //clear color
                    device.Clear(Color.CornflowerBlue);

                    //Set matrices
                    float ratio = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;

                    Vector3 vAt = new Vector3((float)Math.Cos(angle) * distance, (float)Math.Sin(angle) * distance, heightPos);
                    Vector3 vTo = new Vector3(0, 0, 0);
                    Vector3 vUp = new Vector3(0, 0, 1);

                    Matrix view = Matrix.LookAtLH(vAt, vTo, vUp);
                    Matrix proj = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1.0F, 50000);

                    Vector3 lightDir = new Vector3(1, 0, -2);
                    lightDir.Normalize();

                    Matrix WVP = view * proj;
                    WVP.Transpose();

                    //update constant buffer
                    SceneData data = new SceneData()
                    {
                        Transform = WVP,
                        LightDirection = new Vector4(lightDir, 0),
                        ViewAt = new Vector4(vAt, 0)
                    };
                    device.UpdateData<SceneData>(buffer, data);

                    //pass constant buffer to shader
                    device.DeviceContext.VertexShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.PixelShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.HullShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.DomainShader.SetConstantBuffer(0, buffer);

                    //set map to shader
                    device.DeviceContext.DomainShader.SetShaderResource(0, heightMap);
                    device.DeviceContext.PixelShader.SetShaderResource(0, diffuseMap);
                    device.DeviceContext.PixelShader.SetShaderResource(1, normalMap);

                    //apply shader
                    shader.Apply();

                    //draw mesh
                    mesh.DrawPatch(SharpDX.Direct3D.PrimitiveTopology.PatchListWith16ControlPoints);

                    //begin drawing text
                    font.Begin();

                    //draw string
                    fpsCounter.Update();
                    font.DrawString("FPS: " + fpsCounter.FPS, 0, 0, Color.White);

                    font.DrawString("Presso Up,Down,Left,Right,A,Z to move camera", 0, 20, Color.White);
                    font.DrawString("Presso W and S to Switch to Wireframe", 0, 40, Color.White);

                    //flush text to view
                    font.End();
                    //present
                    device.Present();

                });
            }
        }
Пример #7
0
        static void Main(string[] args)
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            //render form
            RenderForm form = new RenderForm();

            form.Text = "Tutorial 4: Primitives";

            //Help to count Frame Per Seconds
            SharpFPS fpsCounter = new SharpFPS();

            using (SharpDevice device = new SharpDevice(form)) {
                //Init Mesh
                Water water = new Water(device);

                //Create Shader From File and Create Input Layout
                SharpShader shader = new SharpShader(device, "../../HLSL.txt",
                                                     new SharpShaderDescription()
                {
                    VertexShaderFunction = "VS", PixelShaderFunction = "PS"
                },
                                                     new InputElement[] {
                    new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                    new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 12, 0)
                });

                //create constant buffer
                Buffer11 buffer      = shader.CreateBuffer <UniformData>();
                Buffer11 arrayBuffer = shader.CreateBuffer <ArrayData>();


                fpsCounter.Reset();

                form.KeyDown += (sender, e) =>
                {
                    switch (e.KeyCode)
                    {
                    case Keys.W:
                        device.SetWireframeRasterState();
                        device.SetDefaultBlendState();
                        break;

                    case Keys.S:
                        device.SetDefaultRasterState();
                        break;

                    case Keys.D1:
                        device.SetDefaultBlendState();
                        break;

                    case Keys.D2:
                        device.SetBlend(BlendOperation.Add, BlendOption.InverseSourceAlpha, BlendOption.SourceAlpha);
                        break;

                    case Keys.D3:
                        device.SetBlend(BlendOperation.Add, BlendOption.SourceAlpha, BlendOption.InverseSourceAlpha);
                        break;

                    case Keys.D4:
                        device.SetBlend(BlendOperation.Add, BlendOption.SourceColor, BlendOption.InverseSourceColor);
                        break;

                    case Keys.D5:
                        device.SetBlend(BlendOperation.Add, BlendOption.SourceColor, BlendOption.DestinationColor);
                        break;
                    }
                };

                //main loop
                RenderLoop.Run(form, () => {
                    //Resizing
                    if (device.MustResize)
                    {
                        device.Resize();
                    }

                    //apply states
                    device.UpdateAllStates();

                    //clear color
                    device.Clear(Color.CornflowerBlue);

                    //apply shader
                    shader.Apply();

                    //Set matrices
                    float ratio       = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1, 1000);
                    Matrix view       = Matrix.LookAtLH(new Vector3(0, 12, -12), new Vector3(), Vector3.UnitY);
                    Matrix world      = Matrix.RotationY(Environment.TickCount / 1000.0F * 0.1f);
                    world             = Matrix.Identity;


                    float time          = Environment.TickCount / 1000.0F;
                    Vector4[] waveStats = new Vector4[count] {
                        new Vector4(water.wave.speed * 1.0f, water.wave.wavelength * 1.0f, water.wave.amplitude * 1.0f, 0),
                        new Vector4(water.wave.speed * 1.0f, water.wave.wavelength * 1.0f, water.wave.amplitude * 1.0f, 0),
                        new Vector4(water.wave.speed * 1.0f, water.wave.wavelength * 1.0f, water.wave.amplitude * 1.0f, 0),
                    };

                    Vector4 tmp = new Vector4(2.0f, 0.0f, -2.0f, 0.0f);
                    tmp.Normalize();

                    Vector4[] waveDir = new Vector4[count] {
                        water.wave.waveDir,
                        new Vector4(water.wave.waveDir.Z, 0, -water.wave.waveDir.X, 0),
                        tmp,
                    };

                    var array = new Vector4[count] {
                        new Vector4(1.0f, 0.0f, 0.0f, 0.0f),
                        new Vector4(1.0f, 1.0f, 1.0f, 0.0f),
                        new Vector4(1.0f, 1.0f, 1.0f, 0.0f),
                    };

                    water.wave.waveDir = new Vector4((float)Math.Sin(time / 5.0), 0, (float)Math.Cos(time / 5.0), 0);

                    UniformData sceneInfo = new UniformData()
                    {
                        worldViewProj = world * view * projection,
                        worldView     = world * view,
                        time          = time,
                    };

                    ArrayData arrayInfo = new ArrayData()
                    {
                        array     = array,
                        waveDir   = waveDir,
                        waveStats = waveStats,
                    };


                    using (DataStream ds = new DataStream(Utilities.SizeOf <ArrayData>(), true, true)) {
                        //device.UpdateDataWithDataStream(arrayBuffer,ds);
                        ds.WriteRange(getBytes(arrayInfo));
                        ds.Position = 0;
                        arrayBuffer.Dispose();
                        arrayBuffer = shader.CreateBuffer <ArrayData>(ds);
                        //device.UnmapDataStream(arrayBuffer);
                    }

                    //update constant buffer
                    device.UpdateData <UniformData>(buffer, sceneInfo);

                    //pass constant buffer to shader
                    device.DeviceContext.VertexShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.PixelShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.VertexShader.SetConstantBuffer(1, arrayBuffer);


                    //draw mesh
                    water.Draw();

                    //begin drawing text
                    device.Font.Begin();

                    //draw string
                    fpsCounter.Update();
                    device.Font.DrawString("FPS: " + fpsCounter.FPS, 0, 0);

                    //flush text to view
                    device.Font.End();
                    //present
                    device.Present();
                });

                //release resources
                water.Dispose();
                buffer.Dispose();
            }
        }