概要
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 WindowsFormsApplication4
{
public partial class Form1 : Form
{
private Device device = null;
private VertexBuffer _vertexBuffer = null;
private IndexBuffer _indexBuffer = null;
private static Int16[] _vertexIndices = new Int16[] {
2, 0, 1,
1, 3, 2,
4, 0, 2,
2, 6, 4,
5, 1, 0,
0, 4, 5,
7, 3, 1,
1, 5, 7,
6, 2, 3,
3, 7, 6,
4, 6, 7,
7, 5, 4
};
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);
_vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 8, device, Usage.None, CustomVertex.PositionColored.Format, Pool.Managed);
CustomVertex.PositionColored[] vertices = new CustomVertex.PositionColored[8];
vertices[0] = new CustomVertex.PositionColored(-1.0f, 1.0f, 1.0f, Color.Yellow.ToArgb());
vertices[1] = new CustomVertex.PositionColored(1.0f, 1.0f, 1.0f, Color.Gray.ToArgb());
vertices[2] = new CustomVertex.PositionColored(-1.0f, 1.0f, -1.0f, Color.Purple.ToArgb());
vertices[3] = new CustomVertex.PositionColored(1.0f, 1.0f, -1.0f, Color.Red.ToArgb());
vertices[4] = new CustomVertex.PositionColored(-1.0f, -1.0f, 1.0f, Color.SkyBlue.ToArgb());
vertices[5] = new CustomVertex.PositionColored(1.0f, -1.0f, 1.0f, Color.Orange.ToArgb());
vertices[6] = new CustomVertex.PositionColored(-1.0f, -1.0f, -1.0f, Color.Green.ToArgb());
vertices[7] = new CustomVertex.PositionColored(1.0f, -1.0f, -1.0f, Color.Blue.ToArgb());
using (GraphicsStream data = _vertexBuffer.Lock(0, 0, LockFlags.None))
{
data.Write(vertices);
_vertexBuffer.Unlock();
}
_indexBuffer = new IndexBuffer(device, 12 * 3 * 2, Usage.WriteOnly, Pool.Managed, true);
using (GraphicsStream data = _indexBuffer.Lock(0, 0, LockFlags.None))
{
data.Write(_vertexIndices);
this._indexBuffer.Unlock();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
Matrix view,
w;
w = Matrix.RotationY(Environment.TickCount / 1000.0f);
view = Matrix.Translation(0, 0, 10);
view = w * view;
device.SetTransform(TransformType.View, view);
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 100.0f);
device.RenderState.Lighting = false;
device.BeginScene();
device.SetStreamSource(0, _vertexBuffer, 0);
device.VertexFormat = CustomVertex.PositionColored.Format;
device.Indices = _indexBuffer;
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12);
device.EndScene();
device.Present();
this.Invalidate();
}
}
}
以上。