cscの作法 その115

# 概要
cscの作法、調べてみた。
managed directxやってみた。
IndexBuffer 使ってみた。


# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/ac0244d1-cb20-10a3-318a-c6765f733f10.png)


# サンプルコード

```
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace mdx
{
	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() {
			Text = "Form1";
			this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
			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();
			}
			this.Paint += Form1_Paint;
		}
		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();
		}
		[STAThread]
		public static void Main() {
			Application.Run(new Form1());
		}
	}
}




```

以上。