Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <summary>
        /// Load texture image file
        /// </summary>
        /// <param name="imageName">image path</param>
        /// <returns>texture Id</returns>
        public static int CreateTexture2D(string imageName)
        {
            int[]   textureId = { 0 };
            MBitmap bitm      = MImageUtil.LoadImage(imageName);

            byte[] pixels = bitm.byteBuffer;
            GL.PixelStore(PixelStoreParameter.UnpackAlignment, 4);
            // Generate a texture object
            GL.GenTextures(1, textureId);
            // Bind the texture object
            GL.BindTexture(TextureTarget.Texture2D, textureId[0]);
            // Load the texture
            GL.TexImage2D(TextureTarget2d.Texture2D, 0, TextureComponentCount.Rgb, (int)bitm.inforHeader.biWidth, (int)bitm.inforHeader.biHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, pixels);

            // Set the filtering mode
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)All.Repeat);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)All.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)All.LinearMipmapLinear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)All.Linear);
            GL.GenerateMipmap(TextureTarget.Texture2D);
            return(textureId[0]);
        }