/// <summary> /// Load bmp image /// </summary> /// <param name="path">image path</param> /// <returns>Image information per pixel</returns> public static MBitmap LoadImage(string path) { MBitmap bitmap = new MBitmap(); Stream imageStream = File.OpenRead(path); BinaryReader reader = new BinaryReader(imageStream); bitmap.header.bfType = reader.ReadUInt16(); bitmap.header.bfSize = reader.ReadUInt32(); bitmap.header.bfReserved1 = reader.ReadUInt16(); bitmap.header.bfReserved2 = reader.ReadUInt16(); bitmap.header.bfOffBytes = reader.ReadUInt32(); bitmap.inforHeader.biSize = reader.ReadUInt32(); bitmap.inforHeader.biWidth = reader.ReadUInt32(); bitmap.inforHeader.biHeight = reader.ReadUInt32(); bitmap.inforHeader.biPlanes = reader.ReadUInt16(); bitmap.inforHeader.biBitCount = reader.ReadUInt16(); bitmap.inforHeader.biCompression = reader.ReadUInt32(); bitmap.inforHeader.biSizeImage = reader.ReadUInt32(); bitmap.inforHeader.biXPelsPerMeter = reader.ReadUInt32(); bitmap.inforHeader.biYPelsPerMeter = reader.ReadUInt32(); bitmap.inforHeader.biClrUsed = reader.ReadUInt32(); bitmap.inforHeader.biClrImportant = reader.ReadUInt32(); byte[] temp = reader.ReadBytes((int)reader.BaseStream.Length); bitmap.byteBuffer = new byte[temp.Length]; for (int i = 2; i < temp.Length; i += 3) { bitmap.byteBuffer[i - 2] = temp[i]; bitmap.byteBuffer[i - 1] = temp[i - 1]; bitmap.byteBuffer[i] = temp[i - 2]; } return(bitmap); }
/// <summary> /// Load texture image /// </summary> /// <param name="path">image path</param> /// <returns>texture Id</returns> private int LoadTexture(string path) { int[] textureId = { 0 }; MBitmap bitm = MImageUtil.LoadImage(path); byte[] pixels = bitm.byteBuffer; GL.PixelStore(PixelStoreParameter.UnpackAlignment, 4); GL.GenTextures(1, textureId); GL.BindTexture(TextureTarget.Texture2D, textureId[0]); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)All.LinearMipmapLinear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)All.Linear); GL.TexImage2D(TextureTarget2d.Texture2D, 0, TextureComponentCount.Rgb, (int)bitm.inforHeader.biWidth, (int)bitm.inforHeader.biHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, pixels); GL.GenerateMipmap(TextureTarget.Texture2D); return(textureId[0]); }