private void SetDefault() { ambient = new List <float>(); diffuse = new List <float>(); specular = new List <float>(); alpha = 1.0f; shininess = 0.0f; texture = null; illumType = Material.IllumType.FLAT; }
public Dictionary <string, Material> LoadFile(String fileName) { map = new Dictionary <string, Material>(); FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); int lastS = fileName.LastIndexOf('/'); string filePath = fileName.Substring(0, lastS + 1); try { StreamReader reader = new StreamReader(fileStream); SetDefault(); Regex regex = new Regex(@"[\s]+"); while (!reader.EndOfStream) { string line = reader.ReadLine().Trim(); if (line.ToLower().StartsWith("newmtl")) { string[] splittedGroupLine = regex.Split(line); if (latestGroup != null) { AddMaterial(); } SetDefault(); latestGroup = splittedGroupLine[1]; while (!reader.EndOfStream) { line = reader.ReadLine().Trim(); if (line.ToLower().StartsWith("ka ")) { string[] splitted = regex.Split(line); for (int i = 1; i < splitted.Length; i++) { ambient.Add(Convert.ToSingle(splitted[i], numformat)); } } if (line.ToLower().StartsWith("ks ")) { string[] splitted = regex.Split(line); for (int i = 1; i < splitted.Length; i++) { specular.Add(ToFloat(splitted[i])); } } if (line.ToLower().StartsWith("kd ")) { string[] splitted = regex.Split(line); for (int i = 1; i < splitted.Length; i++) { diffuse.Add(ToFloat(splitted[i])); } } if (line.ToLower().StartsWith("d ") || line.StartsWith("Tr ")) { string[] splitted = regex.Split(line); alpha = ToFloat(splitted[1]); } if (line.ToLower().StartsWith("ns ")) { string[] splitted = regex.Split(line); shininess = ToFloat(splitted[1]); } if (line.ToLower().StartsWith("illum ")) { string[] splitted = regex.Split(line); int num = ToInt(splitted[1]); if (num == 1) { illumType = Material.IllumType.FLAT; } else if (num == 2) { illumType = Material.IllumType.SPECULAR; } } if ((line.ToLower().StartsWith("map_kd") || line.ToLower().StartsWith("map_ks"))) { string[] splitted = regex.Split(line); if (splitted.Length > 1) { texture = new BasicTexture(new Pixmap(filePath + splitted[1])); } } } } } AddMaterial(); } finally { fileStream.Close(); } return(map); }
private void SetDefault() { ambient = new List<float>(); diffuse = new List<float>(); specular = new List<float>(); alpha = 1.0f; shininess = 0.0f; texture = null; illumType = Material.IllumType.FLAT; }
public Dictionary<string, Material> LoadFile(String fileName) { map = new Dictionary<string, Material>(); FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); int lastS = fileName.LastIndexOf('/'); string filePath = fileName.Substring(0, lastS+1); try { StreamReader reader = new StreamReader(fileStream); SetDefault(); Regex regex = new Regex(@"[\s]+"); while (!reader.EndOfStream) { string line = reader.ReadLine().Trim(); if (line.ToLower().StartsWith("newmtl")) { string[] splittedGroupLine = regex.Split(line); if (latestGroup != null) AddMaterial(); SetDefault(); latestGroup = splittedGroupLine[1]; while (!reader.EndOfStream) { line = reader.ReadLine().Trim(); if (line.ToLower().StartsWith("ka ")) { string[] splitted = regex.Split(line); for (int i = 1; i < splitted.Length; i++) ambient.Add(Convert.ToSingle(splitted[i], numformat)); } if (line.ToLower().StartsWith("ks ")) { string[] splitted = regex.Split(line); for (int i = 1; i < splitted.Length; i++) specular.Add(ToFloat(splitted[i])); } if (line.ToLower().StartsWith("kd ")) { string[] splitted = regex.Split(line); for (int i = 1; i < splitted.Length; i++) diffuse.Add(ToFloat(splitted[i])); } if (line.ToLower().StartsWith("d ") || line.StartsWith("Tr ")) { string[] splitted = regex.Split(line); alpha = ToFloat(splitted[1]); } if (line.ToLower().StartsWith("ns ")) { string[] splitted = regex.Split(line); shininess = ToFloat(splitted[1]); } if (line.ToLower().StartsWith("illum ")) { string[] splitted = regex.Split(line); int num = ToInt(splitted[1]); if (num == 1) illumType = Material.IllumType.FLAT; else if (num == 2) illumType = Material.IllumType.SPECULAR; } if ((line.ToLower().StartsWith("map_kd") || line.ToLower().StartsWith("map_ks"))) { string[] splitted = regex.Split(line); if(splitted.Length>1) texture = new BasicTexture(new Pixmap(filePath + splitted[1])); } } } } AddMaterial(); } finally { fileStream.Close(); } return map; }
private void GetMaterial(StreamReader reader, string name, Dictionary <string, Material> dict, string fileName, Dictionary <string, Texture> textureList) { Material material = new Material(); float[] ambient = new float[4]; float[] diffuse = new float[4]; float[] specular = new float[4]; float alpha = ambient[3] = diffuse[3] = specular[3] = 1.0f; float shininess = 0.2f; Material.IllumType illumType = Material.IllumType.SPECULAR; Texture texture = null; while (!reader.EndOfStream) { string line = reader.ReadLine().Trim(); string[] words = regex.Split(line); string firstWord = words[0].ToLower(); if (firstWord.Equals("newmtl")) { GetMaterial(reader, words[1].Trim(), dict, fileName, textureList); } else if (firstWord.Equals("ka")) { for (int i = 0; i < 3; i++) { ambient[i] = (ToFloat(words[i + 1])); } } else if (firstWord.Equals("kd")) { for (int i = 0; i < 3; i++) { diffuse[i] = (ToFloat(words[i + 1])); } } else if (firstWord.Equals("ks")) { for (int i = 0; i < 3; i++) { specular[i] = (ToFloat(words[i + 1])); } } else if (firstWord.Equals("d") || firstWord.Equals("tr")) { alpha = ambient[3] = diffuse[3] = specular[3] = ToFloat(words[1]); } else if (firstWord.Equals("ns")) { shininess = ToFloat(words[1]); } else if (firstWord.Equals("illum")) { if (words[1].Equals("1")) { illumType = Material.IllumType.FLAT; } else if (words[1].Equals("2")) { illumType = Material.IllumType.SPECULAR; } } else if (firstWord.Equals("map_kd") || firstWord.Equals("map_ks")) { int lastS = fileName.LastIndexOf('/'); string filePath = fileName.Substring(0, lastS + 1); if (words.Length > 1) { if (!textureList.ContainsKey(words[1])) { texture = new BasicTexture(new Pixmap(filePath + words[1])); //texture = Texture.CreateFromFile(filePath + words[1]); textureList.Add(words[1], texture); } else { texture = textureList[words[1]]; } } } } material.SetAmbient(ambient); material.SetDiffuse(diffuse); material.SetSpecular(specular); material.SetAlpha(alpha); material.SetShininess(shininess); material.SetIllumType(illumType); material.SetTexture(texture); dict.Add(name, material); }