Odd effect on loading a .png with alpha channel.

Member
Posts: 110
Joined: 2009.07
Post: #1
I've been working through these excellent openGLES tutorials, and got up to here..

It all works beautifully, and I'd recommend them to anyone just starting out in openGLES.

However...

Sneaky

If you decide that you want to try out adding an alpha channel to the .PNG used to texture the square, the untextured triangle comes out black rather than white.

That's the only change; I have two textures in my project, one with an alpha channel, one without. All I do is load in the one with the alpha channel, and I get a black triangle. Load the one without the channel, and I get a nice white triangle, as per the tutorial.

Is there something weird and sneaky about alpha .pngs I'm not aware of?
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #2
There is a blend mode you need to worry about. Also if you use UIImage to load the png it pre multiplies the alpha so you have to set your color differently. I dont have my code but I think you do something like this
void mycolorFunc(GLfloat r,g,b,a)
{
GLsetColor(a*r,a*g,a*b,a);
}

For your blend I believe its 1, 1 minus src alpha but i dont have my code so somebody can confirm this.
Quote this message in a reply
Member
Posts: 110
Joined: 2009.07
Post: #3
Yeah, I had a feeling the pre-mult alpha might be something to worry about at some point, but the problem I'm having is that the mere act of loading a .png texture with alpha makes makes non-textured objects draw in weird colours.

Here's a code segment:

Code:
    // Square
glPushMatrix();
glColor4f(1.0, 1.0, 1.0, 1.0);  
glTranslatef(1.5, 0.0, -6.0);
glRotatef(rota, 0.0, 0.0, 1.0);
glVertexPointer(3, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
    
glTexCoordPointer(2, GL_SHORT, 0, squareTextureCoords);  
glEnableClientState(GL_TEXTURE_COORD_ARRAY);              
    
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);            
glPopMatrix();
    
// Lines
glPushMatrix();
glVertexPointer(3, GL_FLOAT, 28, lines);    
glColor4f(0.0, 1.0, 0.0, 1.0);  
glDrawArrays(GL_LINES, 0, 4); // or    GL_LINE_STRIP, or GL_LINE_LOOP
glPopMatrix();

Note: I'm not even trying to get the blends to work here. It's just a basic textured square!

The result varies on the texture I use. If I use a non-alpha texture, the lines are drawn green, as expected.

As soon as I load in *any* texture with an alpha component (including Sprite.png from GLSprite), the lines are drawn in the colour of some part of the texture (in GLSprite's case, black).

Quite which part of the texture is responsible for this colour is also a mystery; I've tried it with four different textures, one with a dot of colour at the top-left, another at the top-right and so on. In all cases but the one with the dot in the bottom-left, the lines are drawn pink!

I'm sure I'm missing something obvious, but I certainly can't see it.
Quote this message in a reply
Member
Posts: 269
Joined: 2005.04
Post: #4
You bind a texture when you load it in. That texture stays bound for all future drawing calls, including your lines, until you bind a different one. If you haven't bound any texture coordinates, then it just uses the default (0.0). So it's stretching the first pixel of your texture over your lines/triangles. You'll either need to disable texturing ( glDisable(GL_TEXTURE_2D); ) or unbind the texture ( glBindTexture(GL_TEXTURE_2D, 0); ).

As for drawing alpha-mapped textures on the iPhone, you'll probably want to use this blend mode: glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Quote this message in a reply
Member
Posts: 110
Joined: 2009.07
Post: #5
Bachus - spot on. That was the missing link (though I'm also feeling a bit like an early hominid at the moment).

For some reason, I'd got it into my head that glEnable(GL_TEXTURE_2D); was a once only call - i.e. 'Enable this so I can use it at some point' rather than, 'Enable this and ignore all other colour values'.

As for the blending and depth sorting stuff, I've found that:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

...seems to work pretty well, too.

Anyhow, you've just made my day.

Many thanks.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Water effect markhula 9 8,195 May 10, 2011 12:03 AM
Last Post: markhula
  Creating a water effect iamflimflam1 0 2,462 Aug 4, 2009 06:46 AM
Last Post: iamflimflam1
  How do the hell do the water effect in koi pond? Najdorf 14 7,924 Mar 23, 2009 04:21 PM
Last Post: aBabyRabbit
  Texture2d rainbow effect on flip THRESHE 15 6,578 Mar 19, 2009 08:33 AM
Last Post: AnotherJake