cscの作法 その119

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

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/d2bdab9f-7d0e-adf1-f58a-a3d29795ed5e.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 Texture _texture;
		private CustomVertex.PositionTextured[] vertices;
		private short[] indices;
		private Mesh meshTerrain;
		public Form1() {
			Text = "Form1";
			this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
			PresentParameters pp = new PresentParameters();
			pp.Windowed = true;
			pp.SwapEffect = SwapEffect.Discard;
			device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, pp);
			_texture = TextureLoader.FromFile(device, Application.StartupPath + @"\sprite-animation4.jpg");
			float radius = 2.0f;
			int n = 12;
			vertices = new CustomVertex.PositionTextured[n * n];
			float theta;
			float phi;
			for (int x = 0; x < n; x++)
			{
				theta = x * 180 / n - 90; 
				for (int y = 0; y < n; y++)
				{
					phi = y * 180 / n;
					double dt = theta * Math.PI / 180 * 2;
					double dp = phi * Math.PI / 180 * 2;
					vertices[x + y * n].Position = new Vector3((float)(radius * Math.Cos(dt) * Math.Cos(dp)), (float)(radius * Math.Cos(dt) * Math.Sin(dp)), (float)(radius * Math.Sin(dt)));
					vertices[x + y * n].Tu = (float) x / n;
					vertices[x + y * n].Tv = (float) y / n;
				}
			}
			indices = new short[(n - 1) * (n - 1) * 6];
			for (int x = 0; x < n - 1; x++)
			{
				for (int y = 0; y < n - 1; y++)
				{
					indices[(x + y * (n - 1)) * 6] = (short)(x + y * n);
					indices[(x + y * (n - 1)) * 6 + 1] = (short)((x + 1) + y * n);
					indices[(x + y * (n - 1)) * 6 + 2] = (short)((x + 1) + (y + 1) * n);
					indices[(x + y * (n - 1)) * 6 + 3] = (short)(x + (y + 1) * n);
					indices[(x + y * (n - 1)) * 6 + 4] = (short)(x + y * n);
					indices[(x + y * (n - 1)) * 6 + 5] = (short)((x + 1) + (y + 1) * n);
				}
			}
			meshTerrain = new Mesh((n - 1) * (n - 1) * 2, n * n, MeshFlags.Managed, CustomVertex.PositionTextured.Format, device);
			meshTerrain.SetVertexBufferData(vertices, LockFlags.None);
			meshTerrain.SetIndexBufferData(indices, LockFlags.None);
			int[] adjac = new int[meshTerrain.NumberFaces * 3];
			meshTerrain.GenerateAdjacency(0.05f, adjac);
			meshTerrain.OptimizeInPlace(MeshFlags.OptimizeVertexCache, adjac);
			this.Paint += Form1_Paint;
		}
		private void Form1_Paint(object sender, PaintEventArgs e) {
			device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 1.0f, 7.0f), new Vector3(0.0f, 2.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.Clear(ClearFlags.Target, Color.DarkBlue, 1.0f, 0);
			device.SetTexture(0, _texture);
			device.RenderState.Lighting = false;
			device.RenderState.CullMode = Cull.None;
			Matrix rotateMatrix = Matrix.RotationY(Environment.TickCount / 1000.0f);
			device.BeginScene();
			//device.RenderState.FillMode = FillMode.WireFrame;
			device.SetTransform(TransformType.World, rotateMatrix * Matrix.Translation(new Vector3(0.0f, 2.0f, 0.0f)));
			int numSubSets = meshTerrain.GetAttributeTable().Length;
			//Console.WriteLine(numSubSets);
			for (int i = 0; i < numSubSets; ++i)
			{
				meshTerrain.DrawSubset(i);
			}
			device.EndScene();
			device.Present();
			this.Invalidate();
		}
		[STAThread]
		public static void Main() {
			Application.Run(new Form1());
		}
	}
}





```

以上。