示例#1
0
        Option <ShaderResourceView> TryGetResource(SharpDevice device, string path)
        {
            string filename = DirPath + Path.DirectorySeparatorChar + path;

            if (File.Exists(filename))
            {
                return(Option.Return(device.LoadTextureFromFile(filename)));
            }
            else
            {
                return(Option.Return <ShaderResourceView>( ));
            }
        }
示例#2
0
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            //Init textured cube
            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))
            };

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

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

                //Init shader from file
                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)
                    });

                //Create constant buffer
                Buffer11 buffer = shader.CreateBuffer<Matrix>();
                //Create texture from file
                ShaderResourceView texture = device.LoadTextureFromFile("../../texture.bmp");

                fpsCounter.Reset();

                //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();

                    //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;
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1, 1000);
                    Matrix view = Matrix.LookAtLH(new Vector3(0, 10, -30), new Vector3(), Vector3.UnitY);
                    Matrix world = Matrix.RotationY(Environment.TickCount / 1000.0F);
                    Matrix WVP = world * view * projection;
                    device.UpdateData<Matrix>(buffer, WVP);

                    //draw mesh
                    mesh.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 resource
                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()
        {
            //SharpDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.GenericMouse, DeviceFlags.None);
            //SharpDX.RawInput.Device.MouseInput += (sender, args) => textBox.Invoke(new UpdateTextCallback(UpdateMouseText), args);

            SharpDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.GenericKeyboard, DeviceFlags.None);
            SharpDX.RawInput.Device.KeyboardInput += (sender, args) => UpdateKeyboardText(args);

            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            //Init textured cube
            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))
            };



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

            form.Text = "SGE Demo";
            SharpFPS fpsCounter = new SharpFPS();


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

                //Init shader from file
                //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)
                //    });

                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("NORMAL", 0, Format.R32G32B32_Float, 12, 0),
                    new InputElement("TEXCOORD", 0, Format.R32G32_Float, 24, 0)
                });

                //Create constant buffer
                Buffer11 buffer = shader.CreateBuffer <Data>();
                //Create texture from file
                ShaderResourceView texture = device.LoadTextureFromFile("../../texture2.bmp");

                fpsCounter.Reset();


                var gm = new GameManager();
                gm.gameObjects.Add(new GameObject()
                {
                    mesh = SharpMesh.CreateFromObj(device, "../../Models/sword/sword.obj")
                });

                gm.gameObjects[0].update = (delta)
                                           =>
                {
                    gm.gameObjects[0].position  = Vector3.Up * (-8 + (float)Math.Sin((float)Environment.TickCount / 500));
                    gm.gameObjects[0].rotation *= Quaternion.RotationYawPitchRoll(0.0002f, 0, 0);
                    return(0);
                };
                gm.gameObjects[0].scale *= 4;

                gm.gameObjects.Add(new GameObject()
                {
                    mesh = SharpMesh.CreateFromObj(device, "../../Models/Sphere/sphere2.obj")
                });

                gm.gameObjects[1].update = (delta)
                                           =>
                {
                    gm.gameObjects[1].position += Vector3.Up * -0.0001f;
                    gm.gameObjects[1].rotation *= Quaternion.RotationYawPitchRoll(-0.0001f, 0, 0);
                    return(0);
                };
                gm.gameObjects[1].scale *= 1;


                //var go = new GameObject();

                //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();

                    //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;
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 0.1F, 1000.0F);
                    Matrix view       = Matrix.LookAtLH(new Vector3(0, 10, -30), new Vector3(), Vector3.UnitY);
                    Matrix world      = Matrix.RotationY(Environment.TickCount / 1000.0F);
                    Matrix WVP        = world * view * projection;
                    //device.UpdateData<Matrix>(buffer, WVP);

                    //draw mesh
                    //mesh.Draw();

                    //light direction
                    Vector3 lightDirection = new Vector3(0.5f, 0, -1);
                    lightDirection.Normalize();


                    Data sceneInformation = new Data()
                    {
                        world = world,
                        worldViewProjection = world * view * projection,
                        lightDirection      = new Vector4(lightDirection, 1)
                    };

                    //write data inside constant buffer
                    device.UpdateData <Data>(buffer, sceneInformation);

                    //apply constant buffer to shader



                    //world = Matrix.Transformation(Vector3.Zero, Quaternion.Identity, new Vector3(1.2f, 1.2f, 1.2f), Vector3.Zero, Quaternion.Identity, new Vector3(0, -5, 0));
                    //WVP = world * view * projection;

                    for (int i = 0; i < gm.gameObjects.Count; i++)
                    {
                        gm.gameObjects[i].update(1.0f / fpsCounter.FPS);
                        WVP = gm.gameObjects[i].transform * view * projection;
                        //device.UpdateData<Matrix>(buffer, WVP);

                        sceneInformation.world = gm.gameObjects[i].transform;
                        WVP = sceneInformation.world * view * projection;
                        sceneInformation.worldViewProjection = WVP;
                        device.UpdateData <Data>(buffer, sceneInformation);

                        device.DeviceContext.VertexShader.SetConstantBuffer(0, buffer);
                        device.DeviceContext.PixelShader.SetConstantBuffer(0, buffer);

                        //device.DeviceContext.PixelShader.SetShaderResource(0, gm.gameObjects[i].mesh.SubSets[i].DiffuseMap);
                        //gm.gameObjects[i].mesh.Draw();
                        gm.gameObjects[i].mesh.Begin();
                        for (int j = 0; j < gm.gameObjects[i].mesh.SubSets.Count; j++)
                        {
                            device.DeviceContext.PixelShader.SetShaderResource(0, gm.gameObjects[i].mesh.SubSets[j].DiffuseMap);
                            gm.gameObjects[i].mesh.Draw(j);
                        }
                    }

                    //go.position = go.position + Vector3.Up * 0.001f;

                    //go.rotation = go.rotation * Quaternion.RotationYawPitchRoll(0.0001f, 0, 0);

                    //WVP = go.transform * view * projection;

                    //device.UpdateData<Matrix>(buffer, WVP);

                    //mesh.Draw();



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

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

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

                //release resource
                //mesh.Dispose();
                foreach (GameObject go in gm.gameObjects)
                {
                    go.mesh.Dispose();
                }

                buffer.Dispose();
                texture.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;
            }

            //Init textured cube
            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))
            };

            //render form
            RenderForm form = new RenderForm();
            form.Text = "Tutorial 14: Output Stream";
            SharpFPS fpsCounter = new SharpFPS();

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

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

                //Init shader from file
                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)
                    });

                //output stream element declaration
                StreamOutputElement[] soDeclaration = new StreamOutputElement[]
                {
                    new StreamOutputElement(){SemanticName="POSITION",ComponentCount=3},
                    new StreamOutputElement(){SemanticName="TEXCOORD",ComponentCount=2}
                };
                const int streamOutputVertexSize = 20; //bytes (3 floats for position, 2 floats for texcoord)

                //output shader
                SharpShader shaderOutput = new SharpShader(device, "../../HLSL.txt",
                    new SharpShaderDescription() { VertexShaderFunction = "VS_O", GeometryShaderFunction = "GS_O", GeometrySO = soDeclaration },
                    new InputElement[] {
                        new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                        new InputElement("TEXCOORD", 0, Format.R32G32_Float, 12, 0)
                    });

                //declare 2 output buffer to switch between them
                //one will contain the source and the other will be the target of the rendeinrg
                SharpOutputBuffer outputBufferA = new SharpOutputBuffer(device, 100000);
                SharpOutputBuffer outputBufferB = new SharpOutputBuffer(device, 100000);

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

                //Create texture from file
                ShaderResourceView texture = device.LoadTextureFromFile("../../texture.bmp");

                fpsCounter.Reset();

                //for updating
                bool update = true;
                Vector3 nextPosition = new Vector3();

                form.KeyUp += (sender, e) =>
                {
                    switch (e.KeyCode)
                    {
                        case Keys.Up:
                            update = true;
                            nextPosition += new Vector3(0, 10, 0);
                            break;
                        case Keys.Down:
                            update = true;
                            nextPosition += new Vector3(0, -10, 0);
                            break;
                        case Keys.A:
                            update = true;
                            nextPosition += new Vector3(-10, 0, 0);
                            break;
                        case Keys.D:
                            update = true;
                            nextPosition += new Vector3(10, 0, 0);
                            break;
                        case Keys.W:
                            update = true;
                            nextPosition += new Vector3(0, 0, 10);
                            break;
                        case Keys.S:
                            update = true;
                            nextPosition += new Vector3(0, 0, -10);
                            break;
                    }
                };

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

                    //apply states
                    device.UpdateAllStates();

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

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

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

                    //update output buffer only when needed
                    if (update)
                    {
                        //switch buffer
                        var t = outputBufferA;
                        outputBufferA = outputBufferB;
                        outputBufferB = t;

                        //apply shader
                        shaderOutput.Apply();

                        //start drawing on output buffer
                        outputBufferA.Begin();

                        //draw the other output buffer as source
                        device.UpdateData<Matrix>(buffer, Matrix.Identity);
                        outputBufferB.Draw(streamOutputVertexSize);

                        //draw the mesh to add it to buffer
                        device.UpdateData<Matrix>(buffer, Matrix.Translation(nextPosition));
                        //draw mesh
                        mesh.Draw();

                        //end rendering on buffer
                        outputBufferA.End();

                        //stop updating
                        update = false;
                    }

                    //set transformation matrix
                    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, 50, -100), new Vector3(), Vector3.UnitY);
                    Matrix world = Matrix.Identity;
                    Matrix WVP = world * view * projection;
                    device.UpdateData<Matrix>(buffer, WVP);

                    //stream
                    shader.Apply();

                    //draw all buffer every frame
                    outputBufferA.Draw(streamOutputVertexSize);

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

                    //draw string
                    fpsCounter.Update();
                    device.Font.DrawString("FPS: " + fpsCounter.FPS, 0, 0);
                    device.Font.DrawString("Press WASD, Up, Down to move cube", 0, 30);

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

                //release resource
                mesh.Dispose();
                buffer.Dispose();
                texture.Dispose();
            }
        }
