1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
using System; using System.Drawing; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; namespace WindowsFormsApp5 { public partial class Form1 : Form { private Device device = null; private Microsoft.DirectX.Direct3D.Font _font = null; // Direct3DX public Form1() { InitializeComponent(); this.ClientSize = new Size(640, 480); // デバイス初期化 SetupDevice(this); // マウスイベントで描画 this.MouseMove += MainRender; // 処理ループの場合 while (this.Created) { System.Threading.Thread.Sleep(1); Application.DoEvents(); } } private void MainRender(object sender, EventArgs e) { device.Clear( ClearFlags.Target, // クリアするターゲット Color.Blue, 1.0f, 0); device.BeginScene(); // 図形描画1 DrawVertex(CreateVertex()); // 図形描画2 DrawMesh(); // 文字列描画 DrawText(); device.EndScene(); device.Present(); } private void DrawMesh() { Material material = new Material(); material.Ambient = Color.Red; material.Diffuse = Color.AliceBlue; device.Material = material; // レンダリングオプション device.RenderState.ZBufferEnable = true; // カメラ device.Transform.View = Matrix.LookAtLH( new Vector3(5,5,-10), // カメラの位置 new Vector3(0,0,0), // カメラの向いている方向 new Vector3(0,1,0)); // 空間の上を示す方向 // 投影変換 device.Transform.Projection = Matrix.PerspectiveFovLH( (float)Math.PI/4, // 視界の広さ 1.0F, 1.0F, 100.0F); // 照明 device.Lights[0].Type = LightType.Directional; device.Lights[0].Direction = new Vector3(1, -1, 1); device.Lights[0].Diffuse = Color.FromArgb(255, 255, 255); device.Lights[0].Enabled = true; var mesh = Mesh.Box(device, 5, 5, 5); mesh.DrawSubset(0); } private void DrawText() { FontDescription fd = new FontDescription(); fd.Height = 24; fd.FaceName = "MS ゴシック"; _font = new Microsoft.DirectX.Direct3D.Font(device, fd); _font.DrawText( null, // Spriteクラスの使用有無 "テスト", PointToClient(MousePosition).X, PointToClient(MousePosition).Y, Color.White); } public CustomVertex.TransformedColoredTextured[] CreateVertex() { int mouseX = PointToClient(MousePosition).X; int mouseY = PointToClient(MousePosition).Y; int width = 200; int height = 100; // TransformedColoredTextured // 頂点データ作成 // 頂点の型 (2Dの座標、色、テクスチャの座標を保持) var verts = new CustomVertex.TransformedColoredTextured[4]; // 左上 verts[0].X = mouseX; verts[0].Y = mouseY; verts[0].Tu = 0.0f; verts[0].Tv = 1.0f; verts[0].Color = Color.Green.ToArgb(); // 右上 verts[1].X = mouseX + width; verts[1].Y = mouseY; verts[1].Tu = 0.0f; verts[1].Tv = 1.0f; // 左下 verts[2].X = mouseX; verts[2].Y = mouseY + height; verts[2].Tu = 0.0f; verts[2].Tv = 1.0f; // 右下 verts[3].X = mouseX + width; verts[3].Y = mouseY + height; verts[3].Tu = 0.0f; verts[3].Tv = 1.0f; return verts; } private void DrawVertex(CustomVertex.TransformedColoredTextured[] verts) { // 頂点配列を頂点バッファへ if (device == null) return; VertexBuffer vertexBuffer = new VertexBuffer( typeof(CustomVertex.TransformedColoredTextured), 4, device, 0, CustomVertex.TransformedColoredTextured.Format, Pool.Managed); GraphicsStream stm = vertexBuffer.Lock(0, 0, 0); stm.Write(verts); vertexBuffer.Unlock(); device.SetStreamSource(0, vertexBuffer, 0); device.VertexFormat = CustomVertex.TransformedColoredTextured.Format; device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); } public void SetupDevice(Control target) { PresentParameters pp = new PresentParameters(); pp.Windowed = true; pp.SwapEffect = SwapEffect.Discard; pp.PresentationInterval = PresentInterval.Immediate; // 通常ハードウェア処理の方が高性能だかグラフィックカードなどにより要変更 device = new Device( 0, // 物理デバイスインデックス(通常0) DeviceType.Hardware, // ハードウェアアクセラレーション有効化 target, CreateFlags.HardwareVertexProcessing, // ハードウェアによる頂点処理有効化 pp); } } } |