コード例 #1
0
        //sewer's code
        public static void DetermineRenderStuff2(CLFile data)
        {
            List <Vertex> QuadNodeVertexList = new List <Vertex>();
            List <Int32>  QuadNodeIndexList  = new List <Int32>();

            Int32 k = 0;

            foreach (CLQuadNode i in data.CLQuadNodeList)
            {
                if (Program.collisionEditor.checkedListBox1.CheckedIndices.Contains(i.Index))
                {
                    QuadNodeVertexList.Add(new Vertex(new Vector3(i.NodeSquare.X, 100 / (i.Depth + 1), i.NodeSquare.Y)));
                    QuadNodeIndexList.Add(k);
                    QuadNodeIndexList.Add(k + 1);
                    QuadNodeVertexList.Add(new Vertex(new Vector3(i.NodeSquare.X + i.NodeSquare.Width, 100 / (i.Depth + 1), i.NodeSquare.Y)));
                    QuadNodeIndexList.Add(k + 1);
                    QuadNodeIndexList.Add(k + 2);
                    QuadNodeVertexList.Add(new Vertex(new Vector3(i.NodeSquare.X + i.NodeSquare.Width, 100 / (i.Depth + 1), i.NodeSquare.Y + i.NodeSquare.Height)));
                    QuadNodeIndexList.Add(k + 2);
                    QuadNodeIndexList.Add(k + 3);
                    QuadNodeVertexList.Add(new Vertex(new Vector3(i.NodeSquare.X, 100 / (i.Depth + 1), i.NodeSquare.Y + i.NodeSquare.Height)));
                    QuadNodeIndexList.Add(k + 3);
                    QuadNodeIndexList.Add(k);
                    k += 4;
                }
            }

            secondQuadTreeMesh = SharpMesh.Create(SharpRenderer.device, QuadNodeVertexList.ToArray(), QuadNodeIndexList.ToArray(), new List <SharpSubSet>()
            {
                new SharpSubSet(0, QuadNodeIndexList.Count, null)
            }, SharpDX.Direct3D.PrimitiveTopology.LineList);
            secondQuadtreeRenderData.Color = new Vector4(0.3f, 0.9f, 0.6f, 1f);
        }
コード例 #2
0
        public static void DetermineQuadtreeRenderStuff(ref CLFile data)
        {
            List <Vertex> QuadNodeVertexList = new List <Vertex>();
            List <Int32>  QuadNodeIndexList  = new List <Int32>();

            Int32 k = 0;

            foreach (CLQuadNode i in data.CLQuadNodeList)
            {
                Program.collisionEditor.progressBar1.PerformStep();
                if (i.Child == 0 | i.Index == 0)
                {
                    QuadNodeVertexList.Add(new Vertex(new Vector3(i.NodeSquare.X, 0, i.NodeSquare.Y)));
                    QuadNodeIndexList.Add(k);
                    QuadNodeIndexList.Add(k + 1);
                    QuadNodeVertexList.Add(new Vertex(new Vector3(i.NodeSquare.X + i.NodeSquare.Width, 0, i.NodeSquare.Y)));
                    QuadNodeIndexList.Add(k + 1);
                    QuadNodeIndexList.Add(k + 2);
                    QuadNodeVertexList.Add(new Vertex(new Vector3(i.NodeSquare.X + i.NodeSquare.Width, 0, i.NodeSquare.Y + i.NodeSquare.Height)));
                    QuadNodeIndexList.Add(k + 2);
                    QuadNodeIndexList.Add(k + 3);
                    QuadNodeVertexList.Add(new Vertex(new Vector3(i.NodeSquare.X, 0, i.NodeSquare.Y + i.NodeSquare.Height)));
                    QuadNodeIndexList.Add(k + 3);
                    QuadNodeIndexList.Add(k);
                    k += 4;
                }
            }

            quadTreeMesh = SharpMesh.Create(device, QuadNodeVertexList.ToArray(), QuadNodeIndexList.ToArray(), new List <SharpSubSet>()
            {
                new SharpSubSet(0, QuadNodeIndexList.Count, null)
            }, SharpDX.Direct3D.PrimitiveTopology.LineList);
            mainQuadtreeRenderData.Color = new Vector4(0.9f, 0.3f, 0.6f, 1f);
        }
