04-20-2010, 05:33 AM
#1 (permalink )
Divine avenger
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Problem drawing OpenGL string with GL_ONE
Hi there!
I've had a problem with .PNG images that shown a kind of halo (see my last post!) and I've solved by changing this:
Code:
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // PNGs show an halo with this!
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Now that evil halo has gone, but I found that I cannot draw Texture2D objects now, they are drawn like a white square! I use Texture2D to show text in the screen, and this is how it's done:
Code:
// Init Texture2D object with a string
- (id) initWithString:(NSString*)string dimensions:(CGSize)dimensions alignment:(UITextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size
{
NSUInteger width,
height,
i;
CGContextRef context;
void* data;
CGColorSpaceRef colorSpace;
UIFont * font;
font = [UIFont fontWithName:name size:size];
width = dimensions.width;
if((width != 1) && (width & (width - 1))) {
i = 1;
while(i < width)
i *= 2;
width = i;
}
height = dimensions.height;
if((height != 1) && (height & (height - 1))) {
i = 1;
while(i < height)
i *= 2;
height = i;
}
colorSpace = CGColorSpaceCreateDeviceGray();
data = calloc(height, width);
context = CGBitmapContextCreate(data, width, height, 8, width, colorSpace, kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);
CGContextSetGrayFillColor(context, 1.0, 1.0);
CGContextTranslateCTM(context, 0.0, height);
CGContextScaleCTM(context, 1.0, -1.0); //NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential
UIGraphicsPushContext(context);
[string drawInRect:CGRectMake(0, 0, dimensions.width, dimensions.height) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:alignment];
UIGraphicsPopContext();
self = [self initWithData:data pixelFormat:kTexture2DPixelFormat_A8 pixelsWide:width pixelsHigh:height contentSize:dimensions];
CGContextRelease(context);
free(data);
return self;
}
- (id) initWithData:(const void*)data pixelFormat:(Texture2DPixelFormat)pixelFormat pixelsWide:(NSUInteger)width pixelsHigh:(NSUInteger)height contentSize:(CGSize)size
{
GLint saveName;
if((self = [super init])) {
glGenTextures(1, &_name);
glGetIntegerv(GL_TEXTURE_BINDING_2D, &saveName);
glBindTexture(GL_TEXTURE_2D, _name);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
switch(pixelFormat) {
case kTexture2DPixelFormat_RGBA8888:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
break;
case kTexture2DPixelFormat_RGB565:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
break;
case kTexture2DPixelFormat_A8:
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data);
break;
default:
[NSException raise:NSInternalInconsistencyException format:@""];
}
glBindTexture(GL_TEXTURE_2D, saveName);
_size = size;
_width = width;
_height = height;
_format = pixelFormat;
_maxS = size.width / (float)width;
_maxT = size.height / (float)height;
}
return self;
}
And here the drawing method:
Code:
- (void) drawAtPoint:(CGPoint)point
{
GLfloat coordinates[] = { 0, _maxT,
_maxS, _maxT,
0, 0,
_maxS, 0 };
GLfloat width = (GLfloat)_width * _maxS,
height = (GLfloat)_height * _maxT;
GLfloat vertices[] = { -width / 2 + point.x, -height / 2 + point.y, 0.0,
width / 2 + point.x, -height / 2 + point.y, 0.0,
-width / 2 + point.x, height / 2 + point.y, 0.0,
width / 2 + point.x, height / 2 + point.y, 0.0 };
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, _name);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
What can be happening? Why I'm having problems with this if I've never had before since I've changed the glBlendFunc?
Thanks in advance!
04-20-2010, 01:59 PM
#2 (permalink )
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
It doesn't work because GL_ALPHA creates a texture which is a solid block of color plus an alpha channel. It is *not* premultiplied the way your loaded UIImages are. Since the texture is solid white, the source calculation for (white * GL_ONE) will always be white and the entire square will be drawn as white.
I'd suggest switching your glBlendFund back to glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) when you draw the text, and use the GL_ONE function only for your images; either that or rewrite initWithString: so that it premultiplies the color channels to match your images.
__________________
Free Games!
04-21-2010, 03:08 AM
#3 (permalink )
Divine avenger
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Hi Smasher!
I've switched the glBlendFunc as you've said and it works great now, thanks for your help!
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 268
18 members and 250 guests
ADY , Alsahir , dacapo , Dani77 , Desert Diva , djohnson , Duncan C , F_Bryant , Grinarn , HemiMG , jansan , linkmx , M@realobjects , macquitzon216 , prchn4christ , smethorst , spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan