コード例 #1
0
        public static void RenderCollisionModel(Matrix viewProjection, Vector3 lightDirection, Vector3 lightDirection2)
        {
            if (collisionMesh == null)
            {
                return;
            }

            sceneInformation = new CollisionRenderData()
            {
                viewProjection  = viewProjection,
                ambientColor    = new Vector4(0.1f, 0.1f, 0.3f, 1f),
                lightDirection  = new Vector4(lightDirection, 1f),
                lightDirection2 = new Vector4(lightDirection2, 1f)
            };

            device.SetDefaultBlendState();
            device.SetCullModeDefault();
            device.SetFillModeDefault();
            device.ApplyRasterState();
            device.UpdateAllStates();

            device.UpdateData(collisionBuffer, sceneInformation);
            device.DeviceContext.VertexShader.SetConstantBuffer(0, collisionBuffer);
            collisionShader.Apply();

            collisionMesh.Draw();
        }
コード例 #2
0
        public void RenderCollisionModel(SharpRenderer renderer)
        {
            if (collisionMesh == null)
            {
                return;
            }

            sceneInformation = new CollisionRenderData()
            {
                viewProjection  = renderer.viewProjection,
                ambientColor    = new Vector4(0.1f, 0.1f, 0.3f, 1f),
                lightDirection  = new Vector4(-renderer.Camera.GetForward(), 1f),
                lightDirection2 = new Vector4(renderer.Camera.GetUp(), 1f)
            };

            renderer.Device.SetDefaultBlendState();
            renderer.Device.SetCullModeDefault();
            renderer.Device.SetFillModeDefault();
            renderer.Device.ApplyRasterState();
            renderer.Device.UpdateAllStates();

            renderer.Device.UpdateData(renderer.collisionBuffer, sceneInformation);
            renderer.Device.DeviceContext.VertexShader.SetConstantBuffer(0, renderer.collisionBuffer);
            renderer.collisionShader.Apply();

            collisionMesh.Draw(renderer.Device);
        }
コード例 #3
0
            public void Render()
            {
                if (isSelected)
                {
                    renderData.Color = new Vector4(0.3f, 0.9f, 0.5f, 1f);
                }
                else
                {
                    renderData.Color = new Vector4(0.8f, 0.8f, 0f, 1f);
                }

                renderData.worldViewProjection = SharpRenderer.viewProjection;

                SharpRenderer.device.SetFillModeSolid();
                SharpRenderer.device.SetCullModeNone();
                SharpRenderer.device.SetDefaultBlendState();
                SharpRenderer.device.ApplyRasterState();
                SharpRenderer.device.UpdateAllStates();

                SharpRenderer.device.UpdateData(SharpRenderer.basicBuffer, renderData);
                SharpRenderer.device.DeviceContext.VertexShader.SetConstantBuffer(0, SharpRenderer.basicBuffer);
                SharpRenderer.basicShader.Apply();

                splineMesh.Draw();
            }
コード例 #4
0
        public void RenderQuadTree(SharpRenderer renderer)
        {
            if (quadTreeMesh == null)
            {
                return;
            }

            mainQuadtreeRenderData.worldViewProjection = quadTreeTranslation * renderer.viewProjection;

            renderer.Device.SetFillModeSolid();
            renderer.Device.SetCullModeNone();
            renderer.Device.SetDefaultBlendState();
            renderer.Device.ApplyRasterState();
            renderer.Device.UpdateAllStates();

            renderer.Device.UpdateData(renderer.basicBuffer, mainQuadtreeRenderData);
            renderer.Device.DeviceContext.VertexShader.SetConstantBuffer(0, renderer.basicBuffer);
            renderer.basicShader.Apply();

            quadTreeMesh.Draw(renderer.Device);
        }
