1

I tried to overlay text as a texture using following code snippet:

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
myLabel.text = @"Hello world!";
myLabel.font = [UIFont fontWithName:@"Helvetica" size:18];
myLabel.textColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1];   
myLabel.backgroundColor = [UIColor clearColor];

UIGraphicsBeginImageContext(myLabel.bounds.size);

CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, 30);

CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);

[myLabel.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

But it's not visible. For displaying image as a texture using OpenGL I tried the following code snippet:

/// here image is the layerImage created in above piece of code                                      
GLuint width = CGImageGetWidth(image.CGImage);
GLuint height = CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *imageData = malloc( height * width * 4 );
CGContextRef context = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
CGColorSpaceRelease( colorSpace );
CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );
CGContextTranslateCTM( context, 0, height - height );
CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage );

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

CGContextRelease(context);

free(imageData);
[image release];
[texData release];

But the image is not getting displayed. Is there any other approach to display text as a texture using OpenGL in ios?

combinatorial
  • 9,132
  • 4
  • 40
  • 58
Sonal0706
  • 290
  • 3
  • 15
  • In the first snippet, why are you translating and scaling the context? That doesn't look necessary to me. – combinatorial Nov 15 '13 at 17:33
  • In the second snippet, this looks odd 'CGContextTranslateCTM( context, 0, height - height );' height-height is 0 – combinatorial Nov 15 '13 at 17:34
  • Is there a current OpenGL ES context when you call `glTexImage2D`? Have you generated and bound an OpenGL ES texture object? Is the OpenGL ES code you're (presumably) using to render using that texture otherwise working? – rickster Nov 15 '13 at 22:34
  • Does width and length that you pass to glTexImage2D are Power-Of-Two ? If not, round them up to the next POT, or see this thread for rendering to non POT texture : http://stackoverflow.com/questions/4760174/rendering-to-non-power-of-two-texture-on-iphone/4761453#4761453 – Emmanuel Nov 17 '13 at 12:28

0 Answers0