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