コード例 #5
0
        public static void RenderQuadTree2(SharpDevice device, SharpShader shader, SharpDX.Direct3D11.Buffer buffer, Matrix viewProjection)
        {
            if (secondQuadTreeMesh == null)
            {
                return;
            }

            secondQuadtreeRenderData.worldViewProjection = quadTreeTranslation * viewProjection;

            device.SetFillModeSolid();
            device.SetCullModeNone();
            device.SetDefaultBlendState();
            device.ApplyRasterState();
            device.UpdateAllStates();

            device.UpdateData(buffer, secondQuadtreeRenderData);
            device.DeviceContext.VertexShader.SetConstantBuffer(0, buffer);
            shader.Apply();

            secondQuadTreeMesh.Draw();
        }
コード例 #6
0
        public void Render(SharpRenderer renderer)
        {
            if (splineMesh == null)
            {
                return;
            }

            renderData.Color = isSelected ? new Vector4(0.3f, 0.9f, 0.5f, 1f) : new Vector4(0.8f, 0.8f, 0f, 1f);

            renderData.worldViewProjection = renderer.viewProjection;

            renderer.Device.SetFillModeSolid();
            renderer.Device.SetCullModeNone();
            renderer.Device.SetDefaultBlendState();
            renderer.Device.ApplyRasterState();
            renderer.Device.UpdateAllStates();

            renderer.Device.UpdateData(renderer.basicBuffer, renderData);
            renderer.Device.DeviceContext.VertexShader.SetConstantBuffer(0, renderer.basicBuffer);
            renderer.basicShader.Apply();

            splineMesh.Draw(renderer.Device);
        }
