Hi all,
I have a code snippet, and it seems not working, if anyone run into the same problem and solved, please post the know how.
The error is, the image results in a black plain. Other images I use are work nice, just this text rendering not...
[source lang="cpp"]NSString *aString = [NSString stringWithFormat:@"Hello World."];
CGPoint position = CGPointMake(10,10);
// Make a UILabel with the string
UIFont *font = [UIFont fontWithName:@"Georgia" size:50];
CGSize size = [aString sizeWithFont:font forWidth:20 lineBreakMode:UILineBreakModeClip];
CGRect frame;
frame.origin = position;
frame.size = size;
UILabel *label = [[UILabel alloc]initWithFrame:frame];
label.backgroundColor = [UIColor clearColor];
label.text = aString;
label.textColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
label.font = font;
// Make a UIImage with the UILabel
UIGraphicsBeginImageContext(label.frame.size);
[[label layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Get a textureInfo from the UILabel
texture = [GLKTextureLoader textureWithCGImage:layerImage.CGImage options:options error:&error];
if (texture == nil)
{
NSLog(@"Texture Error:%@", error);
}[/source]
Create textures from text OpenGL ES 2.0 / GLKit
I've made a new function just for text rendering, but the picture is black anyway. I've tried to change the color values in the label, but still black output.
[source lang="cpp"]- (GLuint)setupTextTexture:(NSString *)text
{
//0
CGPoint position = CGPointMake(0,0);
// Make a UILabel with the string
UIFont *font = [UIFont fontWithName:@"Georgia" 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;
// Make a UIImage with the UILabel
UIGraphicsBeginImageContext(label.frame.size);
[[label layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 1
CGImageRef spriteImage = layerImage.CGImage;
if (!spriteImage) {
NSLog(@"Failed to setup text image %@", text);
exit(1);
}
// 2
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*4,
CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
// 3
CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);
CGContextRelease(spriteContext);
// 4
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]
[source lang="cpp"]- (GLuint)setupTextTexture:(NSString *)text
{
//0
CGPoint position = CGPointMake(0,0);
// Make a UILabel with the string
UIFont *font = [UIFont fontWithName:@"Georgia" 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;
// Make a UIImage with the UILabel
UIGraphicsBeginImageContext(label.frame.size);
[[label layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *layerImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 1
CGImageRef spriteImage = layerImage.CGImage;
if (!spriteImage) {
NSLog(@"Failed to setup text image %@", text);
exit(1);
}
// 2
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*4,
CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);
// 3
CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);
CGContextRelease(spriteContext);
// 4
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement