Rendering text using OpenTK

Started by
1 comment, last by Plerion 11 years, 5 months ago
In the early stages of working on a game project using Visual Studio 2012 and OpenTK. What I'm working on right now, based on a series of tutorials I found, is a way to render text reliably. The general method I'm using is to create a Bitmap, draw the text to the Bitmap, then use it as a GL texture. The current method seems pretty inefficient (I'm recreating the bitmap every frame, I could probably split it into one smaller Bitmap per string and not have to constantly redraw them), but the problem I'm having is before that. After a few seconds of generating bitmaps, the program crashes.

Code:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
namespace FusionMan {
class TextObject {
public string text;
public PointF pos;
public TextObject(string nText, PointF nPos) {
text = nText;
pos = nPos;
}
}
class Controller {
Bitmap textBmp;
int textTexture;
Font font;
List<TextObject> textObjects;
// Called in OnLoad
public void initialize() {
// Initialize text rendering
// Create Bitmap and OpenGL texture
textBmp = new Bitmap(640, 480); // match window size
PrivateFontCollection fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile("fixed.ttf");
font = new Font(
fontCollection.Families[0],
11,
FontStyle.Regular);
flushText();
}
private void flushText() {
textObjects = new List<TextObject>();
}
public void addText(TextObject textObject) {
textObjects.Add(textObject);
textBmp.Dispose();
textBmp = new Bitmap(640, 480); // match window size
textTexture = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, textTexture);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, textBmp.Width, textBmp.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); // just allocate memory, so we can update efficiently using TexSubImage2D
Graphics gfx = Graphics.FromImage(textBmp);
gfx.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
foreach (TextObject t in textObjects) {
gfx.DrawString(t.text, font, Brushes.White, t.pos);
}
BitmapData data = textBmp.LockBits(new Rectangle(0, 0, textBmp.Width, textBmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, 640, 480, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
textBmp.UnlockBits(data);
}
private void drawText() {
GL.LoadIdentity();
GL.Ortho(0.0, 640.0, 0.0, 480.0, 0.0, 4.0);
GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
GL.Color3(Color.White);
GL.Begin(BeginMode.Quads);
GL.TexCoord2(0f, 1f); GL.Vertex2(0f, 0f);
GL.TexCoord2(1f, 1f); GL.Vertex2(640f, 0f);
GL.TexCoord2(1f, 0f); GL.Vertex2(640f, 480f);
GL.TexCoord2(0f, 0f); GL.Vertex2(0f, 480f);
GL.End();
}
// Called in OnRenderFrame
public void draw() {
flushText();
addText(new TextObject("Test", new PointF(10, 360)));
drawText();
}
}
}


Error message:


System.ArgumentException was unhandled
HResult=-2147024809
Message=Parameter is not valid.
Source=System.Drawing
StackTrace:
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
at FusionMan.Controller.addText(TextObject textObject) in c:\Users\algordon\Documents\Visual Studio 2012\Projects\FusionMan\FusionMan\Controller.cs:line 50
at FusionMan.Controller.draw() in c:\Users\algordon\Documents\Visual Studio 2012\Projects\FusionMan\FusionMan\Controller.cs:line 88
at FusionMan.SimpleWindow.OnRenderFrame(FrameEventArgs e) in c:\Users\algordon\Documents\Visual Studio 2012\Projects\FusionMan\FusionMan\Program.cs:line 61
at OpenTK.GameWindow.OnRenderFrameInternal(FrameEventArgs e)
at OpenTK.GameWindow.RaiseRenderFrame(Stopwatch render_watch, Double& next_render, FrameEventArgs render_args)
at OpenTK.GameWindow.DispatchUpdateAndRenderFrame(Object sender, EventArgs e)
at OpenTK.GameWindow.Run(Double updates_per_second, Double frames_per_second)
at FusionMan.SimpleWindow.Main() in c:\Users\algordon\Documents\Visual Studio 2012\Projects\FusionMan\FusionMan\Program.cs:line 72
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

it says "Parameter is not valid", but the line in question just says

textBmp = new Bitmap(640, 480); // match window size

Any idea what's going on there?
Advertisement
The constructor int, int, PixelFormat throws an exception when the format does not start with Format. On the other hand according to the MSDNA the constructor int, int invokes the other one with Format32bppArgb so it shouldnt throw an ArgumentException.

You might wanna debug the bitmap constructor using the .NET source code so you can ispect where the exception is actually thrown. Here is a link how this can be done:
http://msdn.microsoft.com/en-us/library/cc667410.aspx
[edit: Bad internetconnection -> two times same post]

This topic is closed to new replies.

Advertisement