示例#7
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 15: Toon Shading With Multiple Render Target";
            //frame rate counter
            SharpFPS fpsCounter = new SharpFPS();


            using (SharpDevice device = new SharpDevice(form))
            {
                //load model from wavefront obj file
                SharpMesh dogMesh = SharpMesh.CreateFromObj(device, "../../../Models/dog/dog.obj");

                //quad
                SharpMesh quad = SharpMesh.CreateQuad(device);

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

                //second pass
                SharpShader secondPass = new SharpShader(device, "../../HLSL.txt",
                                                         new SharpShaderDescription()
                {
                    VertexShaderFunction = "VS_QUAD", PixelShaderFunction = "PS_QUAD"
                },
                                                         new InputElement[] {
                    new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                });

                //render target
                SharpRenderTarget target = new SharpRenderTarget(device, form.ClientSize.Width, form.ClientSize.Height, Format.R8G8B8A8_UNorm);
                //render target
                SharpRenderTarget target2 = new SharpRenderTarget(device, form.ClientSize.Width, form.ClientSize.Height, Format.R8G8B8A8_UNorm);


                //toon texture
                ShaderResourceView bandTexture = device.LoadTextureFromFile("../../../Models/band.bmp");

                //init constant buffer
                Buffer11 toonConstantBuffer = firstPass.CreateBuffer <ToonData>();

                //init frame counter
                fpsCounter.Reset();


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

                        //render target
                        target.Dispose();
                        target = new SharpRenderTarget(device, form.ClientSize.Width, form.ClientSize.Height, Format.R8G8B8A8_UNorm);


                        //render target
                        target2.Dispose();
                        target2 = new SharpRenderTarget(device, form.ClientSize.Width, form.ClientSize.Height, Format.R8G8B8A8_UNorm);
                    }

                    //apply states
                    device.UpdateAllStates();


                    //BEGIN RENDERING TO TEXTURE (MULTIPLE)

                    device.ApplyMultipleRenderTarget(target, target2);


                    target.Clear(Color.CornflowerBlue);
                    target2.Clear(Color.Black);


                    //set transformation matrix
                    float ratio       = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1F, 1000.0F);

                    Vector3 from = new Vector3(0, 70, -150);
                    Vector3 to   = new Vector3(0, 50, 0);

                    Matrix view  = Matrix.LookAtLH(from, to, Vector3.UnitY);
                    Matrix world = Matrix.RotationY(Environment.TickCount / 2000.0F);

                    //light direction
                    Vector3 lightDirection = new Vector3(0.5f, 0, -1);
                    lightDirection.Normalize();


                    ToonData sceneInformation = new ToonData()
                    {
                        world = world,
                        worldViewProjection = world * view * projection,
                        lightDirection      = new Vector4(lightDirection, 1),
                        cameraPosition      = new Vector4(from, 1)
                    };


                    //apply shader
                    firstPass.Apply();

                    //set toon band texture
                    device.DeviceContext.PixelShader.SetShaderResource(1, bandTexture);

                    //write data inside constant buffer
                    device.UpdateData <ToonData>(toonConstantBuffer, sceneInformation);

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

                    //draw mesh
                    dogMesh.Begin();
                    for (int i = 0; i < dogMesh.SubSets.Count; i++)
                    {
                        device.DeviceContext.PixelShader.SetShaderResource(0, dogMesh.SubSets[i].DiffuseMap);
                        dogMesh.Draw(i);
                    }



                    //RENDERING TO DEVICE

                    //Set original targets
                    device.SetDefaultTargers();

                    //apply shader
                    secondPass.Apply();


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

                    secondPass.Apply();

                    //set target
                    device.DeviceContext.PixelShader.SetShaderResource(0, target.Resource);
                    device.DeviceContext.PixelShader.SetShaderResource(1, target2.Resource);

                    quad.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 resource
                dogMesh.Dispose();
                quad.Dispose();
                toonConstantBuffer.Dispose();
                firstPass.Dispose();
                secondPass.Dispose();
                bandTexture.Dispose();
                target.Dispose();
                target2.Dispose();
            }
        }
