/// <summary> /// Creates a Triangle as specified. Vertices must be CCW /// </summary> /// <param name="vertex1"></param> /// <param name="texel1"></param> /// <param name="normal1"></param> /// <param name="vertex2"></param> /// <param name="texel2"></param> /// <param name="normal2"></param> /// <param name="vertex3"></param> /// <param name="texel3"></param> /// <param name="normal3"></param> /// <param name="material"></param> The texture for this Triangle public Triangle(int vertex1, int texel1, int normal1, int vertex2, int texel2, int normal2, int vertex3, int texel3, int normal3, Material material) { this.vertex1 = vertex1; this.vertex2 = vertex2; this.vertex3 = vertex3; this.texel1 = texel1; this.texel2 = texel2; this.texel3 = texel3; this.normal1 = normal1; this.normal2 = normal2; this.normal3 = normal3; Debug.Assert(normal1 == normal2); Debug.Assert(normal1 == normal3); this.material = material; }
/// <summary> /// Creates a Triangle as specified /// </summary> /// <param name="vertex1">A string in the format "vertex/texture/normal". These index into the vertex, texture, and normal array</param> /// <param name="vertex2">A string in the format "vertex/texture/normal". These index into the vertex, texture, and normal array</param> /// <param name="vertex3">A string in the format "vertex/texture/normal". These index into the vertex, texture, and normal array</param> /// <param name="material">A Material that will be the texture for this Triangle</param> public Triangle(string vertex1, string vertex2, string vertex3, Material material) { // vertex/texture/normal string[] firstVertex = vertex1.Split('/'); string[] secondVertex = vertex2.Split('/'); string[] thirdVertex = vertex3.Split('/'); this.vertex1 = Convert.ToInt32(firstVertex[0]); this.texel1 = Convert.ToInt32(firstVertex[1]); this.normal1 = Convert.ToInt32(firstVertex[2]); this.vertex2 = Convert.ToInt32(secondVertex[0]); this.texel2 = Convert.ToInt32(secondVertex[1]); this.normal2 = Convert.ToInt32(secondVertex[2]); this.vertex3 = Convert.ToInt32(thirdVertex[0]); this.texel3 = Convert.ToInt32(thirdVertex[1]); this.normal3 = Convert.ToInt32(thirdVertex[2]); Debug.Assert(normal1 == normal2); Debug.Assert(normal1 == normal3); this.material = material; }
/// <summary> /// Parses an OBJ file /// </summary> /// <param name="fileName"></param> private void parseObjFile(string fileName) { vertexArray = new List<Vector3f>(); texelArray = new List<Vector2f>(); normalArray = new List<Vector3f>(); triangleArray = new List<Triangle>(); Dictionary<string, Material> materialDict = null; Material currentMaterial = new Material(); FileInfo fileInfo = new FileInfo(fileName); bool validTexture = false; using (StreamReader reader = File.OpenText(fileName)) { string line; while ((line = reader.ReadLine()) != null) { string[] items = line.Split(' '); switch (items[0]) { case "#": continue; case "v": vertexArray.Add(new Vector3f(items[1], items[2], items[3])); break; case "vt": texelArray.Add(new Vector2f(items[1], items[2])); break; case "vn": normalArray.Add(new Vector3f(items[1], items[2], items[3])); break; case "f": Debug.Assert(currentMaterial.id != null); if (validTexture) { triangleArray.Add(new Triangle(items[1], items[2], items[3], currentMaterial)); } break; case "mtllib": materialDict = parseMtlFile(fileInfo.DirectoryName + @"\" + items[1]); break; case "usemtl": Debug.Assert(materialDict != null); Material tempMaterial; validTexture = materialDict.TryGetValue(items[1], out tempMaterial); if (validTexture) { currentMaterial = tempMaterial; } break; } } } }
/// <summary> /// Parses an MTL file /// </summary> /// <param name="filename">The absolute path to the MTL file. For example: @"C:\Users\Mike\Documents\Visual Studio 2008\Projects\SkatePark\SkatePark\Raw Models\DAE to OBJ\Cube\cube.mtl". Hint: Derive the path from the OBJ file.</param> /// <returns>A Dictionary of Material IDs to their Materials</returns> private Dictionary<string, Material> parseMtlFile(string filename) { Dictionary<string, Material> materialDict = new Dictionary<string, Material>(); Material currentMaterial = new Material(); using (StreamReader reader = File.OpenText(filename)) { string line; while ((line = reader.ReadLine()) != null) { string[] items = line.Split(' '); switch (items[0]) { case "#": continue; case "newmtl": if ((currentMaterial.id != null) && (currentMaterial.fileName != null)) { materialDict.Add(currentMaterial.id, currentMaterial); } currentMaterial = new Material(); currentMaterial.id = items[1]; break; case "Ka": currentMaterial.ambient = new Vector3f(items[1], items[2], items[3]); break; case "Kd": currentMaterial.diffuse = new Vector3f(items[1], items[2], items[3]); break; case "Ks": currentMaterial.specular = new Vector3f(items[1], items[2], items[3]); break; case "map_Kd": FileInfo fileInfo = new FileInfo(filename); currentMaterial.fileName = fileInfo.DirectoryName + @"\" + items[1]; break; } } } materialDict.Add(currentMaterial.id, currentMaterial); LoadGLTextures(filename, materialDict); return materialDict; }