I am trying to render text to screen using Open GL.
All tutorials I found were for iOS, and I am developing for OS X, and I don't want to artificially include UI* just for this purpose, unless someone gives me convincing arguments.
This is the method I am using, as it is the only one I managed to make work:
NSColor *background = [NSColor colorWithDeviceRed:0.0f green:0.0f blue:0.0f alpha:0.0f];
NSColor *textColor = [NSColor colorWithDeviceRed:1.0f green:1.0f blue:1.0f alpha:1.0f];
stringAttributes = [NSMutableDictionary dictionary];
[stringAttributes setObject:[NSFont fontWithName:@"trench" size:64] forKey:NSFontAttributeName];
[stringAttributes setObject:textColor forKey:NSForegroundColorAttributeName];
[stringAttributes setObject:background forKey:NSBackgroundColorAttributeName];
NSRect myTextRect = [string boundingRectWithSize:NSMakeSize(1000.0f, 0.0f) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingOneShot | NSStringDrawingTruncatesLastVisibleLine) attributes:stringAttributes];
myTextRect.origin.x = 0.0f;
myTextRect.origin.y = 0.0f;
width = ceil(myTextRect.size.width);
height = ceil(myTextRect.size.height);
textureBuffer = malloc(sizeof(int unsigned)*(height*width));
memset(textureBuffer, 0, 4*height*width);
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:(char unsigned **)&textureBuffer pixelsWide:width pixelsHigh:height bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bitmapFormat:0 bytesPerRow:4*width bitsPerPixel:32];
NSGraphicsContext *ctx = [NSGraphicsContext graphicsContextWithBitmapImageRep:bitmap];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:ctx];
[string drawWithRect:myTextRect options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingOneShot | NSStringDrawingTruncatesLastVisibleLine) attributes:stringAttributes];
[NSGraphicsContext restoreGraphicsState];
The result is a back buffer with the text I want.
Then I need to regenerate the textures when the user resizes the window. I have this simple reshape method:
- (void)reshape {
[super reshape];
float w = [self frame].size.width;
float h = [self frame].size.height;
glViewport(0, 0, w, h);
for (id o in guiObjects) {
if ([o isKindOfClass:[STAString class]]) { // Yes I am going to make a protocol eventually
[o regenerate:NSMakeSize(w, h)];
}
}
}
This reshape method does not account for scaling of the font yet, so whatever the size of the window, the text stays the same size.
The scaling is done by deleting the texture, and making a new one on the fly.
The problem is... As I told, the text stays the same since I am not scaling the text with window size. Yet, the text shows minor antialiasing artefacts that seems to have two patterns that changes with different zones of vertical sizes. With zones I mean there is three states the font renders to: one where the first 'o' looks a bit odd, then one with the 'W' looking a bit odd, then back to the first sitation again. The middle zone seems to be around 50-10 pixels as I increase the height of the window.
Here are pictures to show:
It looks ugly. Anyone having any help, or tips how to make text look better?
Please also grab the opportunity to pick on my coding practise so I can get better. I am always looking to perfect my skills.
EDIT: Forgot tags... Corrected title...