示例#8
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();

                });
            }
        }
示例#9
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 18: Skin Animation";
            SharpFPS fpsCounter = new SharpFPS();

            //number of cube
            int count = 1000;

            using (SharpDevice device = new SharpDevice(form))
            {
                //Input layout for Skinning Mesh
                InputElement[] description = new InputElement[]
                {
                    new InputElement("POSITION", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0, 0),
                    new InputElement("NORMAL", 0, SharpDX.DXGI.Format.R32G32B32_Float, 12, 0),
                    new InputElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float, 24, 0),
                    new InputElement("BINORMAL", 0, SharpDX.DXGI.Format.R32G32B32_Float, 32, 0),
                    new InputElement("TANGENT", 0, SharpDX.DXGI.Format.R32G32B32_Float, 44, 0),
                    new InputElement("JOINT", 0, SharpDX.DXGI.Format.R32G32B32A32_Float, 56, 0),
                    new InputElement("WEIGHT", 0, SharpDX.DXGI.Format.R32G32B32A32_Float, 72, 0),
                };

                SharpShader staticShader = new SharpShader(device, "../../Basic.hlsl",
                                                           new SharpShaderDescription()
                {
                    VertexShaderFunction = "VSMain",
                    PixelShaderFunction  = "PSMain"
                }, description);

                SharpShader skinShader = new SharpShader(device, "../../BasicSkin.hlsl",
                                                         new SharpShaderDescription()
                {
                    VertexShaderFunction = "VSMain",
                    PixelShaderFunction  = "PSMain"
                }, description);


                Buffer11 lightBuffer = skinShader.CreateBuffer <Vector4>();

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

                SharpModel model = new SharpModel(device,
                                                  ColladaImporter.Import(path + "troll.dae"));

                foreach (Geometry g in model.Geometries)
                {
                    if (g.IsAnimated)
                    {
                        g.Shader = skinShader;
                    }
                    else
                    {
                        g.Shader = staticShader;
                    }

                    if (!string.IsNullOrEmpty(g.Material.DiffuseTextureName))
                    {
                        g.Material.DiffuseTexture = device.LoadTextureFromFile(path + g.Material.DiffuseTextureName);

                        g.Material.NormalTextureName = Path.GetFileNameWithoutExtension(g.Material.DiffuseTextureName) + "N.dds";

                        g.Material.NormalTexture = device.LoadTextureFromFile(path + g.Material.NormalTextureName);
                    }
                }

                fpsCounter.Reset();

                form.KeyDown += (sender, e) =>
                {
                    switch (e.KeyCode)
                    {
                    case Keys.Up:
                        if (count < 1000)
                        {
                            count++;
                        }
                        break;

                    case Keys.Down:
                        if (count > 0)
                        {
                            count--;
                        }
                        break;
                    }
                };

                int lastTick = Environment.TickCount;

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


                    //apply state
                    device.UpdateAllStates();

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



                    //set transformation matrix
                    float ratio       = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1, 10000);
                    Matrix view       = Matrix.LookAtLH(new Vector3(0, -100, 50), new Vector3(0, 0, 50), Vector3.UnitZ);
                    Matrix world      = Matrix.Identity;

                    float angle   = Environment.TickCount / 2000.0F;
                    Vector3 light = new Vector3((float)Math.Sin(angle), (float)Math.Cos(angle), 0);
                    light.Normalize();
                    device.UpdateData <Vector4>(lightBuffer, new Vector4(light, 1));
                    device.DeviceContext.VertexShader.SetConstantBuffer(2, lightBuffer);



                    float animationTime = (Environment.TickCount - lastTick) / 1000.0F;

                    if (animationTime >= model.Animations.First().Duration)
                    {
                        lastTick      = Environment.TickCount;
                        animationTime = 0;
                    }

                    model.SetTime(animationTime);

                    model.Draw(device, new SkinShaderInformation()
                    {
                        Trasform = world * view * projection,
                        World    = world
                    });

                    device.Font.Begin();

                    //draw string
                    fpsCounter.Update();
                    device.Font.DrawString("FPS: " + fpsCounter.FPS, 0, 0);
                    device.Font.DrawString("Skinning Animation With Collada", 0, 30);

                    //flush text to view
                    device.Font.End();
                    //present
                    device.Present();
                });
            }
        }