コード例 #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 7: Loading Obj File";
            //frame rate counter
            SharpFPS fpsCounter = new SharpFPS();


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

                //load model from wavefront obj file
                SharpMesh mesh = SharpMesh.CreateFromObj(device, "../../../Models/car/car.obj");

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

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

                //init frame counter
                fpsCounter.Reset();

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

                    //apply states
                    device.UpdateAllStates();

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

                    //apply shader
                    shader.Apply();

                    //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);
                    Matrix view = Matrix.LookAtLH(new Vector3(0, 10, -30), new Vector3(), Vector3.UnitY);
                    Matrix world = Matrix.RotationY(Environment.TickCount / 1000.0F);

                    //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
                    device.DeviceContext.VertexShader.SetConstantBuffer(0, buffer);
                    device.DeviceContext.PixelShader.SetConstantBuffer(0, buffer);

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


                    //begin drawing text
                    font.Begin();

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

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

                });

                //release resource
                font.Dispose();
                mesh.Dispose();
                buffer.Dispose();
            }
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: falbertp/SharpDX_Demo
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

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

            form.Text = "Tutorial 13: Geometry Instancing";
            //frame rate counter
            SharpFPS fpsCounter = new SharpFPS();


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

                //load model from wavefront obj file
                SharpMesh mesh = SharpMesh.CreateNormalMappedFromObj(device, "../../../Models/dog/dog.obj");

                //init shader with normal map illumination
                SharpShader shader = new SharpShader(device, "../../HLSL_instancing.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("TANGENT", 0, Format.R32G32B32_Float, 24, 0),
                    new InputElement("BINORMAL", 0, Format.R32G32B32_Float, 36, 0),
                    new InputElement("TEXCOORD", 0, Format.R32G32_Float, 48, 0),
                    new InputElement("INSTANCEPOSITION", 0, Format.R32G32B32_Float, 0, 1, InputClassification.PerInstanceData, 1)
                });

                SharpShader shaderStandard = new SharpShader(device, "../../HLSL_standard.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("TANGENT", 0, Format.R32G32B32_Float, 24, 0),
                    new InputElement("BINORMAL", 0, Format.R32G32B32_Float, 36, 0),
                    new InputElement("TEXCOORD", 0, Format.R32G32_Float, 48, 0)
                });

                //create instance buffer data
                List <Vector3> positions = new List <Vector3>();

                for (int z = 0; z < 100; z++)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        for (int j = 0; j < 10; j++)
                        {
                            positions.Add(new Vector3(i * 80 - 400, j * 80 - 400, z * 80 - 400));
                        }
                    }
                }

                SharpInstanceBuffer <Vector3> instanceBuffer = new SharpInstanceBuffer <Vector3>(device, positions.ToArray());

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



                //init frame counter
                fpsCounter.Reset();

                //to active normal mapping
                bool instancing    = true;
                int  instanceCount = 5000;

                form.KeyDown += (sender, e) =>
                {
                    switch (e.KeyCode)
                    {
                    case Keys.I:
                        instancing = true;
                        break;

                    case Keys.D:
                        instancing = false;
                        break;

                    case Keys.Up:

                        instanceCount += 100;

                        if (instanceCount >= 10000)
                        {
                            instanceCount = 10000;
                        }
                        break;

                    case Keys.Down:

                        instanceCount -= 100;
                        if (instanceCount < 0)
                        {
                            instanceCount = 0;
                        }
                        break;
                    }
                };

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

                    //apply states
                    device.UpdateAllStates();

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



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

                    //set camera position and target
                    Vector3 from = new Vector3(0, 70, -1000);
                    Vector3 to   = new Vector3(0, 50, 0);
                    Matrix view  = Matrix.LookAtLH(from, to, Vector3.UnitY);

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

                    if (instancing)
                    {
                        //Instancing rendering loop

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


                        Data sceneInformation = new Data()
                        {
                            world          = world,
                            viewProjection = view * projection,
                            lightDirection = new Vector4(lightDirection, 1),
                            viewDirection  = new Vector4(Vector3.Normalize(from - to), 1)
                        };


                        //apply shader
                        shader.Apply();

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

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

                        //draw mesh
                        mesh.Begin();
                        for (int i = 0; i < mesh.SubSets.Count; i++)
                        {
                            device.DeviceContext.PixelShader.SetShaderResource(0, mesh.SubSets[i].DiffuseMap);
                            device.DeviceContext.PixelShader.SetShaderResource(1, mesh.SubSets[i].NormalMap);
                            instanceBuffer.DrawInstance(instanceCount, mesh.SubSets[i].IndexCount, mesh.SubSets[i].StartIndex);
                        }
                    }
                    else
                    {
                        //non instancing
                        //apply shader
                        shaderStandard.Apply();

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

                        //prepare mesh
                        mesh.Begin();

                        for (int j = 0; j < instanceCount; j++)
                        {
                            //set world matrix
                            Matrix world = Matrix.RotationY(Environment.TickCount / 2000.0F) * Matrix.Translation(positions[j]);


                            Data sceneInformation = new Data()
                            {
                                world          = world,
                                viewProjection = view * projection,
                                lightDirection = new Vector4(lightDirection, 1),
                                viewDirection  = new Vector4(Vector3.Normalize(from - to), 1)
                            };

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


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

                    //begin drawing text
                    font.Begin();

                    //draw string
                    fpsCounter.Update();
                    font.DrawString("FPS: " + fpsCounter.FPS, 0, 0, Color.White);
                    if (instancing)
                    {
                        font.DrawString("Instancing On: Press D to use standard rendering. See FPS", 0, 30, Color.White);
                    }
                    else
                    {
                        font.DrawString("Instancing Off: Press I to use Instancing. See FPS", 0, 30, Color.White);
                    }

                    font.DrawString("Press up and down to change count ", 0, 60, Color.White);
                    font.DrawString("Count: " + instanceCount, 0, 90, Color.White);

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

                //release resource
                font.Dispose();
                mesh.Dispose();
                buffer.Dispose();
                shader.Dispose();
                shaderStandard.Dispose();
                instanceBuffer.Dispose();
            }
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: PavelBrokhman/SharpDX_Demo
        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 font
                SharpBatch font = new SharpBatch(device, "textfont.dds");

                //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 = ShaderResourceView.FromFile(device.Device, "../../texture.bmp");

                fpsCounter.Reset();

                //main loop
                RenderLoop.Run(form, () =>
                {
                    //Resizing
                    if (device.MustResize)
                    {
                        device.Resize();
                        font.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
                    font.Begin();

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

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

                //release resource
                font.Dispose();
                mesh.Dispose();
                buffer.Dispose();
                texture.Dispose();
            }
        }
コード例 #10
0
ファイル: Program.cs プロジェクト: falbertp/SharpDX_Demo
        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 font
                SharpBatch font = new SharpBatch(device, "textfont.dds");

                //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 = ShaderResourceView.FromFile(device.Device, "../../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();
                        font.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
                    font.Begin();

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

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

                //release resource
                font.Dispose();
                mesh.Dispose();
                buffer.Dispose();
                texture.Dispose();
            }
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: falbertp/SharpDX_Demo
        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 font
                SharpBatch font = new SharpBatch(device, "textfont.dds");

                //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 = ShaderResourceView.FromFile(device.Device, "../../../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);


                        font.Resize();
                    }

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

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

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


                //release resource
                font.Dispose();
                dogMesh.Dispose();
                quad.Dispose();
                toonConstantBuffer.Dispose();
                firstPass.Dispose();
                secondPass.Dispose();
                bandTexture.Dispose();
                target.Dispose();
                target2.Dispose();
            }
        }
