public FontRenderer(Font font) { Font = font; charHeight = -Font.Height; GlyphBitmap = new Bitmap(TextureSize.Width, TextureSize.Height); texture = new Texture(GL.GenTexture()); GL.BindTexture(TextureTarget.Texture2D, texture.GetId()); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear); using (Graphics gfx = Graphics.FromImage(GlyphBitmap)) { //this supposedly improves perforance //Application.SetCompatibleTextRenderingDefault(false); StringFormat format = new StringFormat(StringFormat.GenericTypographic); Point charPoint = new Point(0, 0); for (int i = 0; i < chars.Length; i++) { SizeF charSizeF = gfx.MeasureString(new string(new Char[1] { Convert.ToChar(i) }), Font, 0, format); Size charSize = new Size((int)Math.Ceiling(charSizeF.Width), (int)Math.Ceiling(charSizeF.Height)); //fudge factor to prevent glyphs from overlapping charSize.Width += 2; if (charSize.Width + charPoint.X > TextureSize.Width) { charPoint.X = 0; charPoint.Y += (int)Math.Ceiling(Font.GetHeight()); } chars[i] = new CharData(new Rectangle(charPoint, charSize), this); charPoint.X += charSize.Width; } gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; gfx.Clear(Color.Transparent); for (int i = 0; i < chars.Length; i++) { PointF point = new PointF(chars[i].PixelRegion.X, chars[i].PixelRegion.Y); gfx.DrawString(new string(new Char[1] { Convert.ToChar(i) }), Font, new SolidBrush(Color.Black), point); } } BitmapData data = GlyphBitmap.LockBits(new Rectangle(0, 0, GlyphBitmap.Width, GlyphBitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, TextureSize.Width, TextureSize.Height, 0, Format, PixelType.UnsignedByte, data.Scan0); GlyphBitmap.UnlockBits(data); }
private void LoadImage() { try { using (Bitmap file = new Bitmap(Filename)) { LoadImage(file); } } catch (FileNotFoundException) { Debug.Assert(false, "Texture missing."); _texture = _textureMissing; } }
private void LoadImage(Bitmap image) { int texID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, texID); BitmapData data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); image.UnlockBits(data); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); _texture = new Texture(texID); }