示例#10
0
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            int[]          indices  = Enumerable.Range(0, 1000).ToArray();
            List <Vector3> vertices = new List <Vector3>();

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    for (int z = 0; z < 10; z++)
                    {
                        vertices.Add(new Vector3(i - 5, j - 5, 5 - z) * 12);
                    }
                }
            }


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

            form.Text = "Tutorial 8: Geometry Shader";
            SharpFPS fpsCounter = new SharpFPS();

            //number of cube
            int count = 1000;

            using (SharpDevice device = new SharpDevice(form))
            {
                SharpMesh   mesh   = SharpMesh.Create <Vector3>(device, vertices.ToArray(), indices);
                SharpShader shader = new SharpShader(device, "../../HLSL.txt",
                                                     new SharpShaderDescription()
                {
                    VertexShaderFunction = "VS", PixelShaderFunction = "PS", GeometryShaderFunction = "GS"
                },
                                                     new InputElement[] {
                    new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0)
                });

                Buffer11           buffer  = shader.CreateBuffer <Data>();
                ShaderResourceView texture = device.LoadTextureFromFile("../../texture.dds");

                fpsCounter.Reset();

                form.KeyDown += (sender, e) =>
                {
                    switch (e.KeyCode)
                    {
                    case Keys.Up:
                        if (count < 1000)
                        {
                            count++;
                        }
                        break;

                    case Keys.Down:
                        if (count > 0)
                        {
                            count--;
                        }
                        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.GeometryShader.SetConstantBuffer(0, buffer);

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

                    //set transformation matrix
                    float ratio       = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1, 10000);
                    Matrix view       = Matrix.LookAtLH(new Vector3(10, 20, -100), new Vector3(), Vector3.UnitY);
                    Matrix world      = Matrix.RotationY(Environment.TickCount / 1000.0F);

                    Data matrices = new Data()
                    {
                        World = world, ViewProj = view * projection
                    };
                    device.UpdateData <Data>(buffer, matrices);

                    //draw mesh
                    mesh.DrawPoints(count);


                    //begin drawing text
                    device.DeviceContext.GeometryShader.Set(null);
                    device.Font.Begin();

                    //draw string
                    fpsCounter.Update();
                    device.Font.DrawString("FPS: " + fpsCounter.FPS, 0, 0);
                    device.Font.DrawString("Cube Count: " + count, 0, 30);
                    device.Font.DrawString("Press Up and Down to change number", 0, 60);

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

                //release resource
                mesh.Dispose();
                buffer.Dispose();
            }
        }
