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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


                    //apply state
                    device.UpdateAllStates();

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

                    //apply shader
                    shader.Apply();

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

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

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

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

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

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


                    Matrix worldViewProjection = world * view * projection;

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

                    //draw mesh
                    mesh.Draw();

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

                    //draw second mesh
                    mesh.Draw();

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

                    //draw third mesh
                    mesh.Draw();


                    //begin drawing text
                    font.Begin();

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

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


                //release resource
                font.Dispose();
                mesh.Dispose();
                buffer.Dispose();
                texture.Dispose();
            }
        }
Exemple #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 6: Rasterizer & Alphablending";
            //frame rate counter
            SharpFPS fpsCounter = new SharpFPS();

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

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

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

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

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

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

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

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

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

                    //apply state
                    device.UpdateAllStates();

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

                    //apply shader
                    shader.Apply();

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

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

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

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

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

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

                    Matrix worldViewProjection = world * view * projection;

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

                    //draw mesh
                    mesh.Draw();

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

                    //draw second mesh
                    mesh.Draw();

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

                    //draw third mesh
                    mesh.Draw();

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

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

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

                });

                //release resource
                mesh.Dispose();
                buffer.Dispose();
                texture.Dispose();
            }
        }
Exemple #4
0
        static void Main(string[] args)
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

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

            form.Text = "Tutorial 4: Primitives";

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

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

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

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


                fpsCounter.Reset();

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

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

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

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

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

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

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

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

                    //apply states
                    device.UpdateAllStates();

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

                    //apply shader
                    shader.Apply();

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


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

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

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

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

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

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

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


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

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

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


                    //draw mesh
                    water.Draw();

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

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

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

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