コード例 #12
0
 public void Draw()
 {
     mesh.Draw();
 }
コード例 #13
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 17: Query";
            //frame rate counter
            SharpFPS fpsCounter = new SharpFPS();


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

                //load model from wavefront obj file
                SharpMesh earth = SharpMesh.CreateFromObj(device, "../../../Models/planets/earth.obj");
                SharpMesh moon  = SharpMesh.CreateFromObj(device, "../../../Models/planets/moon.obj");

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

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

                Query pipelineQuery = new Query(device.Device, new QueryDescription()
                {
                    Flags = QueryFlags.None, Type = QueryType.PipelineStatistics
                });

                QueryDataPipelineStatistics stats = new QueryDataPipelineStatistics();


                //init frame counter
                fpsCounter.Reset();

                int   lastX        = 0;
                float currentAngle = 200;
                form.MouseMove += (sender, e) =>
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        currentAngle += (lastX - e.X);
                    }
                    lastX = e.X;
                };

                //main loop
                RenderLoop.Run(form, () =>
                {
                    //Resizing
                    if (device.MustResize)
                    {
                        device.Resize();
                        font.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);
                    device.DeviceContext.PixelShader.SetConstantBuffer(0, buffer);

                    //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);
                    Matrix view       = Matrix.LookAtLH(new Vector3(0, 0, -250), new Vector3(), Vector3.UnitY);
                    Matrix world      = Matrix.Translation(0, 0, 200) * Matrix.RotationY(MathUtil.DegreesToRadians(currentAngle));

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

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

                    //begin analizing
                    device.DeviceContext.Begin(pipelineQuery);

                    world            = Matrix.RotationY(Environment.TickCount / 2000.0F);
                    sceneInformation = new Data()
                    {
                        world = world,
                        worldViewProjection = world * view * projection,
                        lightDirection      = new Vector4(lightDirection, 1)
                    };

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

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

                    //get result
                    while (!device.DeviceContext.GetData <QueryDataPipelineStatistics>(pipelineQuery, AsynchronousFlags.None, out stats))
                    {
                    }

                    //begin drawing text
                    font.Begin();

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

                    //print earth stats
                    font.DrawString("Earth Stats : Rotate Moon To Cover Earth ", 0, 30, Color.White);
                    font.DrawString(string.Format("Primitive Count: {0}", stats.IAPrimitiveCount), 0, 60, Color.White);
                    font.DrawString(string.Format("Vertex Count Count: {0}", stats.IAVerticeCount), 0, 90, Color.White);
                    font.DrawString(string.Format("Vertex Shader Execution: {0}", stats.VSInvocationCount), 0, 120, Color.White);
                    font.DrawString(string.Format("Pixel Shader Execution: {0}", stats.PSInvocationCount), 0, 150, Color.White);

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

                //release resource
                font.Dispose();
                earth.Dispose();
                moon.Dispose();
                buffer.Dispose();
                pipelineQuery.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 10: Render Target";
            //frame rate counter
            SharpFPS fpsCounter = new SharpFPS();


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

                //init shader
                SharpShader phongShader = 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("TANGENT", 0, Format.R32G32B32_Float, 24, 0),
                    new InputElement("BINORMAL", 0, Format.R32G32B32_Float, 36, 0),
                    new InputElement("TEXCOORD", 0, Format.R32G32_Float, 48, 0)
                });

                SharpShader renderTargetShader = new SharpShader(device, "../../HLSL_RT.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)
                });

                //render target
                SharpRenderTarget target = new SharpRenderTarget(device, 512, 512, Format.B8G8R8A8_UNorm);

                //init constant buffer
                Buffer11 phongConstantBuffer        = phongShader.CreateBuffer <PhongData>();
                Buffer11 renderTargetConstantBuffer = phongShader.CreateBuffer <RenderTargetData>();

                //init frame counter
                fpsCounter.Reset();

                var capture = false;

                //effect inside shader
                int mode = 0;
                form.KeyDown += (sender, e) =>
                {
                    switch (e.KeyCode)
                    {
                    case Keys.D1:
                        mode = 0;
                        break;

                    case Keys.D2:
                        mode = 1;
                        break;

                    case Keys.D3:
                        mode = 2;
                        break;

                    case Keys.D4:
                        mode = 3;
                        break;

                    case Keys.D5:
                        capture = true;
                        break;
                    }
                };

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

                    //apply states
                    device.UpdateAllStates();


                    //BEGIN RENDERING TO TEXTURE
                    target.Apply();
                    target.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, 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();


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


                    //apply shader
                    phongShader.Apply();

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

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

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


                    //RENDERING TO DEVICE

                    //Set original targets
                    device.SetDefaultTargers();

                    if (capture)
                    {
                        target.ToBitmap().Save("Test.jpg");
                        capture = false;
                    }

                    //apply shader
                    renderTargetShader.Apply();

                    Matrix WVP =
                        Matrix.RotationY(Environment.TickCount / 2000.0F) *
                        Matrix.LookAtLH(new Vector3(7, 10, -13), new Vector3(), Vector3.UnitY) *
                        projection;
                    device.UpdateData <RenderTargetData>(renderTargetConstantBuffer, new RenderTargetData()
                    {
                        worldViewProjection = WVP, data = new Vector4(mode, 0, 0, 0)
                    });


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

                    renderTargetShader.Apply();

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

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

                    cubeMesh.Draw();


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

                    //draw string
                    fpsCounter.Update();
                    device.Font.DrawString("FPS: " + fpsCounter.FPS, 0, 0);
                    device.Font.DrawString("Press 1 To 4 to change Effect", 0, 30);

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


                //release resource
                dogMesh.Dispose();
                cubeMesh.Dispose();
                phongConstantBuffer.Dispose();
                renderTargetConstantBuffer.Dispose();
                phongShader.Dispose();
                renderTargetShader.Dispose();
            }
        }
