public static void LoadMTL(string path, Material parent) { Console.WriteLine("Loading MTL:" + path); var parentCopy = parent; var material = parentCopy; if (!File.Exists(path)) { throw new FileNotFoundException("Unable to open \"" + path + "\", does not exist."); } using (StreamReader streamReader = new StreamReader(path)) { while (!streamReader.EndOfStream) { string[] words = streamReader.ReadLine().Split(' '); switch (words[0]) { case "newmtl": parentCopy = parent; material = parentCopy; matList[words[1]] = material; break; case "Ke": var max = Math.Max(Math.Max(float.Parse(words[1]), float.Parse(words[2])), float.Parse(words[3])); if (max > 0) { material.Color = new Color(float.Parse(words[1]) / max, float.Parse(words[2]) / max, float.Parse(words[3]) / max); material.Emittance = max; } break; case "Kd": material.Color = new Color(float.Parse(words[1]), float.Parse(words[2]), float.Parse(words[3])); break; case "map_Kd": Console.WriteLine("map_Kd: " + Directory.GetCurrentDirectory() + "\\" + words[1]); var kdmap = Directory.GetCurrentDirectory() + "\\" + words[1]; material.Texture = ColorTexture.GetTexture(kdmap); break; case "map_bump": Console.WriteLine("map_bump: " + Directory.GetCurrentDirectory() + "\\" + words[3]); var bumpmap = Directory.GetCurrentDirectory() + "\\" + words[3]; material.NormalTexture = ColorTexture.GetTexture(bumpmap).Pow(1 / 2.2); break; default: break; } } } }
internal static ITexture GetTexture(string path) { if (textures.ContainsKey(path)) { Console.WriteLine("Texture: " + path + " ... OK"); return(textures[path]); } else { Console.WriteLine("Adding texture to list..."); ITexture img = new ColorTexture().LoadTexture(path); textures.Add(path, img); return(img); } }