I\'m trying to figure out a simple to draw some text in OpenGL. My research has shown its a fairly complex task. It involves creating (or generating at runtime) a font atl
One way to do this is by setting up a UILabel and then rendering its layer into a texture. An indirect route to this would be to first set up the UILabel with text, font, etc. and then use the following code
UIGraphicsBeginImageContext(yourLabel.frame.size);
[yourLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
to capture the UILabel to a UIImage. You could then use the initWithImage: constructor of the Texture2D class in the CrashLanding project to transform this to a texture.
It looks like initWithImage: in that example re-renders the UIImage to a bitmap context and uses that to create the texture. With a little work, you could extract the relevant code from that method and alter the above code to render your layer directly into the texture.
This way, you get the nice Unicode and font support of UILabel when creating the text for your texture.
Thanks for that method. It saved me some time.
There were a couple things I had to change though. The glBlendFunc call needed to be:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_ALPHA)
And secondly, the text appeared to be drawing into a single point. The CGRectMake call is creating a 1x1 rectangle, unless I'm reading that incorrectly.
Use the "musings" font library:
http://www.themusingsofalostprogrammer.com/2010/01/how-to-do-font-rendering-in-opengl-on.html
Font font("Verdena");
font.print("Hello World!", x, y, z);
Easy :)
It comes with unicode support, loads glyphs "on-demand", and even has a way to output variables
font.print(x, y, z) << "fps: " << fps;
I found a simple solution to this. Here's a quick function I wrote for it. (You will need the Texture2D class.)
- (void) drawText:(NSString*)theString AtX:(float)X Y:(float)Y {
// Use black
glColor4f(0, 0, 0, 1.0);
// Set up texture
Texture2D* statusTexture = [[Texture2D alloc] initWithString:theString dimensions:CGSizeMake(150, 150) alignment:UITextAlignmentLeft fontName:@"Helvetica" fontSize:11];
// Bind texture
glBindTexture(GL_TEXTURE_2D, [statusTexture name]);
// Enable modes needed for drawing
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
// Draw
[statusTexture drawInRect:CGRectMake(X,Y-1,1,1)];
// Disable modes so they don't interfere with other parts of the program
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
}
I believe the input coordinates need to be in your world coordinates, but I'm not sure if that's true in general or if it just works that way for me. You might need to play with the offsets to get it right.
I've created a Font class similar to the "musings" font library, minus the memory leaks and minus the texture creation for every character, every frame. It still uses a different glTexture for each character though. Note: Texture2D has been changed to comment out its texture deletion.
init in constructor:
_font = new Font("Helvetica", 40);
use in redraw() method:
_font->draw("Hello, World!", 50, 100, ALIGN_LEFT);
GitHub link:
https://github.com/chandl34/public/tree/master/personal/c%2B%2B/Font
Check the crash landing demo. The Texture2D class that is provided allows for creation of textures with text and a font. (Hopefully there will be a better answer I would love a simpler way of drawing to an opengles view)