我正在加载一个PNG纹理,我在具有大量alpha透明度的图像上得到了奇怪的文物。
这是源png: http://imgur.com/M8Wj3
这是白色背景上的游戏纹理: http://imgur.com/anliu
以下是我用来加载图片的代码:
NSString* pTextureNameString = [NSString stringWithFormat:@"%s", fileNameWithPath];
UIImage* pImage = [UIImage imageNamed:pTextureNameString];
GLubyte* pImageData = (GLubyte*)malloc(pImage.size.width * pImage.size.height * 4);
CGContextRef imageContext = CGBitmapContextCreate(pImageData, pImage.size.width, pImage.size.height, 8, pImage.size.width * 4, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(imageContext, CGRectMake(0.0, 0.0, pImage.size.width, pImage.size.height), pImage.CGImage);
CGContextRelease(imageContext);
// Build A Texture From The Data
glGenTextures(1, &m_textureID); // generate opengl texture id
glBindTexture(GL_TEXTURE_2D, m_textureID); // Bind Our Texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Mipmap Linear Filtering
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pImage.size.width, pImage.size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pImageData);
pImageData的内存在从UIImage抓取后已经搞砸了,第一个像素正确为红色,但下一个像素应该是透明的(如第3个像素)
red ok bad pixel ok bad pixel etc...
FF0000FF 05F04713 00000000 70EF7D15 302635B0 02000000 00000000
我在构建设置中关闭了png压缩,但它没有任何区别。有人有主意吗? alpha / opengl设置似乎是正确的,因为将此纹理更改为TGA或压缩到PVRTC会产生正确的图像。
答案 0 :(得分:4)
添加以下行可解决问题:
CGContextRef imageContext = CGBitmapContextCreate(pImageData, pImage.size.width, pImage.size.height, 8, pImage.size.width * 4, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);
// Set the correct blending copy mode!
CGContextSetBlendMode(imageContext, kCGBlendModeCopy);
CGContextDrawImage(imageContext, CGRectMake(0.0, 0.0, pImage.size.width, pImage.size.height), pImage.CGImage);