概要
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
private Device device = null;
private VertexBuffer buffer = null;
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
}
private void Form1_Load(object sender, EventArgs e)
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
buffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 3, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[]) buffer.Lock(0, 0);
verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
verts[0].Color = Color.BlueViolet.ToArgb();
verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
verts[1].Color = Color.GreenYellow.ToArgb();
verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
verts[2].Color = Color.Red.ToArgb();
buffer.Unlock();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
int iTime = Environment.TickCount % 5000;
float fAngle = iTime * (2.0f * (float) Math.PI) / 5000.0f;
device.Transform.World = Matrix.RotationY(fAngle);
device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 1.0f, 4.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, 1.0f, 100.0f);
device.RenderState.CullMode = Cull.None;
device.RenderState.Lighting = false;
device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.SetStreamSource(0, buffer, 0);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
device.EndScene();
device.Present();
this.Invalidate();
}
}
}
以上。