Rendering text iPhone

Started by
0 comments, last by Vorel512 11 years, 8 months ago
Hi,

I would like to ask for a little help. I am stuck with displaying texts with OpenGL ES 2.0. I have read a lot of tutorial, but none of them working.
If anybody knows where can I find some working example please link it here.

Thanx,
RP

My existing, tho not working code:
[source lang="cpp"]- (GLuint)setupTextTexture:(NSString *)text
{
CGPoint position = CGPointMake(0,0);

UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = [text sizeWithFont:font forWidth:12 lineBreakMode:UILineBreakModeClip];
CGRect frame;
frame.origin = position;
frame.size = size;
UILabel *label = [[UILabel alloc]initWithFrame:frame];
label.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
label.text = text;
label.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
label.font = font;
label.alpha = 1;

UIGraphicsBeginImageContext(label.frame.size);
[[label layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGImageRef spriteImage = layerImage.CGImage;
if (!spriteImage) {
NSLog(@"Failed to setup text image %@", text);
exit(1);
}

size_t width = CGImageGetWidth(spriteImage);
size_t height = CGImageGetHeight(spriteImage);

GLubyte * spriteData = (GLubyte *) calloc(width*height*4, sizeof(GLubyte));

CGContextRef spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width*height*4,
CGColorSpaceCreateDeviceGray(), kCGImageAlphaNone);

CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);
CGContextRelease(spriteContext);

GLuint texName;
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);

free(spriteData);

return texName;
}[/source]
Advertisement
The problem was, that the frame size has to be power of two.
Please mark this topic "Resolved".

This topic is closed to new replies.

Advertisement