{"id":2863,"date":"2019-09-17T03:54:12","date_gmt":"2019-09-16T18:54:12","guid":{"rendered":"http:\/\/okamurax.com\/?p=2863"},"modified":"2020-05-05T01:00:45","modified_gmt":"2020-05-04T16:00:45","slug":"c-directx%e5%9f%ba%e6%9c%ac%e8%89%b2%e3%80%85","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=2863","title":{"rendered":"C# DirectX\u57fa\u672c\u8272\u3005"},"content":{"rendered":"<pre class=\"lang:c# decode:true \">using System;\r\nusing System.Drawing;\r\nusing System.Windows.Forms;\r\n\r\nusing Microsoft.DirectX;\r\nusing Microsoft.DirectX.Direct3D;\r\n\r\nnamespace WindowsFormsApp5\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        private Device device = null;\r\n        private Microsoft.DirectX.Direct3D.Font _font = null; \/\/ Direct3DX\r\n\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n            this.ClientSize = new Size(640, 480);\r\n\r\n            \/\/ \u30c7\u30d0\u30a4\u30b9\u521d\u671f\u5316\r\n            SetupDevice(this);\r\n\r\n            \/\/ \u30de\u30a6\u30b9\u30a4\u30d9\u30f3\u30c8\u3067\u63cf\u753b\r\n            this.MouseMove += MainRender;\r\n\r\n            \/\/ \u51e6\u7406\u30eb\u30fc\u30d7\u306e\u5834\u5408\r\n            while (this.Created)\r\n            {\r\n                System.Threading.Thread.Sleep(1);\r\n                Application.DoEvents();\r\n            }\r\n        }\r\n        private void MainRender(object sender, EventArgs e)\r\n        {\r\n            device.Clear(\r\n                ClearFlags.Target, \/\/ \u30af\u30ea\u30a2\u3059\u308b\u30bf\u30fc\u30b2\u30c3\u30c8\r\n                Color.Blue,\r\n                1.0f,\r\n                0);\r\n\r\n            device.BeginScene();\r\n\r\n            \/\/ \u56f3\u5f62\u63cf\u753b1\r\n            DrawVertex(CreateVertex());\r\n\r\n            \/\/ \u56f3\u5f62\u63cf\u753b2\r\n            DrawMesh();\r\n\r\n            \/\/ \u6587\u5b57\u5217\u63cf\u753b\r\n            DrawText();\r\n\r\n            device.EndScene();\r\n            device.Present();\r\n        }\r\n\r\n        private void DrawMesh()\r\n        {\r\n            Material material = new Material();\r\n            material.Ambient = Color.Red;\r\n            material.Diffuse = Color.AliceBlue;\r\n\r\n            device.Material = material;\r\n\r\n            \/\/ \u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u30aa\u30d7\u30b7\u30e7\u30f3\r\n            device.RenderState.ZBufferEnable = true;\r\n\r\n            \/\/ \u30ab\u30e1\u30e9\r\n            device.Transform.View = Matrix.LookAtLH(\r\n                new Vector3(5,5,-10), \/\/ \u30ab\u30e1\u30e9\u306e\u4f4d\u7f6e\r\n                new Vector3(0,0,0), \/\/ \u30ab\u30e1\u30e9\u306e\u5411\u3044\u3066\u3044\u308b\u65b9\u5411\r\n                new Vector3(0,1,0)); \/\/ \u7a7a\u9593\u306e\u4e0a\u3092\u793a\u3059\u65b9\u5411\r\n\r\n            \/\/ \u6295\u5f71\u5909\u63db\r\n            device.Transform.Projection = Matrix.PerspectiveFovLH(\r\n                (float)Math.PI\/4, \/\/ \u8996\u754c\u306e\u5e83\u3055\r\n                1.0F,\r\n                1.0F,\r\n                100.0F);\r\n\r\n            \/\/ \u7167\u660e\r\n            device.Lights[0].Type = LightType.Directional;\r\n            device.Lights[0].Direction = new Vector3(1, -1, 1);\r\n            device.Lights[0].Diffuse = Color.FromArgb(255, 255, 255);\r\n            device.Lights[0].Enabled = true;\r\n\r\n            var mesh = Mesh.Box(device, 5, 5, 5);\r\n            mesh.DrawSubset(0);\r\n        }\r\n\r\n        private void DrawText()\r\n        {\r\n            FontDescription fd = new FontDescription();\r\n            fd.Height = 24;\r\n            fd.FaceName = \"\uff2d\uff33 \u30b4\u30b7\u30c3\u30af\";\r\n\r\n            _font = new Microsoft.DirectX.Direct3D.Font(device, fd);\r\n            _font.DrawText(\r\n                null, \/\/ Sprite\u30af\u30e9\u30b9\u306e\u4f7f\u7528\u6709\u7121\r\n                \"\u30c6\u30b9\u30c8\", \r\n                PointToClient(MousePosition).X,\r\n                PointToClient(MousePosition).Y,\r\n                Color.White);\r\n        }\r\n\r\n        public CustomVertex.TransformedColoredTextured[] CreateVertex()\r\n        {\r\n            int mouseX = PointToClient(MousePosition).X;\r\n            int mouseY = PointToClient(MousePosition).Y;\r\n            int width = 200;\r\n            int height = 100;\r\n\r\n            \/\/ TransformedColoredTextured\r\n            \/\/ \u9802\u70b9\u30c7\u30fc\u30bf\u4f5c\u6210\r\n            \/\/ \u9802\u70b9\u306e\u578b (2D\u306e\u5ea7\u6a19\u3001\u8272\u3001\u30c6\u30af\u30b9\u30c1\u30e3\u306e\u5ea7\u6a19\u3092\u4fdd\u6301)\r\n\r\n            var verts = new CustomVertex.TransformedColoredTextured[4];\r\n\r\n            \/\/ \u5de6\u4e0a\r\n            verts[0].X = mouseX;\r\n            verts[0].Y = mouseY;\r\n            verts[0].Tu = 0.0f;\r\n            verts[0].Tv = 1.0f;\r\n            verts[0].Color = Color.Green.ToArgb();\r\n\r\n            \/\/ \u53f3\u4e0a\r\n            verts[1].X = mouseX + width;\r\n            verts[1].Y = mouseY;\r\n            verts[1].Tu = 0.0f;\r\n            verts[1].Tv = 1.0f;\r\n\r\n            \/\/ \u5de6\u4e0b\r\n            verts[2].X = mouseX;\r\n            verts[2].Y = mouseY + height;\r\n            verts[2].Tu = 0.0f;\r\n            verts[2].Tv = 1.0f;\r\n\r\n            \/\/ \u53f3\u4e0b\r\n            verts[3].X = mouseX + width;\r\n            verts[3].Y = mouseY + height;\r\n            verts[3].Tu = 0.0f;\r\n            verts[3].Tv = 1.0f;\r\n\r\n            return verts;\r\n        }\r\n\r\n        private void DrawVertex(CustomVertex.TransformedColoredTextured[] verts)\r\n        {\r\n            \/\/ \u9802\u70b9\u914d\u5217\u3092\u9802\u70b9\u30d0\u30c3\u30d5\u30a1\u3078\r\n\r\n            if (device == null) return;\r\n\r\n            VertexBuffer vertexBuffer = new VertexBuffer(\r\n                typeof(CustomVertex.TransformedColoredTextured),\r\n                4,\r\n                device,\r\n                0,\r\n                CustomVertex.TransformedColoredTextured.Format,\r\n                Pool.Managed);\r\n\r\n            GraphicsStream stm = vertexBuffer.Lock(0, 0, 0);\r\n            stm.Write(verts);\r\n            vertexBuffer.Unlock();\r\n            device.SetStreamSource(0, vertexBuffer, 0);\r\n            device.VertexFormat = CustomVertex.TransformedColoredTextured.Format;\r\n            device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);\r\n        }\r\n\r\n        public void SetupDevice(Control target)\r\n        {\r\n            PresentParameters pp = new PresentParameters();\r\n            pp.Windowed = true;\r\n            pp.SwapEffect = SwapEffect.Discard;\r\n            pp.PresentationInterval = PresentInterval.Immediate;\r\n\r\n            \/\/ \u901a\u5e38\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u51e6\u7406\u306e\u65b9\u304c\u9ad8\u6027\u80fd\u3060\u304b\u30b0\u30e9\u30d5\u30a3\u30c3\u30af\u30ab\u30fc\u30c9\u306a\u3069\u306b\u3088\u308a\u8981\u5909\u66f4\r\n\r\n            device = new Device(\r\n                0, \/\/ \u7269\u7406\u30c7\u30d0\u30a4\u30b9\u30a4\u30f3\u30c7\u30c3\u30af\u30b9(\u901a\u5e380)\r\n                DeviceType.Hardware,\u3000\/\/ \u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u30a2\u30af\u30bb\u30e9\u30ec\u30fc\u30b7\u30e7\u30f3\u6709\u52b9\u5316\r\n                target,\r\n                CreateFlags.HardwareVertexProcessing, \/\/ \u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u306b\u3088\u308b\u9802\u70b9\u51e6\u7406\u6709\u52b9\u5316\r\n                pp);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using System; using System.Drawing; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.Direc &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=2863\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# DirectX\u57fa\u672c\u8272\u3005&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9],"class_list":["post-2863","post","type-post","status-publish","format-standard","hentry","category-1","tag-c-net"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2863"}],"version-history":[{"count":1,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2863\/revisions"}],"predecessor-version":[{"id":2864,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2863\/revisions\/2864"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}