コード例 #15
0
ファイル: Program.cs プロジェクト: PavelBrokhman/SharpDX_Demo
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

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

            form.Text = "Tutorial 16: Environment Mapping";
            //frame rate counter
            SharpFPS fpsCounter = new SharpFPS();


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

                //load model from wavefront obj file
                SharpMesh teapot = SharpMesh.CreateFromObj(device, "../../../Models/teapot.obj");

                //init shader
                SharpShader cubeMapPass = new SharpShader(device, "../../HLSL.txt",
                                                          new SharpShaderDescription()
                {
                    VertexShaderFunction = "VS", GeometryShaderFunction = "GS", 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 standard = new SharpShader(device, "../../HLSL.txt",
                                                       new SharpShaderDescription()
                {
                    VertexShaderFunction = "VS_SECOND", PixelShaderFunction = "PS_SECOND"
                },
                                                       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 reflection = new SharpShader(device, "../../HLSL.txt",
                                                         new SharpShaderDescription()
                {
                    VertexShaderFunction = "VS_SECOND", PixelShaderFunction = "PS_REFLECTION"
                },
                                                         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)
                });

                //render target
                SharpCubeTarget cubeTarget = new SharpCubeTarget(device, 512, Format.R8G8B8A8_UNorm);

                //init constant buffer
                Buffer11 dataConstantBuffer = cubeMapPass.CreateBuffer <Data>();

                //init frame counter
                fpsCounter.Reset();


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

                        font.Resize();
                    }

                    //apply states
                    device.UpdateAllStates();


                    //MATRICES

                    //set transformation matrix
                    float ratio = (float)form.ClientRectangle.Width / (float)form.ClientRectangle.Height;
                    //90° degree with 1 ratio
                    Matrix projection = Matrix.PerspectiveFovLH(3.14F / 2.0F, 1, 1F, 10000.0F);

                    //camera
                    Vector3 from = new Vector3(0, 30, 70);
                    Vector3 to   = new Vector3(0, 0, 0);

                    Matrix view  = Matrix.LookAtLH(from, to, Vector3.UnitY);
                    Matrix world = Matrix.Translation(0, 0, 50) * Matrix.RotationY(Environment.TickCount / 1000.0F);

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

                    //six axis
                    Matrix view1 = Matrix.LookAtLH(new Vector3(), new Vector3(1, 0, 0), Vector3.UnitY);
                    Matrix view2 = Matrix.LookAtLH(new Vector3(), new Vector3(-1, 0, 0), Vector3.UnitY);
                    Matrix view3 = Matrix.LookAtLH(new Vector3(), new Vector3(0, 1, 0), -Vector3.UnitZ);
                    Matrix view4 = Matrix.LookAtLH(new Vector3(), new Vector3(0, -1, 0), Vector3.UnitZ);
                    Matrix view5 = Matrix.LookAtLH(new Vector3(), new Vector3(0, 0, 1), Vector3.UnitY);
                    Matrix view6 = Matrix.LookAtLH(new Vector3(), new Vector3(0, 0, -1), Vector3.UnitY);



                    //BEGIN RENDERING TO CUBE TEXTURE

                    cubeTarget.Apply();
                    cubeTarget.Clear(Color.CornflowerBlue);


                    Data sceneInformation = new Data()
                    {
                        world = world,
                        worldViewProjection = world * view * projection,
                        lightDirection      = new Vector4(lightDirection, 1),
                        cameraPosition      = new Vector4(from, 1),
                        mat1 = world * view1 * projection,
                        mat2 = world * view2 * projection,
                        mat3 = world * view3 * projection,
                        mat4 = world * view4 * projection,
                        mat5 = world * view5 * projection,
                        mat6 = world * view6 * projection
                    };
                    //write data inside constant buffer
                    device.UpdateData <Data>(dataConstantBuffer, sceneInformation);


                    //apply shader
                    cubeMapPass.Apply();

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

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


                    //RENDERING TO DEVICE

                    //Set original targets
                    device.SetDefaultTargers();

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

                    //apply shader
                    standard.Apply();


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


                    projection       = Matrix.PerspectiveFovLH(3.14F / 3.0F, ratio, 1, 10000.0F);
                    sceneInformation = new Data()
                    {
                        world = world,
                        worldViewProjection = world * view * projection,
                        lightDirection      = new Vector4(lightDirection, 1),
                        cameraPosition      = new Vector4(from, 1),
                        mat1 = world * view1 * projection,
                        mat2 = world * view2 * projection,
                        mat3 = world * view3 * projection,
                        mat4 = world * view4 * projection,
                        mat5 = world * view5 * projection,
                        mat6 = world * view6 * projection
                    };
                    //write data inside constant buffer
                    device.UpdateData <Data>(dataConstantBuffer, sceneInformation);

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



                    //apply shader
                    reflection.Apply();


                    sceneInformation = new Data()
                    {
                        world = Matrix.Identity,
                        worldViewProjection = view * projection,
                        lightDirection      = new Vector4(lightDirection, 1),
                        cameraPosition      = new Vector4(from, 1),
                        mat1 = Matrix.Identity,
                        mat2 = Matrix.Identity,
                        mat3 = Matrix.Identity,
                        mat4 = Matrix.Identity,
                        mat5 = Matrix.Identity,
                        mat6 = Matrix.Identity
                    };
                    //write data inside constant buffer
                    device.UpdateData <Data>(dataConstantBuffer, sceneInformation);

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

                    //begin drawing text
                    font.Begin();

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

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


                //release resource
                font.Dispose();
                teapot.Dispose();
                dataConstantBuffer.Dispose();

                cubeMapPass.Dispose();
                standard.Dispose();
                reflection.Dispose();

                cubeTarget.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 6: Rasterizer & Alphablending";
            //frame rate counter
            SharpFPS fpsCounter = new SharpFPS();


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


                    //apply state
                    device.UpdateAllStates();

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

                    //apply shader
                    shader.Apply();

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

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

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

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

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

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


                    Matrix worldViewProjection = world * view * projection;

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

                    //draw mesh
                    mesh.Draw();

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

                    //draw second mesh
                    mesh.Draw();

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

                    //draw third mesh
                    mesh.Draw();


                    //begin drawing text
                    font.Begin();

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

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


                //release resource
                font.Dispose();
                mesh.Dispose();
                buffer.Dispose();
                texture.Dispose();
            }
        }