示例#11
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();
            }
        }
示例#12
0
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            int[] indices = Enumerable.Range(0, 1000).ToArray();
            List<Vector3> vertices = new List<Vector3>();

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    for (int z = 0; z < 10; z++)
                    {
                        vertices.Add(new Vector3(i - 5, j - 5, 5 - z) * 12);
                    }
                }
            }

            //render form
            RenderForm form = new RenderForm();
            form.Text = "Tutorial 8: Geometry Shader";
            SharpFPS fpsCounter = new SharpFPS();

            //number of cube
            int count = 1000;

            using (SharpDevice device = new SharpDevice(form))
            {
                SharpMesh mesh = SharpMesh.Create<Vector3>(device, vertices.ToArray(), indices);
                SharpShader shader = new SharpShader(device, "../../HLSL.txt",
                    new SharpShaderDescription() { VertexShaderFunction = "VS", PixelShaderFunction = "PS", GeometryShaderFunction = "GS" },
                    new InputElement[] {
                        new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0)
                    });

                Buffer11 buffer = shader.CreateBuffer<Data>();
                ShaderResourceView texture = device.LoadTextureFromFile("../../texture.dds");

                fpsCounter.Reset();

                form.KeyDown += (sender, e) =>
                {
                    switch (e.KeyCode)
                    {
                        case Keys.Up:
                            if (count < 1000)
                                count++;
                            break;
                        case Keys.Down:
                            if (count > 0)
                                count--;
                            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.GeometryShader.SetConstantBuffer(0, buffer);

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

                    //set transformation matrix
                    float ratio = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1, 10000);
                    Matrix view = Matrix.LookAtLH(new Vector3(10, 20, -100), new Vector3(), Vector3.UnitY);
                    Matrix world = Matrix.RotationY(Environment.TickCount / 1000.0F);

                    Data matrices = new Data() { World = world, ViewProj = view * projection };
                    device.UpdateData<Data>(buffer, matrices);

                    //draw mesh
                    mesh.DrawPoints(count);

                    //begin drawing text
                    device.DeviceContext.GeometryShader.Set(null);
                    device.Font.Begin();

                    //draw string
                    fpsCounter.Update();
                    device.Font.DrawString("FPS: " + fpsCounter.FPS, 0, 0);
                    device.Font.DrawString("Cube Count: " + count, 0, 30);
                    device.Font.DrawString("Press Up and Down to change number", 0, 60);

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

                });

                //release resource
                mesh.Dispose();
                buffer.Dispose();
            }
        }
