private bool SectionVertex(string[] tokens) { if (tokens[0] == "}") { return(false); } UVertex v = new UVertex(); v.position = ParsePoint3(tokens, 0); current.vertices.Add(v); return(true); }
public ushort Add(UVertex v) { ushort n; if (map.TryGetValue(v, out n)) { return(n); } n = (ushort)ary.Count; map.Add(v, n); ary.Add(v); return(n); }
/// <summary> /// 頂点をDirect3Dバッファに書き込みます。 /// </summary> /// <param name="device">device</param> public void WriteBuffer(Device device) { Vector3[] normals = new Vector3[vertices.Count]; foreach (MqoFace face in faces) { Vector3 v1 = vertices[face.a].position - vertices[face.c].position; Vector3 v2 = vertices[face.b].position - vertices[face.c].position; Vector3 n = Vector3.Normalize(Vector3.Cross(v2, v1)); normals[face.a] += n; normals[face.b] += n; normals[face.c] += n; } { int i = 0; foreach (UVertex v in vertices) { v.normal = Vector3.Normalize(normals[i]); i++; } } UVertexHeap heap = new UVertexHeap(); List <ushort> indices = new List <ushort>(faces.Count * 3); { int face_len = 0; MqoAttributeRange ar = at.Start(face_len, faces[0].mtl); foreach (MqoFace face in faces) { if (face.mtl != ar.mtl) { face_len += indices.Count / 3; ar = at.Next(face_len, face.mtl); } UVertex a = new UVertex(vertices[face.a].position, vertices[face.a].normal, face.ta.X, 1 - face.ta.Y, face.mtl); UVertex b = new UVertex(vertices[face.b].position, vertices[face.b].normal, face.tb.X, 1 - face.tb.Y, face.mtl); UVertex c = new UVertex(vertices[face.c].position, vertices[face.c].normal, face.tc.X, 1 - face.tc.Y, face.mtl); indices.Add(heap.Add(a)); indices.Add(heap.Add(c)); indices.Add(heap.Add(b)); } { face_len += indices.Count / 3; at.Finish(face_len); ar = null; } } if (dm != null) { dm.Dispose(); dm = null; } dm = new Mesh(at.FaceCount, heap.Count, MeshFlags.Managed | MeshFlags.WriteOnly, ve, device); // // rewrite vertex buffer // { GraphicsStream gs = dm.LockVertexBuffer(LockFlags.None); { foreach (UVertex v in heap.ary) { gs.Write(v.position); gs.Write(1.0f); gs.Write(0.0f); gs.Write(0.0f); gs.Write(0.0f); gs.Write(0); gs.Write(v.normal); gs.Write(v.u); gs.Write(v.v); } } dm.UnlockVertexBuffer(); } // // rewrite index buffer // { GraphicsStream gs = dm.LockIndexBuffer(LockFlags.None); { foreach (ushort idx in indices) { gs.Write(idx); } } dm.UnlockIndexBuffer(); } // // rewrite attribute buffer // { int[] attribBuffer = dm.LockAttributeBufferArray(LockFlags.None); foreach (MqoAttributeRange ar in at.Ranges) { for (int i = 0; i < ar.FaceCount; i++) { attribBuffer[ar.FaceStart + i] = ar.AttributeId; } } dm.UnlockAttributeBuffer(attribBuffer); dm.SetAttributeTable(at.GenerateAttributeTable(0, heap.Count)); } }
public ushort this[UVertex index] { get { return(map[index]); } }