来自PNG的具有透明度的openGL ES纹理正在呈现出奇怪的神器并让我疯狂! [英] openGL ES textures from PNGs with transparency are being rendered with weird artifacts and driving me nuts!

查看:187
本文介绍了来自PNG的具有透明度的openGL ES纹理正在呈现出奇怪的神器并让我疯狂!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始研究我的第一个OpenGL iPhone应用程序了,但我已经遇到了一个早期障碍。

I am beginning to work on my first OpenGL iPhone app, but I've hit an early snag.

我有一个非常简单的小纹理我想要在2D游戏中用作精灵,但它会在顶部呈现奇怪的随机颜色像素。

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.

http://i40.tinypic.com/2s7c9ro.png < - 此处的截图

http://i40.tinypic.com/2s7c9ro.png <-- Screenshot here

我有点觉得这是Photoshop的错,所以如果有人对此有所了解请告诉我。

I sort of get the feeling that this is Photoshop's fault, so if anybody something about that please let me know.

如果它不是photoshop那么它必须是我的代码..所以这里是有问题的代码...

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, &textures[0]);

glBindTexture(GL_TEXTURE_2D, textures[0]);

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.

推荐答案

我从代码中看到的一个问题是你在绘图之前没有清除你的背景图片。由于您的图像包含透明区域并且在背景上组成,因此您只需看到malloc分配的内存中的内容。尝试在绘制图像之前将Quartz Blend模式设置为copy:

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);

您也可以使用calloc而不是malloc,因为calloc会为您提供归零内存。

You could also use calloc instead of malloc, since calloc gives you zeroed memory.

您的OpenGL混合是正确的:

Your OpenGL blending is correct:

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

给你Porter-DuffOVER,这是你通常想要的。

gives you Porter-Duff "OVER", which is what you usually want.

这篇关于来自PNG的具有透明度的openGL ES纹理正在呈现出奇怪的神器并让我疯狂!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