/// <summary> /// Starts a new mesh and fills it with mesh mapped positions. /// </summary> /// <param name="name">Name of mesh.</param> private void StartGroup(string name) { curGroup = new OBJGroup(); if (!String.IsNullOrEmpty(name)) curGroup.name = name; // Obj files need their winding orders swapped //meshBuilder.SwapWindingOrder = true; // Add additional vertex channels for texture coordinates and normals /*textureCoordinateDataIndex = meshBuilder.CreateVertexChannel<Vector2>( VertexChannelNames.TextureCoordinate(0)); normalDataIndex = meshBuilder.CreateVertexChannel<Vector3>(VertexChannelNames.Normal()); // Add each position to this mesh with CreatePosition positionMap = new int[positions.Count]; for (int i = 0; i < positions.Count; i++) { // positionsMap redirects from the original positions in the order // they were read from file to indices returned from CreatePosition positionMap[i] = meshBuilder.CreatePosition(positions[i]); }*/ }
/// <summary> /// Finishes building a mesh and adds the resulting MeshContent or /// NodeContent to the root model's NodeContent. /// </summary> private void FinishGroup() { /*MeshContent meshContent = meshBuilder.FinishMesh(); // Groups without any geometry are just for transform if (meshContent.Geometry.Count > 0) { // Add the mesh to the model rootNode.Children.Add(meshContent); } else { // Convert to a general NodeContent NodeContent nodeContent = new NodeContent(); nodeContent.Name = meshContent.Name; // Add the transform-only node to the model rootNode.Children.Add(nodeContent); } meshBuilder = null;*/ groups.Add(curGroup); curGroup = null; }
void fillPosTex(ref OBJGroup group) { List<CustomVertex.PositionTextured> verts = new List<CustomVertex.PositionTextured>(); List<int> indicies = new List<int>(); foreach (OBJFace f in group.faces) { int numVerts = f.vertIndices.Count; for (int i = 0; i < numVerts; i++) { CustomVertex.PositionTextured vert = new CustomVertex.PositionTextured(positions[f.vertIndices[i]], texCoords[f.texCoords[i] - 1].X, texCoords[f.texCoords[i] - 1].Y); verts.Add(vert); indicies.Add(indicies.Count); } } group.numVerts = verts.Count; group.numTris = indicies.Count / 3; group.vertexFormat = CustomVertex.PositionTextured.Format; group.vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), verts.Count, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default); group.vertexBuffer.SetData(verts.ToArray(), 0, LockFlags.None); group.indexBuffer = new IndexBuffer(typeof(int), indicies.Count * sizeof(int), device, Usage.WriteOnly, Pool.Default); group.indexBuffer.SetData(indicies.ToArray(), 0, LockFlags.None); }
void fillPosNormTex(ref OBJGroup group) { List<CustomVertex.PositionNormalTextured> verts = new List<CustomVertex.PositionNormalTextured>(); List<int> indicies = new List<int>(); foreach (OBJFace f in group.faces) { int numVerts = f.vertIndices.Count; for (int i = 0; i < numVerts; i++) { bool foundMatch = false; for (int j = 0; j < verts.Count; j++) { if (verts[j].Position == positions[f.vertIndices[i]] && verts[j].Normal == normals[f.vertNormals[i]] && verts[j].Tu == texCoords[f.texCoords[i]].X && verts[j].Tv == texCoords[f.texCoords[i]].Y) { indicies.Add(j); foundMatch = true; break; } } if (foundMatch) continue; CustomVertex.PositionNormalTextured vert = new CustomVertex.PositionNormalTextured(positions[f.vertIndices[i]], normals[f.vertNormals[i]], texCoords[f.texCoords[i]].X, texCoords[f.texCoords[i]].Y); verts.Add(vert); indicies.Add(verts.Count-1); } } group.numVerts = verts.Count; group.numTris = indicies.Count / 3; group.vertexFormat = CustomVertex.PositionNormalTextured.Format; group.vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), verts.Count, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Default); group.vertexBuffer.SetData(verts.ToArray(), 0, LockFlags.None); group.indexBuffer = new IndexBuffer(typeof(int), indicies.Count * sizeof(int), device, Usage.WriteOnly, Pool.Default); group.indexBuffer.SetData(indicies.ToArray(), 0, LockFlags.None); }
void fillPosNorm(ref OBJGroup group) { List<CustomVertex.PositionNormal> verts = new List<CustomVertex.PositionNormal>(); List<int> indicies = new List<int>(); foreach (OBJFace f in group.faces) { int numVerts = f.vertIndices.Count; for (int i = 0; i < numVerts; i++) { bool foundMatch = false; for (int j = 0; j < verts.Count; j++) { // Look for identical verts if (verts[j].Position == positions[f.vertIndices[i]] && verts[j].Normal == normals[f.vertNormals[i]]) { indicies.Add(j); foundMatch = true; break; } } if (foundMatch) continue; CustomVertex.PositionNormal vert = new CustomVertex.PositionNormal(positions[f.vertIndices[i]], normals[f.vertNormals[i]]); verts.Add(vert); indicies.Add(verts.Count - 1); } } group.numVerts = verts.Count; group.numTris = indicies.Count / 3; group.vertexFormat = CustomVertex.PositionNormal.Format; group.vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormal), verts.Count, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionNormal.Format, Pool.Default); group.vertexBuffer.SetData(verts.ToArray(), 0, LockFlags.None); group.indexBuffer = new IndexBuffer(typeof(int), indicies.Count * sizeof(int), device, Usage.WriteOnly, Pool.Default); group.indexBuffer.SetData(indicies.ToArray(), 0, LockFlags.None); /*using (StreamWriter fs = new StreamWriter("output.txt",false)) { for(int k=0; k<verts.Count; k++) { fs.WriteLine("V"+k+" Pos: ("+ verts[k].Position.X+ ", "+verts[k].Position.Y+ ", "+verts[k].Position.Z+ ") Norm: ("+ verts[k].Normal.X+ ", "+verts[k].Normal.Y+ ", "+verts[k].Normal.Z+")"); } }*/ /*using (StreamWriter fs = new StreamWriter("indiciesOut.txt", false)) { for (int k = 0; k < indicies.Count; k++) { fs.WriteLine(indicies[k]); } }*/ }
void fillPos(ref OBJGroup group) { List<CustomVertex.PositionOnly> verts = new List<CustomVertex.PositionOnly>(); List<int> indicies = new List<int>(); foreach (OBJFace f in group.faces) { int numVerts = f.vertIndices.Count; for (int i = numVerts - 1; i > -1; i--) { CustomVertex.PositionOnly vert = new CustomVertex.PositionOnly(positions[f.vertIndices[i]]); verts.Add(vert); indicies.Add(indicies.Count); } } group.numVerts = verts.Count; group.numTris = indicies.Count / 3; group.vertexFormat = CustomVertex.PositionOnly.Format; group.vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionOnly), verts.Count, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionOnly.Format, Pool.Default); group.vertexBuffer.SetData(verts.ToArray(), 0, LockFlags.None); group.indexBuffer = new IndexBuffer(typeof(int), indicies.Count * sizeof(int), device, Usage.WriteOnly, Pool.Default); group.indexBuffer.SetData(indicies.ToArray(), 0, LockFlags.None); }