Exemplo n.º 1
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 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();
            }
        }
Exemplo n.º 2
0
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

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

            using (SharpDevice device = new SharpDevice(form))
            {
                //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();
                    }

                    //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
                    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
                teapot.Dispose();
                dataConstantBuffer.Dispose();

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

                cubeTarget.Dispose();

            }
        }
Exemplo n.º 3
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 font
                SharpBatch font = new SharpBatch(device, "textfont.dds");

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

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

                //init frame counter
                fpsCounter.Reset();

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

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

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

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

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

                });

                //release resource
                font.Dispose();
                dogMesh.Dispose();
                cubeMesh.Dispose();
                phongConstantBuffer.Dispose();
                renderTargetConstantBuffer.Dispose();
                phongShader.Dispose();
                renderTargetShader.Dispose();

            }
        }
Exemplo n.º 4
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();
            }
        }
Exemplo n.º 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 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();

            }
        }
Exemplo n.º 6
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 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();
            }
        }
Exemplo n.º 7
0
        private static void OnUpdate( SharpDevice device )
        {
            // ここでしかdevice手に入らない、まだロードが完全でない
            if ( RefModel != null )
            {
                if ( RefModel.Mesh == null )
                {
                    RefModel.LoadTexture( device );
                }
            }
            //set transformation matrix
            float ratio = ( float )Form.ClientRectangle.Width / Form.ClientRectangle.Height;
            //90° degree with 1 ratio
            Projection = Camera.Projection;
            //Resizing
            if ( device.MustResize )
            {
                device.Resize( );
                OnResizeForm( ratio , device );

            }

            //apply states
            device.UpdateAllStates( );
            PreViewUpdate( device );

            //MATRICES

            //camera

            View = Camera.GetView( );
            //View = Matrix.LookAtLH(new Vector3(0, 30, 70), new Vector3(0, 0, 0), Vector3.UnitY);
            Camera.Update( Mouse , FpsCounter.Delta * 0.001f );
            Mouse.Update( );
            Vector3 from = Camera.Position;
            if ( !float.IsNaN( from.X ) )
            {
                Debug.vdb_label( "campos" );
                Debug.Send( from.DebugStr( ) );
            }

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

            //RENDERING TO DEVICE

            //Set original targets
            device.SetDefaultTargers( );

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

            //apply shader
            Model.Update( device , View , Projection );
            RefModel?.Update( device , View , Projection );
            Axis.Update( device , View , Projection );

#if DEBUGLINE
            Line.Update(device, View , Projection);
#endif

            PostViewUpdate( device );

            //present
            device.Present( );

        }