概要
c#で、3Dやりたかった。
キーで、回転を操作してみた。
写真
サンプルコード
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
private Device device = null;
private Microsoft.DirectX.Direct3D.Font font;
private Mesh mesh = null;
float x = 0,
y = 0,
z = 0;
private Matrix rot = Matrix.Identity;
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
}
private void Form1_Load(object sender, EventArgs e)
{
PresentParameters pp = new PresentParameters();
pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, pp);
info = string.Format("あす");
mesh = Mesh.Cylinder(device, 0.2f, 0.8f, 1.5f, 8, 1);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
rot.Multiply(Matrix.RotationYawPitchRoll(y, x, z));
device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 1.0f, 7.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));
device.Transform.Projection = Matrix.PerspectiveFovLH((float)(Math.PI / 4), 1.0f, 0.1f, 1000.0f);
device.RenderState.Lighting = false;
device.Clear(ClearFlags.Target, Color.DarkBlue, 1.0f, 0);
device.BeginScene();
device.SetTransform(TransformType.World, Matrix.Identity);
device.Transform.World = rot;
mesh.DrawSubset(0);
device.EndScene();
device.Present();
this.Invalidate();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Escape:
this.Close();
break;
case Keys.F1:
break;
case Keys.F2:
Console.WriteLine("f2");
break;
case Keys.Up:
z -= (float)Math.PI / 90f;
break;
case Keys.Down:
z += (float)Math.PI / 90f;
break;
case Keys.Z:
x -= (float)Math.PI / 90f;
break;
case Keys.A:
x += (float)Math.PI / 90f;
break;
case Keys.Right:
y -= (float)Math.PI / 90f;
break;
case Keys.Left:
y += (float)Math.PI / 90f;
break;
case Keys.Shift:
break;
}
}
}
}
以上。