コード例 #3
0
            public void SetRenderStuff()
            {
                if (splineMesh != null)
                {
                    splineMesh.Dispose();
                }

                splineMesh = SharpMesh.Create(SharpRenderer.device, Points, ReadWriteCommon.Range(Points.Length), new List <SharpSubSet>()
                {
                    new SharpSubSet(0, Points.Length, null)
                }, SharpDX.Direct3D.PrimitiveTopology.LineStrip);
            }
コード例 #4
0
        protected void CreateMesh(SharpRenderer renderer, Vector3[] vertices)
        {
            if (splineMesh != null)
            {
                splineMesh.Dispose();
            }

            if (vertices.Length > 1)
            {
                splineMesh = SharpMesh.Create(renderer.Device, vertices, Range(vertices.Length), new List <SharpSubSet>()
                {
                    new SharpSubSet(0, vertices.Length, null)
                }, SharpDX.Direct3D.PrimitiveTopology.LineStrip);
            }
            else
            {
                splineMesh = null;
            }
        }
コード例 #5
0
        public void DetermineQuadtreeRenderStuff(ref CLFile data, SharpDevice device, ProgressBar bar)
        {
            List <Vertex> QuadNodeVertexList = new List <Vertex>();
            List <int>    QuadNodeIndexList  = new List <int>();

            int k = 0;

            foreach (QuadNode i in data.CLQuadNodeList)
            {
                bar.PerformStep();
                if (i.Child == 0 | i.Index == 0)
                {
                    QuadNodeVertexList.Add(new Vertex(new Vector3(i.NodeSquare.X, 0, i.NodeSquare.Y)));
                    QuadNodeIndexList.Add(k);
                    QuadNodeIndexList.Add(k + 1);
                    QuadNodeVertexList.Add(new Vertex(new Vector3(i.NodeSquare.X + i.NodeSquare.Width, 0, i.NodeSquare.Y)));
                    QuadNodeIndexList.Add(k + 1);
                    QuadNodeIndexList.Add(k + 2);
                    QuadNodeVertexList.Add(new Vertex(new Vector3(i.NodeSquare.X + i.NodeSquare.Width, 0, i.NodeSquare.Y + i.NodeSquare.Height)));
                    QuadNodeIndexList.Add(k + 2);
                    QuadNodeIndexList.Add(k + 3);
                    QuadNodeVertexList.Add(new Vertex(new Vector3(i.NodeSquare.X, 0, i.NodeSquare.Y + i.NodeSquare.Height)));
                    QuadNodeIndexList.Add(k + 3);
                    QuadNodeIndexList.Add(k);
                    k += 4;
                }
            }

            if (quadTreeMesh != null)
            {
                quadTreeMesh.Dispose();
            }

            quadTreeMesh = SharpMesh.Create(device, QuadNodeVertexList.ToArray(), QuadNodeIndexList.ToArray(), new List <SharpSubSet>()
            {
                new SharpSubSet(0, QuadNodeIndexList.Count, null)
            }, SharpDX.Direct3D.PrimitiveTopology.LineList);
            mainQuadtreeRenderData.Color = new Vector4(0.9f, 0.3f, 0.6f, 1f);
        }
コード例 #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 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();
            }
        }
