public Material loadMaterial(string materialFileName) { StreamReader sourceFile = null; try { sourceFile = new StreamReader(materialFileName); } catch (Exception streamException) { Logger.LogError(Logger.ErrorState.Limited, "Failed to load material from file " + materialFileName + " Error:" + streamException.Message); return(null); } Logger.LogInfoLinePart("Loading material from file ", ConsoleColor.Gray); Logger.LogInfoLinePart(materialFileName, ConsoleColor.Cyan); Logger.LogInfoLineEnd(); string line = null; string materialName = null; Material newMaterial = null; char[] space = { ' ' }; // First check if we already have this material do { line = sourceFile.ReadLine(); if (line == null) { break; } else if (line.Contains("newmtl")) { materialName = line.Split(space)[1]; break; } }while (line != null); if (doesMaterialExist(materialName)) { sourceFile.Close(); return(GetMaterialByName(materialName)); } else { newMaterial = new Material(materialName); } // Different texture maps can have different options, change this struct // as we go LoadOptions options = new LoadOptions(false, false, false); // Read rest of the file // use . as separator instead of system default System.Globalization.NumberFormatInfo nfi = new System.Globalization.CultureInfo("en-US", false).NumberFormat; bool isCubemap = false; List <string> cubemapNames = null; do { line = sourceFile.ReadLine(); if (line == null) { break; } if (line.Contains("#")) { // comment } else if (line.Contains("cubemap")) { // This is a cubemap isCubemap = true; cubemapNames = new List <string>(6); for (int i = 0; i < 6; i++) { cubemapNames.Add(""); } } else if (line.Contains("mipmaps")) { // Mipmap option // mipmaps: true string optionValue = line.Split(space)[1]; options.mipmaps = (optionValue == "true"); } else if (line.Contains("repeat")) { // Repeat option // repeat: true string optionValue = line.Split(space)[1]; options.repeat = (optionValue == "true"); } else if (line.Contains("interpolate")) { // Interpolation option // interpolate: true string optionValue = line.Split(space)[1]; options.interpolation = (optionValue == "true"); } if (isCubemap) { // Look for cubemap map identifiers if (line.Contains("map_top")) { cubemapNames[posY] = line.Split(space)[1]; } else if (line.Contains("map_bottom")) { cubemapNames[negY] = line.Split(space)[1]; } else if (line.Contains("map_left")) { cubemapNames[negX] = line.Split(space)[1]; } else if (line.Contains("map_right")) { cubemapNames[posX] = line.Split(space)[1]; } else if (line.Contains("map_front")) { cubemapNames[posZ] = line.Split(space)[1]; } else if (line.Contains("map_back")) { cubemapNames[negZ] = line.Split(space)[1]; } } else { if (line.Contains("map_Kd")) { // Diffuse map // map_Kd filename.png string textureName = line.Split(space)[1]; int textureGLIndex = loadTexture(textureName, options); TextureMap diffuse = new TextureMap(textureName, textureGLIndex); newMaterial.textureMaps.Add(ShaderUniformName.DiffuseMap, diffuse); Logger.LogInfoLinePart(" Diffuse map :", ConsoleColor.Gray); Logger.LogInfoLinePart(textureName, ConsoleColor.Cyan); Logger.LogInfoLineEnd(); } else if (line.Contains("map_Ki")) { // Illumination map // map_Ki filename_i.png string textureName = line.Split(space)[1]; int textureGLIndex = loadTexture(textureName, options); TextureMap illumination = new TextureMap(textureName, textureGLIndex); newMaterial.textureMaps.Add(ShaderUniformName.IlluminationMap, illumination); Logger.LogInfoLinePart(" Illumination map :", ConsoleColor.Gray); Logger.LogInfoLinePart(textureName, ConsoleColor.Cyan); Logger.LogInfoLineEnd(); } else if (line.Contains("map_Kn")) { // Normal map // map_Kn filename_n.png string textureName = line.Split(space)[1]; int textureGLIndex = loadTexture(textureName, options); TextureMap normal = new TextureMap(textureName, textureGLIndex); newMaterial.textureMaps.Add(ShaderUniformName.NormalMap, normal); Logger.LogInfoLinePart(" Normal map :", ConsoleColor.Gray); Logger.LogInfoLinePart(textureName, ConsoleColor.Cyan); Logger.LogInfoLineEnd(); } else if (line.Contains("map_Kr")) { // Roughness map // map_Kr filename_r.png string textureName = line.Split(space)[1]; int textureGLIndex = loadTexture(textureName, options); TextureMap roughness = new TextureMap(textureName, textureGLIndex); newMaterial.textureMaps.Add(ShaderUniformName.RoughnessMap, roughness); Logger.LogInfoLinePart(" Roughness map :", ConsoleColor.Gray); Logger.LogInfoLinePart(textureName, ConsoleColor.Cyan); Logger.LogInfoLineEnd(); } else if (line.Contains("map_Km")) { // Metallic map // map_Km filename_m.png string textureName = line.Split(space)[1]; int textureGLIndex = loadTexture(textureName, options); TextureMap metallic = new TextureMap(textureName, textureGLIndex); newMaterial.textureMaps.Add(ShaderUniformName.MetallicMap, metallic); Logger.LogInfoLinePart(" Metallic map :", ConsoleColor.Gray); Logger.LogInfoLinePart(textureName, ConsoleColor.Cyan); Logger.LogInfoLineEnd(); } else if (line.Contains("map_Kh")) { // Height map // map_Kh filename_h.png string textureName = line.Split(space)[1]; int textureGLIndex = loadTexture(textureName, options); TextureMap height = new TextureMap(textureName, textureGLIndex); newMaterial.textureMaps.Add(ShaderUniformName.HeightMap, height); Logger.LogInfoLinePart(" Height map :", ConsoleColor.Gray); Logger.LogInfoLinePart(textureName, ConsoleColor.Cyan); Logger.LogInfoLineEnd(); } } } while (line != null); if (isCubemap) { int cubeIndex = LoadCubeMap(cubemapNames, options); TextureMap cube = new TextureMap(newMaterial.materialName, cubeIndex); newMaterial.textureMaps.Add(ShaderUniformName.CubeMap, cube); Logger.LogInfoLinePart(" Cube map :", ConsoleColor.Gray); for (int i = 0; i < 6; i++) { Logger.LogInfoLinePart(cubemapNames[i], ConsoleColor.Cyan); Logger.LogInfoLinePart(", ", ConsoleColor.Gray); } Logger.LogInfoLineEnd(); } sourceFile.Close(); materials.Add(newMaterial); return(newMaterial); }
public void SetTextureUniform(ShaderProgram shaderProgram, int location, ShaderUniformName uniform, TextureMap map) { int textureUnit = GetTextureUnitForMap(uniform); if (uniform == ShaderUniformName.DiffuseMap) { GL.ActiveTexture(TextureUnit.Texture0); } else if (uniform == ShaderUniformName.IlluminationMap) { GL.ActiveTexture(TextureUnit.Texture1); } else if (uniform == ShaderUniformName.NormalMap) { GL.ActiveTexture(TextureUnit.Texture2); } else if (uniform == ShaderUniformName.RoughnessMap) { GL.ActiveTexture(TextureUnit.Texture3); } else if (uniform == ShaderUniformName.MetallicMap) { GL.ActiveTexture(TextureUnit.Texture4); } else if (uniform == ShaderUniformName.HeightMap) { GL.ActiveTexture(TextureUnit.Texture5); } else if (uniform == ShaderUniformName.CubeMap) { GL.ActiveTexture(TextureUnit.Texture6); } if (textureUnit == -1) { Logger.LogError(Logger.ErrorState.Limited, "No defined texture unit for uniform " + ShaderUniformManager.GetSingleton().GetUniformName(uniform) + ", cannot bind"); } if (uniform == ShaderUniformName.CubeMap) { GL.BindTexture(TextureTarget.TextureCubeMap, map.textureGLIndex); } else { GL.BindTexture(TextureTarget.Texture2D, map.textureGLIndex); } shaderProgram.SetSamplerUniform(location, textureUnit); }