private MeshTexture TextureFromFile(string filename, string directory, string typeName) { string resPath = directory + @"\" + filename; MeshTexture texture = new MeshTexture { Type = typeName, Path = filename }; // load texture file with StbImage var io = new System.IO.FileStream(resPath, System.IO.FileMode.Open); var resLoad = ImageResult.FromStream(io); int width = resLoad.Width, height = resLoad.Height; ColorComponents nrComponents = resLoad.SourceComp; if (resLoad != null) { PixelInternalFormat format = 0; if (nrComponents == ColorComponents.Grey) { format = PixelInternalFormat.CompressedRed; } else if (nrComponents == ColorComponents.RedGreenBlue) { format = PixelInternalFormat.Rgb; } else if (nrComponents == ColorComponents.RedGreenBlueAlpha) { format = PixelInternalFormat.Rgba; } // send necessary actions to dispatcher Dispatcher.ActionsQueue.Enqueue(() => { // load and generate the texture GL.GenTextures(1, out texture.ID); GL.BindTexture(TextureTarget.Texture2D, texture.ID); GL.TexImage2D(TextureTarget.Texture2D, 0, format, width, height, 0, (PixelFormat)format, PixelType.UnsignedByte, resLoad.Data); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); // set the texture wrapping/filtering options (on the currently bound texture object) GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)OpenTK.Graphics.OpenGL4.TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)OpenTK.Graphics.OpenGL4.TextureWrapMode.Repeat); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); }); } else { // TODO: throw new exception, although not necessarily - it's already implemented in ImageResult.FromResult } // explicitly destroy I/O stream object io.Dispose(); return(texture); }
private List <MeshTexture> LoadMaterialTextures(Material mat, TextureType type, string typeName) { var textures = new List <MeshTexture>(); for (int i = 0; i < mat.GetMaterialTextureCount(type); i++) { mat.GetMaterialTexture(type, i, out TextureSlot slot); var texLoaded = TexturesLoaded.Find((t) => { return(t.Path == slot.FilePath); }); if (texLoaded == null) { MeshTexture texture = TextureFromFile(slot.FilePath, Directory, typeName); textures.Add(texture); TexturesLoaded.Add(texture); // add to loaded textures } else { textures.Add(texLoaded); } } return(textures); }