• Advertisement
Sign in to follow this  

Rendering text iPhone

This topic is 1989 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


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.


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;

[[label layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();

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

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);

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);


return texName;

Share this post

Link to post
Share on other sites
The problem was, that the frame size has to be power of two.
Please mark this topic "Resolved".

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement