One problem I can see from the code is that you do not clear your context before drawing the image. Since your image contains transparent areas and is composed on the background, you just see what's in the memory allocated by malloc. Try setting you Quartz Blend mode to copy before drawing the image.
Up vote 3 down vote favorite 2 share g+ share fb share tw.
I am beginning to work on my first OpenGL iPhone app, but I've hit an early snag. I have a VERY SIMPLE little texture that I want to use as a sprite in a 2D game, but it renders with weird 'randomly' colored pixels up top.
If it's not photoshop then it's gotta be my code... So here is the code in question... - (void)loadTexture { CGImageRef textureImage = UIImage imageNamed:@"zombie0.Png". CGImage; if (textureImage == nil) { NSLog(@"Failed to load texture image"); return; } NSInteger texWidth = CGImageGetWidth(textureImage); NSInteger texHeight = CGImageGetHeight(textureImage); GLubyte *textureData = (GLubyte *)malloc(texWidth * texHeight * 4); CGContextRef textureContext = CGBitmapContextCreate(textureData, texWidth, texHeight, 8, texWidth * 4, CGImageGetColorSpace(textureImage), kCGImageAlphaPremultipliedLast); CGContextDrawImage(textureContext, CGRectMake(0.0, 0.0, (float)texWidth, (float)texHeight), textureImage); CGContextRelease(textureContext); glGenTextures(1, &textures0); glBindTexture(GL_TEXTURE_2D, textures0); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData); free(textureData); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); } This blend function yielded the best results. Please, let me know what you think is wrong.
Thank you very much, this problem has been driving me nuts. Iphone opengl-es quartz-graphics textures link|improve this question edited Jun 18 '10 at 20:57genpfault11.4k4918 asked Jul 3 '09 at 15:37Super Toaster Fiend.
One problem I can see from the code is that you do not clear your context before drawing the image. Since your image contains transparent areas and is composed on the background, you just see what's in the memory allocated by malloc. Try setting you Quartz Blend mode to copy before drawing the image: CGContextSetBlendMode(textureContext, kCGBlendModeCopy); You could also use calloc instead of malloc, since calloc gives you zeroed memory.
Your OpenGL blending is correct: glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); gives you Porter-Duff "OVER", which is what you usually want.
YOU ARE AWESOME! This answer totally makes me happy and makes sense to me! I had gotten the feeling that those artifacts were some sort of left over memory but I had not thought it through.
Thanks again sooooooo much! You rule! – Super Toaster Fiend Jul 3 '09 at 15:57 turning red ... glad I could help.
– Nikolai Ruhe Jul 3 '09 at 16:08 THANKS also! I couldn't figure out why I had all these artifacts, now it looks great. – Mk12 Sep 2 '10 at 16:57.
Try erasing your CGContextRef first: CGContextSetRGBFillColor(ctxt, 1, 1, 1, 0); CGContextFillRect(ctxt, CGRectMake(0, 0, w, h)).
Most probably your image has some colored pixels with an zero alpha value, but because of the blending function you are showing them. Try glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA).
This blend func gives good results only for non-premultiplied alpha, which is seldom in Quartz. – Nikolai Ruhe Jul 3 '09 at 16:07.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.