cscの作法 その116

# 概要
cscの作法、調べてみた。
managed directxやってみた。
マウスでぐりぐり、やってみた。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/5a612930-d26f-1b54-d6ea-5ee91378c9ac.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
		};
		private float cheta = 270.0f;
		private float chi = 0.0f;
		private Point oPoint = Point.Empty;
		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(-2.0f, 2.0f, 2.0f, Color.Yellow.ToArgb());
			vertices[1] = new CustomVertex.PositionColored(2.0f, 2.0f, 2.0f, Color.Gray.ToArgb());
			vertices[2] = new CustomVertex.PositionColored(-2.0f, 2.0f, -2.0f, Color.Purple.ToArgb());
			vertices[3] = new CustomVertex.PositionColored(2.0f, 2.0f, -2.0f, Color.Red.ToArgb());
			vertices[4] = new CustomVertex.PositionColored(-2.0f, -2.0f, 2.0f, Color.SkyBlue.ToArgb());
			vertices[5] = new CustomVertex.PositionColored(2.0f, -2.0f, 2.0f, Color.Orange.ToArgb());
			vertices[6] = new CustomVertex.PositionColored(-2.0f, -2.0f, -2.0f, Color.Green.ToArgb());
			vertices[7] = new CustomVertex.PositionColored(2.0f, -2.0f, -2.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;
			this.MouseMove += new MouseEventHandler(Form1_MouseMove);
		}
		private void Form1_Paint(object sender, PaintEventArgs e)
		{
			device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
			float radius = 10.0f;
			float theta = Geometry.DegreeToRadian(cheta);
			float phi = Geometry.DegreeToRadian(chi);
			Vector3 camera = new Vector3((float)(radius * Math.Cos(theta) * Math.Cos(phi)), (float)(radius * Math.Sin(phi)), (float)(radius * Math.Sin(theta) * Math.Cos(phi)));
			device.Transform.View = Matrix.LookAtLH(camera, 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.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();
		}
		private void Form1_MouseMove(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Left)
			{
				cheta -= e.Location.X - oPoint.X;
				chi += e.Location.Y - oPoint.Y;
				if (chi >= 90.0f)
				{
					chi = 89.9999f;
				}
				else if (chi <= -90.0f)
				{
					chi = -89.9999f;
				}
			}
			oPoint = e.Location;
		}
		[STAThread]
		public static void Main() {
			Application.Run(new Form1());
		}
	}
}





```


以上。