コード例 #7
0
        public CLFile LoadCLFile(string FileName, SharpDevice device, ProgressBar bar)
        {
            CLFile data = new CLFile(0);

            BinaryReader CLReader = new BinaryReader(new FileStream(FileName, FileMode.Open));

            CLReader.BaseStream.Position = 0;

            data.numBytes = Switch(CLReader.ReadUInt32());
            //Get total number of bytes in file

            if (data.numBytes != CLReader.BaseStream.Length)
            {
                throw new ArgumentException("Not a valid CL file.");
            }

            //Get offset of structs
            data.pointQuadtree = Switch(CLReader.ReadUInt32());
            data.pointTriangle = Switch(CLReader.ReadUInt32());
            data.pointVertex   = Switch(CLReader.ReadUInt32());

            //Get quadtree center and radius
            data.quadCenterX = Switch(CLReader.ReadSingle());
            data.quadCenterY = Switch(CLReader.ReadSingle());
            data.quadCenterZ = Switch(CLReader.ReadSingle());
            data.quadLength  = Switch(CLReader.ReadSingle());

            //Get amount of stuff
            data.basePower    = Switch(CLReader.ReadUInt16());
            data.numTriangles = Switch(CLReader.ReadUInt16());
            data.numVertices  = Switch(CLReader.ReadUInt16());
            data.numQuadnodes = Switch(CLReader.ReadUInt16());

            bar.Maximum = data.numTriangles + data.numVertices + 3 * data.numQuadnodes;

            List <Triangle>        CLTriangleList = new List <Triangle>(data.numTriangles);
            List <CollisionVertex> CLVertexList   = new List <CollisionVertex>(data.numVertices);

            data.MeshTypeList = new List <UInt64>();

            for (int i = 0; i < data.numVertices; i++)
            {
                CLReader.BaseStream.Position = data.pointVertex + i * 0xC;
                CLVertexList.Add(new CollisionVertex(Switch(CLReader.ReadSingle()), Switch(CLReader.ReadSingle()), Switch(CLReader.ReadSingle())));
                bar.PerformStep();
            }

            for (int i = 0; i < data.numTriangles; i++)
            {
                CLReader.BaseStream.Position = data.pointTriangle + i * 0x20;
                CLTriangleList.Add(new Triangle(Switch(CLReader.ReadUInt16()), Switch(CLReader.ReadUInt16()), Switch(CLReader.ReadUInt16())));
                CLReader.BaseStream.Position += 6;

                Vector3 Normals = new Vector3(Switch(CLReader.ReadSingle()), Switch(CLReader.ReadSingle()), Switch(CLReader.ReadSingle()));
                CLVertexList[CLTriangleList[i].Vertices[0]].NormalList.Add(Normals);
                CLVertexList[CLTriangleList[i].Vertices[1]].NormalList.Add(Normals);
                CLVertexList[CLTriangleList[i].Vertices[2]].NormalList.Add(Normals);

                UInt64 FlagsAsUint64 = CLReader.ReadUInt64();
                CLTriangleList[i].ColFlags = BitConverter.GetBytes(FlagsAsUint64);
                if (!data.MeshTypeList.Contains(FlagsAsUint64))
                {
                    data.MeshTypeList.Add(FlagsAsUint64);
                }

                Color TempColor = new Color(CLTriangleList[i].ColFlags[1], CLTriangleList[i].ColFlags[2], CLTriangleList[i].ColFlags[3]);

                if (TempColor.R == 0)
                {
                    TempColor.R = 255;
                }
                else
                {
                    TempColor.R = (byte)(256 - (Math.Log(TempColor.R, 2) + 1) * 32);
                }
                if (TempColor.G == 0)
                {
                    TempColor.G = 255;
                }
                else
                {
                    TempColor.G = (byte)(256 - (Math.Log(TempColor.G, 2) + 1) * 32);
                }
                if (TempColor.B == 0)
                {
                    TempColor.B = 255;
                }
                else
                {
                    TempColor.B = (byte)(256 - (Math.Log(TempColor.B, 2) + 1) * 32);
                }

                CLVertexList[CLTriangleList[i].Vertices[0]].Color = TempColor;
                CLVertexList[CLTriangleList[i].Vertices[1]].Color = TempColor;
                CLVertexList[CLTriangleList[i].Vertices[2]].Color = TempColor;

                bar.PerformStep();
            }

            data.CLTriangleArray = CLTriangleList.ToArray();

            for (int i = 0; i < data.numQuadnodes; i++)
            {
                CLReader.BaseStream.Position = data.pointQuadtree + i * 0x20;
                QuadNode TempNode = new QuadNode
                {
                    Index  = Switch(CLReader.ReadUInt16()),
                    Parent = Switch(CLReader.ReadUInt16()),
                    Child  = Switch(CLReader.ReadUInt16())
                };
                CLReader.BaseStream.Position += 8;
                TempNode.NodeTriangleAmount   = Switch(CLReader.ReadUInt16());
                TempNode.TriListOffset        = Switch(CLReader.ReadUInt32());
                TempNode.PosValueX            = Switch(CLReader.ReadUInt16());
                TempNode.PosValueZ            = Switch(CLReader.ReadUInt16());
                TempNode.Depth = CLReader.ReadByte();

                data.CLQuadNodeList.Add(TempNode);
                bar.PerformStep();
            }

            ReBuildQuadtree(ref data, bar);
            DetermineQuadtreeRenderStuff(ref data, device, bar);

            CLReader.Close();

            data.CLVertexArray = new VertexColoredNormalized[CLVertexList.Count()];
            for (int i = 0; i < CLVertexList.Count; i++)
            {
                data.CLVertexArray[i] = new VertexColoredNormalized(CLVertexList[i].Position,
                                                                    CLVertexList[i].CalculateNormals(),
                                                                    CLVertexList[i].Color);
            }

            int[] IndexArray = new int[data.CLTriangleArray.Count() * 3];

            for (int i = 0; i < data.CLTriangleArray.Count() * 3; i++)
            {
                IndexArray[i] = data.CLTriangleArray[i / 3].Vertices[i % 3];
            }

            if (collisionMesh != null)
            {
                collisionMesh.Dispose();
            }

            collisionMesh = SharpMesh.Create(device, data.CLVertexArray, IndexArray, new List <SharpSubSet>()
            {
                new SharpSubSet(0, IndexArray.Count(), null)
            }, SharpDX.Direct3D.PrimitiveTopology.TriangleList);

            return(data);
        }
