// Adds the specified caharacter to the texture packer.
        private void LoadGlyph(char c, out RectangleF rectangle)
        {
            if (pack == null)
            {
                PrepareTexturePacker();
            }

            RectangleF glyph_rect = MeasureText(c.ToString(), SizeF.Empty, load_glyph_string_format);
            SizeF      glyph_size = new SizeF(glyph_rect.Right, glyph_rect.Bottom); // We need to do this, since the origin might not be (0, 0)
            Glyph      g          = new Glyph(c, font, glyph_size);
            Rectangle  rect;

            try
            {
                pack.Add(g, out rect);
            }
            catch (InvalidOperationException expt)
            {
                // TODO: The TexturePacker is full, create a new font sheet.
                Trace.WriteLine(expt);
                throw;
            }

            GL.BindTexture(TextureTarget.Texture2D, texture);

            gfx.Clear(System.Drawing.Color.Transparent);
            gfx.DrawString(g.Character.ToString(), g.Font, System.Drawing.Brushes.White, 0.0f, 0.0f, default_string_format);

            BitmapData bitmap_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly,
                                                  System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            GL.PushClientAttrib(ClientAttribMask.ClientPixelStoreBit);
            try
            {
                GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1.0f);
                GL.PixelStore(PixelStoreParameter.UnpackRowLength, bmp.Width);
                GL.TexSubImage2D(TextureTarget.Texture2D, 0, (int)rect.Left, (int)rect.Top,
                                 rect.Width, rect.Height,
                                 OpenTK.Graphics.PixelFormat.Rgba,
                                 PixelType.UnsignedByte, bitmap_data.Scan0);
            }
            finally
            {
                GL.PopClientAttrib();
            }

            bmp.UnlockBits(bitmap_data);

            rectangle = RectangleF.FromLTRB(
                rect.Left / (float)texture_width,
                rect.Top / (float)texture_height,
                rect.Right / (float)texture_width,
                rect.Bottom / (float)texture_height);

            loaded_glyphs.Add(g.Character, rectangle);
        }
Esempio n. 2
0
        public void WriteRegion(Rectangle source, Rectangle target, int mipLevel, Bitmap bitmap)
        {
            if (bitmap == null)
            {
                throw new ArgumentNullException("data");
            }

            GraphicsUnit unit = GraphicsUnit.Pixel;

            if (!bitmap.GetBounds(ref unit).Contains(source))
            {
                throw new InvalidOperationException("The source Rectangle is larger than the Bitmap.");
            }

            if (mipLevel < 0)
            {
                throw new ArgumentOutOfRangeException("mipLevel");
            }

            Bind();

            BitmapData data = null;

            GL.PushClientAttrib(ClientAttribMask.ClientPixelStoreBit);
            try
            {
                data = bitmap.LockBits(source, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
                GL.PixelStore(PixelStoreParameter.UnpackRowLength, bitmap.Width);
                GL.TexSubImage2D(TextureTarget.Texture2D, mipLevel,
                                 target.Left, target.Top,
                                 target.Width, target.Height,
                                 OpenTK.Graphics.PixelFormat.Bgra,
                                 PixelType.UnsignedByte, data.Scan0);
            }
            finally
            {
                GL.PopClientAttrib();
                if (data != null)
                {
                    bitmap.UnlockBits(data);
                }
            }
        }