Transparent png texture in cocos2d using opengl iphone
I am creating a polygon filled with texture using opengl in cocos2d framework. I have used PRKit(link) and modified the draw method with the code shown below.
glBlendFunc(GL_ONE, GL_ZERO);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glBindTexture(GL_TEXTURE_2D, self.texture.name);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glVertexPointer(2, GL_FLOAT, 0, polyTriangulatedPoints);
glTexCoordPointer(2, GL_FLOAT, 0, textureCoordinates);
glDrawArrays(GL_TRIANGLES, 0, trianglePointCount);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
When I use transparent png as texture I get a bit weird result. The transparent area is not black, but some random colors with black. Here is the output I get with the above code.
I had to keep glBlendFunc(GL_ONE, GL_ZERO) which is same as disabling blending. If I don't do so, my background layer in cocos2d gets added with my texture image resulting in a very bright looking texture. To add more information, I have set texture mode so that all images are loaded with the following.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
My question is that why do I see this kind of random colors in transparent area and what are the ways to solve this? I have a feeling that I must be doing something wrong since I am new to opengl.
If you have a texture with full opaque / full transparent pixel (alpha = 1.0 or 0.0 only), you can keep GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA to keep the transparency.
Else you can use alpha test with GL_GREATER, 0.0 (or discard keyword in fragment shader if you use opengl es 2.0).
But on ios, alpha test is less performant than alpha blending.
链接地址: http://www.djcxy.com/p/34002.html上一篇: 透明纹理阻挡对象后面