コード例 #8
0
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

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

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


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

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


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

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

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

                fpsCounter.Reset();

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

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

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

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

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

                    shader.Apply();

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

                    //remove unused shader
                    shader.Clear();

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

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

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


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

            RenderForm form = new RenderForm();

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

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

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



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

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

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

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

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

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

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

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

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

                fpsCounter.Reset();

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


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

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

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

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

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

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

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

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

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

                    //apply states
                    device.UpdateAllStates();

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

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

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

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

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

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

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

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

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

                    //apply shader
                    shader.Apply();

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

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

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

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


                    //flush text to view
                    device.Font.End();
                    //present
                    device.Present();
                });
            }
        }
コード例 #10
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();
            }
        }
コード例 #11
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();
            }
        }
コード例 #12
0
        static void Main(string[] args)
        {
            //int? i = 10;
            //Console.WriteLine(i.GetType()); // Displays
            //Type type = TypedReference.GetTargetType(__makeref(i));
            //Console.WriteLine((type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)).ToString());
            //Console.ReadLine();
            //return;

            //TreeTest.Run();
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //using (var frm = new Form1())
            //{
            //    Application.Run(frm);
            //}



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

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

            form.Text = "Tutorial 3: Font";
            int count = 0;


            Global.doc = XDocument.Load(@".\\..\\..\\XMLfile1.xml");

            Global.dta = Global.doc.Descendants("lites").Descendants("lite").Select(n => new TreeData()
            {
                row   = (int)n.Attribute("row"),
                ctr   = (int)n.Attribute("cir"),
                col   = (int)n.Attribute("col"),
                ndx   = (int)n.Attribute("ndx"),
                color = Colors[(int)(ColorEnum)Enum.Parse(typeof(ColorEnum), (string)n.Attribute("color"))],
            }).OrderBy(t => t.ndx).ToArray();

            Global.dict = Global.dta.ToDictionary(d => Tuple.Create <int, int>(d.row, d.ctr), d => d);



            int[] indices = Enumerable.Range(0, Global.dta.Count()).ToArray();

            ColoredVertex[] vertices =
                Global.dta.Select(x => new ColoredVertex(new Vector3((x.ctr), -(x.row - 16) * 2, -5),
                                                         new Vector4(x.color.R / 255.0f, x.color.G / 255.0f, x.color.B / 255.0f, 1)
                                                         )).ToArray();

            using (SharpDevice device = new SharpDevice(form))

                //Init Font
                using (SharpBatch font = new SharpBatch(device, "../../textfont.dds"))

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

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

                            //create constant buffer
                            using (Buffer11 buffer = shader.CreateBuffer <Matrix>())
                            {
                                SharpFPS fpsCounter = new SharpFPS();
                                fpsCounter.Reset();

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

                                    //apply states
                                    device.UpdateAllStates();

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

                                    //apply shader
                                    shader.Apply();


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

                                    //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, 0, -50), new Vector3(0, 0, 0), Vector3.UnitY);
                                    Matrix world      = Matrix.RotationY(0); // (float)Math.PI /2);
                                    Matrix WVP        = world * view * projection;

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

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

                                    //draw mesh
                                    mesh.DrawPoints(vertices.Length);


                                    ////Set matrices
                                    //world = Matrix.RotationZ( (float)Math.PI /2);
                                    //WVP = world * view * projection;

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

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

                                    //////apply shader
                                    ////shader.Apply();

                                    ////draw mesh
                                    //mesh.DrawPoints(vertices.Length);


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



                                    //begin drawing text
                                    font.Begin();

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

                                    font.DrawString("Current Time " + DateTime.Now.ToString(), 0, 32, Color.White);

                                    //flush text to view
                                    font.End();

                                    //present
                                    device.Present();
                                });
                            }



            Console.WriteLine("Completed.");
            //Console.ReadLine();
            return;

            Dictionary <string, int> crdtypes = new Dictionary <string, int>(50);

            using (StreamReader rdr = new StreamReader("c:\\temp\\popmoney\\rmaccounts"))
            {
                while (!rdr.EndOfStream)
                {
                    string rcd = rdr.ReadLine();
                    if (rcd.Length != 78)
                    {
                    }
                    Console.WriteLine(rcd.Length.ToString());
                    string typ = rcd.Substring(53, 2);
                    int    lng = rcd.Substring(30, 23).Trim().Length;
                    if (!crdtypes.ContainsKey(rcd))
                    {
                        crdtypes[typ] = 0;
                    }
                    if (crdtypes[typ] < lng)
                    {
                        crdtypes[typ] = lng;
                    }
                }
            }
            foreach (var dict in crdtypes.AsQueryable())
            {
                Console.WriteLine(dict.Key + ":" + dict.Value.ToString());
            }
            Console.ReadLine();
        }
コード例 #13
0
ファイル: Program.cs プロジェクト: PavelBrokhman/SharpDX_Demo
        static void Main()
        {
            if (!SharpDevice.IsDirectX11Supported())
            {
                System.Windows.Forms.MessageBox.Show("DirectX11 Not Supported");
                return;
            }

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

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


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

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

            //number of cube
            int count = 1000;

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

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

                fpsCounter.Reset();

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

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

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


                    //apply state
                    device.UpdateAllStates();

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

                    //apply shader
                    shader.Apply();



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

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

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

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

                    //draw mesh
                    mesh.DrawPoints(count);


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

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

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

                //release resource
                font.Dispose();
                mesh.Dispose();
                buffer.Dispose();
            }
        }
コード例 #14
0
 public SharpMesh createMesh(SharpDevice device)
 {
     this.mesh = SharpMesh.Create <ColoredVertex>(device, vertices, indices);
     return(mesh);
 }