コード例 #17
0
ファイル: Program.cs プロジェクト: falbertp/SharpDX_Demo
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

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

            form.Text = "Tutorial 9: Normal Mapping";
            //frame rate counter
            SharpFPS fpsCounter = new SharpFPS();


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

                //load model from wavefront obj file
                SharpMesh mesh = SharpMesh.CreateNormalMappedFromObj(device, "../../../Models/dog/dog.obj");

                //init shader with normal map illumination
                SharpShader shaderNormal = new SharpShader(device, "../../HLSL_normal.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("TANGENT", 0, Format.R32G32B32_Float, 24, 0),
                    new InputElement("BINORMAL", 0, Format.R32G32B32_Float, 36, 0),
                    new InputElement("TEXCOORD", 0, Format.R32G32_Float, 48, 0)
                });

                //Init shader with standard illumination
                SharpShader shaderStandard = new SharpShader(device, "../../HLSL_standard.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("TANGENT", 0, Format.R32G32B32_Float, 24, 0),
                    new InputElement("BINORMAL", 0, Format.R32G32B32_Float, 36, 0),
                    new InputElement("TEXCOORD", 0, Format.R32G32_Float, 48, 0)
                });

                //init constant buffer
                Buffer11 buffer = shaderNormal.CreateBuffer <Data>();

                //init frame counter
                fpsCounter.Reset();

                //Used for parallax mapping
                float bias = 0.005f;
                //to active normal mapping
                bool normalMap = true;


                form.KeyDown += (sender, e) =>
                {
                    if (e.KeyCode == Keys.A)
                    {
                        bias = 0.005f;
                    }
                    else if (e.KeyCode == Keys.S)
                    {
                        bias = 0;
                    }

                    if (e.KeyCode == Keys.N)
                    {
                        normalMap = true;
                    }
                    if (e.KeyCode == Keys.D)
                    {
                        normalMap = false;
                    }
                };

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

                    //apply states
                    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, 1F, 1000.0F);
                    //set camera position and target
                    Vector3 from = new Vector3(0, 70, -150);
                    Vector3 to   = new Vector3(0, 50, 0);

                    Matrix view = Matrix.LookAtLH(from, to, Vector3.UnitY);

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

                    //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),
                        viewDirection       = new Vector4(Vector3.Normalize(from - to), 1),
                        bias = new Vector4(bias, 0, 0, 0)
                    };


                    //apply shader
                    if (normalMap)
                    {
                        shaderNormal.Apply();
                    }
                    else
                    {
                        shaderStandard.Apply();
                    }

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

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

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


                    //begin drawing text
                    font.Begin();

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

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

                //release resource
                font.Dispose();
                mesh.Dispose();
                buffer.Dispose();
                shaderNormal.Dispose();
                shaderStandard.Dispose();
            }
        }