示例#13
0
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            //Init textured cube
            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))
            };



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

            form.Text = "Tutorial 5: Texture";
            SharpFPS fpsCounter = new SharpFPS();


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

                //Init shader from file
                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)
                });

                //Create constant buffer
                Buffer11 buffer = shader.CreateBuffer <Matrix>();
                //Create texture from file
                ShaderResourceView texture = device.LoadTextureFromFile("../../texture.bmp");

                fpsCounter.Reset();

                //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();

                    //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;
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1, 1000);
                    Matrix view       = Matrix.LookAtLH(new Vector3(0, 10, -30), new Vector3(), Vector3.UnitY);
                    Matrix world      = Matrix.RotationY(Environment.TickCount / 1000.0F);
                    Matrix WVP        = world * view * projection;
                    device.UpdateData <Matrix>(buffer, WVP);

                    //draw mesh
                    mesh.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 resource
                mesh.Dispose();
                buffer.Dispose();
                texture.Dispose();
            }
        }
示例#14
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 15: Toon Shading With Multiple Render Target";
            //frame rate counter
            SharpFPS fpsCounter = new SharpFPS();

            using (SharpDevice device = new SharpDevice(form))
            {
                //load model from wavefront obj file
                SharpMesh dogMesh = SharpMesh.CreateFromObj(device, "../../../Models/dog/dog.obj");

                //quad
                SharpMesh quad = SharpMesh.CreateQuad(device);

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

                //second pass
                SharpShader secondPass = new SharpShader(device, "../../HLSL.txt",
                    new SharpShaderDescription() { VertexShaderFunction = "VS_QUAD", PixelShaderFunction = "PS_QUAD" },
                    new InputElement[] {
                        new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                    });

                //render target
                SharpRenderTarget target = new SharpRenderTarget(device, form.ClientSize.Width, form.ClientSize.Height, Format.R8G8B8A8_UNorm);
                //render target
                SharpRenderTarget target2 = new SharpRenderTarget(device, form.ClientSize.Width, form.ClientSize.Height, Format.R8G8B8A8_UNorm);

                //toon texture
                ShaderResourceView bandTexture = device.LoadTextureFromFile("../../../Models/band.bmp");

                //init constant buffer
                Buffer11 toonConstantBuffer = firstPass.CreateBuffer<ToonData>();

                //init frame counter
                fpsCounter.Reset();

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

                        //render target
                        target.Dispose();
                        target = new SharpRenderTarget(device, form.ClientSize.Width, form.ClientSize.Height, Format.R8G8B8A8_UNorm);

                        //render target
                        target2.Dispose();
                        target2 = new SharpRenderTarget(device, form.ClientSize.Width, form.ClientSize.Height, Format.R8G8B8A8_UNorm);

                    }

                    //apply states
                    device.UpdateAllStates();

                    //BEGIN RENDERING TO TEXTURE (MULTIPLE)

                    device.ApplyMultipleRenderTarget(target, target2);

                    target.Clear(Color.CornflowerBlue);
                    target2.Clear(Color.Black);

                    //set transformation matrix
                    float ratio = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1F, 1000.0F);

                    Vector3 from = new Vector3(0, 70, -150);
                    Vector3 to = new Vector3(0, 50, 0);

                    Matrix view = Matrix.LookAtLH(from, to, Vector3.UnitY);
                    Matrix world = Matrix.RotationY(Environment.TickCount / 2000.0F);

                    //light direction
                    Vector3 lightDirection = new Vector3(0.5f, 0, -1);
                    lightDirection.Normalize();

                    ToonData sceneInformation = new ToonData()
                    {
                        world = world,
                        worldViewProjection = world * view * projection,
                        lightDirection = new Vector4(lightDirection, 1),
                        cameraPosition = new Vector4(from, 1)
                    };

                    //apply shader
                    firstPass.Apply();

                    //set toon band texture
                    device.DeviceContext.PixelShader.SetShaderResource(1, bandTexture);

                    //write data inside constant buffer
                    device.UpdateData<ToonData>(toonConstantBuffer, sceneInformation);

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

                    //draw mesh
                    dogMesh.Begin();
                    for (int i = 0; i < dogMesh.SubSets.Count; i++)
                    {
                        device.DeviceContext.PixelShader.SetShaderResource(0, dogMesh.SubSets[i].DiffuseMap);
                        dogMesh.Draw(i);
                    }

                    //RENDERING TO DEVICE

                    //Set original targets
                    device.SetDefaultTargers();

                    //apply shader
                    secondPass.Apply();

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

                    secondPass.Apply();

                    //set target
                    device.DeviceContext.PixelShader.SetShaderResource(0, target.Resource);
                    device.DeviceContext.PixelShader.SetShaderResource(1, target2.Resource);

                    quad.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 resource
                dogMesh.Dispose();
                quad.Dispose();
                toonConstantBuffer.Dispose();
                firstPass.Dispose();
                secondPass.Dispose();
                bandTexture.Dispose();
                target.Dispose();
                target2.Dispose();

            }
        }
