vs2010でmanaged directx その8

#概要
c#で、3Dがやりたかった。
meshを作ってみた。


#写真

![image](https://qiita-image-store.s3.amazonaws.com/0/18104/7908f505-455a-40cd-faa3-a13fa0c0338e.png)


#サンプルコード

```
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 WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        private Device device = null;
        private Texture _texture;
        private VertexBuffer _vertexBuffer;
        private CustomVertex.PositionTextured[] vertices;
        private short[] indices;
        private Mesh meshTerrain;

        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            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 + @"\earth2k.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);
        }
        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();
        }
    }
}


```

以上。