public static unsafe DemoImage LoadImage(string filePathName, bool flipVertical) { var imageClass = UIImage.FromFile(filePathName); var cgImage = imageClass.CGImage; if(cgImage == null) { return null; } var image = new DemoImage(); image.Width = cgImage.Width; image.Height = cgImage.Height; image.RowByteSize = image.Width * 4; image.Data = new byte[cgImage.Height * image.RowByteSize]; image.Format = PixelInternalFormat.Rgba; image.Type = PixelType.UnsignedByte; fixed (byte *ptr = &image.Data [0]){ using(var context = new CGBitmapContext((IntPtr) ptr, image.Width, image.Height, 8, image.RowByteSize, cgImage.ColorSpace, CGImageAlphaInfo.NoneSkipLast)) { context.SetBlendMode(CGBlendMode.Copy); if(flipVertical) { context.TranslateCTM(0.0f, (float)image.Height); context.ScaleCTM(1.0f, -1.0f); } context.DrawImage(new RectangleF(0f, 0f, image.Width, image.Height), cgImage); } } return image; }
int BuildTexture(DemoImage image) { int texName = 0; // Create a texture object to apply to model GL.GenTextures(1, out texName); GL.BindTexture(TextureTarget.Texture2D, texName); // Set up filter and wrap modes for this texture object GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.LinearMipmapLinear); // Indicate that pixel rows are tightly packed // (defaults to stride of 4 which is kind of only good for // RGBA or FLOAT data types) GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1); // Allocate and load image data into texture GL.TexImage2D<byte>(TextureTarget.Texture2D, 0, image.Format, image.Width, image.Height, 0, (PixelFormat)Enum.ToObject(typeof(PixelFormat), (int)image.Format), image.Type, image.Data); // Create mipmaps for this texture for better image quality GL.GenerateMipmap(TextureTarget.Texture2D); GetGLError(); return texName; }