示例#15
0
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

            //Init textured cube
            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))
            };



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

            form.Text = "Tutorial 14: Output Stream";
            SharpFPS fpsCounter = new SharpFPS();


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

                //Init shader from file
                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)
                });

                //output stream element declaration
                StreamOutputElement[] soDeclaration = new StreamOutputElement[]
                {
                    new StreamOutputElement()
                    {
                        SemanticName = "POSITION", ComponentCount = 3
                    },
                    new StreamOutputElement()
                    {
                        SemanticName = "TEXCOORD", ComponentCount = 2
                    }
                };
                const int streamOutputVertexSize = 20; //bytes (3 floats for position, 2 floats for texcoord)


                //output shader
                SharpShader shaderOutput = new SharpShader(device, "../../HLSL.txt",
                                                           new SharpShaderDescription()
                {
                    VertexShaderFunction = "VS_O", GeometryShaderFunction = "GS_O", GeometrySO = soDeclaration
                },
                                                           new InputElement[] {
                    new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
                    new InputElement("TEXCOORD", 0, Format.R32G32_Float, 12, 0)
                });

                //declare 2 output buffer to switch between them
                //one will contain the source and the other will be the target of the rendeinrg
                SharpOutputBuffer outputBufferA = new SharpOutputBuffer(device, 100000);
                SharpOutputBuffer outputBufferB = new SharpOutputBuffer(device, 100000);


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

                //Create texture from file
                ShaderResourceView texture = device.LoadTextureFromFile("../../texture.bmp");


                fpsCounter.Reset();

                //for updating
                bool    update       = true;
                Vector3 nextPosition = new Vector3();


                form.KeyUp += (sender, e) =>
                {
                    switch (e.KeyCode)
                    {
                    case Keys.Up:
                        update        = true;
                        nextPosition += new Vector3(0, 10, 0);
                        break;

                    case Keys.Down:
                        update        = true;
                        nextPosition += new Vector3(0, -10, 0);
                        break;

                    case Keys.A:
                        update        = true;
                        nextPosition += new Vector3(-10, 0, 0);
                        break;

                    case Keys.D:
                        update        = true;
                        nextPosition += new Vector3(10, 0, 0);
                        break;

                    case Keys.W:
                        update        = true;
                        nextPosition += new Vector3(0, 0, 10);
                        break;

                    case Keys.S:
                        update        = true;
                        nextPosition += new Vector3(0, 0, -10);
                        break;
                    }
                };

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

                    //apply states
                    device.UpdateAllStates();

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



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

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

                    //update output buffer only when needed
                    if (update)
                    {
                        //switch buffer
                        var t         = outputBufferA;
                        outputBufferA = outputBufferB;
                        outputBufferB = t;

                        //apply shader
                        shaderOutput.Apply();

                        //start drawing on output buffer
                        outputBufferA.Begin();

                        //draw the other output buffer as source
                        device.UpdateData <Matrix>(buffer, Matrix.Identity);
                        outputBufferB.Draw(streamOutputVertexSize);


                        //draw the mesh to add it to buffer
                        device.UpdateData <Matrix>(buffer, Matrix.Translation(nextPosition));
                        //draw mesh
                        mesh.Draw();

                        //end rendering on buffer
                        outputBufferA.End();

                        //stop updating
                        update = false;
                    }



                    //set transformation matrix
                    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, 50, -100), new Vector3(), Vector3.UnitY);
                    Matrix world      = Matrix.Identity;
                    Matrix WVP        = world * view * projection;
                    device.UpdateData <Matrix>(buffer, WVP);

                    //stream
                    shader.Apply();

                    //draw all buffer every frame
                    outputBufferA.Draw(streamOutputVertexSize);

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

                    //draw string
                    fpsCounter.Update();
                    device.Font.DrawString("FPS: " + fpsCounter.FPS, 0, 0);
                    device.Font.DrawString("Press WASD, Up, Down to move cube", 0, 30);

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

                //release resource
                mesh.Dispose();
                buffer.Dispose();
                texture.Dispose();
            }
        }
示例#16
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 18: Skin Animation";
            SharpFPS fpsCounter = new SharpFPS();

            //number of cube
            int count = 1000;

            using (SharpDevice device = new SharpDevice(form))
            {
                //Input layout for Skinning Mesh
                InputElement[] description = new InputElement[]
                {
                    new InputElement("POSITION",0,SharpDX.DXGI.Format.R32G32B32_Float,0,0),
                    new InputElement("NORMAL",0,SharpDX.DXGI.Format.R32G32B32_Float,12,0),
                    new InputElement("TEXCOORD",0,SharpDX.DXGI.Format.R32G32_Float,24,0),
                    new InputElement("BINORMAL",0,SharpDX.DXGI.Format.R32G32B32_Float,32,0),
                    new InputElement("TANGENT",0,SharpDX.DXGI.Format.R32G32B32_Float,44,0),
                    new InputElement("JOINT",0,SharpDX.DXGI.Format.R32G32B32A32_Float,56,0),
                    new InputElement("WEIGHT",0,SharpDX.DXGI.Format.R32G32B32A32_Float,72,0),
                };

                SharpShader staticShader = new SharpShader(device, "../../Basic.hlsl",
                    new SharpShaderDescription()
                    {
                        VertexShaderFunction = "VSMain",
                        PixelShaderFunction = "PSMain"
                    }, description);

                SharpShader skinShader = new SharpShader(device, "../../BasicSkin.hlsl",
                    new SharpShaderDescription()
                    {
                        VertexShaderFunction = "VSMain",
                        PixelShaderFunction = "PSMain"
                    }, description);

                Buffer11 lightBuffer = skinShader.CreateBuffer<Vector4>();

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

                SharpModel model = new SharpModel(device,
                    ColladaImporter.Import(path + "troll.dae"));

                foreach (Geometry g in model.Geometries)
                {
                    if (g.IsAnimated)
                        g.Shader = skinShader;
                    else
                        g.Shader = staticShader;

                    if (!string.IsNullOrEmpty(g.Material.DiffuseTextureName))
                    {
                        g.Material.DiffuseTexture = device.LoadTextureFromFile(path + g.Material.DiffuseTextureName);

                        g.Material.NormalTextureName = Path.GetFileNameWithoutExtension(g.Material.DiffuseTextureName) + "N.dds";

                        g.Material.NormalTexture = device.LoadTextureFromFile(path + g.Material.NormalTextureName);
                    }
                }

                fpsCounter.Reset();

                form.KeyDown += (sender, e) =>
                {
                    switch (e.KeyCode)
                    {
                        case Keys.Up:
                            if (count < 1000)
                                count++;
                            break;
                        case Keys.Down:
                            if (count > 0)
                                count--;
                            break;
                    }
                };

                int lastTick = Environment.TickCount;

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

                    //apply state
                    device.UpdateAllStates();

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

                    //set transformation matrix
                    float ratio = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1, 10000);
                    Matrix view = Matrix.LookAtLH(new Vector3(0, -100, 50), new Vector3(0, 0, 50), Vector3.UnitZ);
                    Matrix world = Matrix.Identity;

                    float angle = Environment.TickCount / 2000.0F;
                    Vector3 light = new Vector3((float)Math.Sin(angle), (float)Math.Cos(angle), 0);
                    light.Normalize();
                    device.UpdateData<Vector4>(lightBuffer, new Vector4(light, 1));
                    device.DeviceContext.VertexShader.SetConstantBuffer(2, lightBuffer);

                    float animationTime = (Environment.TickCount - lastTick) / 1000.0F;

                    if (animationTime >= model.Animations.First().Duration)
                    {
                        lastTick = Environment.TickCount;
                        animationTime = 0;
                    }

                    model.SetTime(animationTime);

                    model.Draw(device, new SkinShaderInformation()
                    {
                        Trasform = world * view * projection,
                        World = world
                    });

                    device.Font.Begin();

                    //draw string
                    fpsCounter.Update();
                    device.Font.DrawString("FPS: " + fpsCounter.FPS, 0, 0);
                    device.Font.DrawString("Skinning Animation With Collada", 0, 30);

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

                });

            }
        }