How to dynamically affect alpha of sprite?

Apprentice
Posts: 16
Joined: 2008.10
Post: #1
Can someone point me to a tutorial that shows how to dynamically affect the alpha of a sprite when displaying to screen?

I have alpha channels in my PNG files for transparency but also want to be able to fade textures in and out using RGBA values.

In XNA I do this by setting the color in RGBA format and adjust the A value over time. Is there a similar way to do that in Open GL ES for iPhone SDK?
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #2
Call glColor4f(R, G, B, A); just before the primitive(s) you want faded out. RGBA values are clamped to be between 0 and 1.

Also need to make sure you call glEnable(GL_BLEND); before you draw any translucent primitives.

You might also need to set your blend function to something like:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

or

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Quote this message in a reply
Member
Posts: 21
Joined: 2008.10
Post: #3
is there a way to change arbitrary pixels values within a sprite overtime?
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #4
You could use glTexSubImage2D to replace the part or pixel of the sprite you want to change, I suppose, but it's not very efficient. Maybe we could suggest some other method if you give us more details on what exactly you are trying to achieve.
Quote this message in a reply
Member
Posts: 21
Joined: 2008.10
Post: #5
I'm just playing around with a drawing app similar to GLPaint.

GLPaint preserves the drawing buffer so it can keep adding to it over time. I occasionally want to read color info from the buffer, but glReadPixels doesn't appear to report back the latest screen info.

In one of my various ideas for fixing this, I was wondering if it would be smarter to have a separate canvas object that I could read and write to directly, and then update it to the screen instead.
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #6
Hmm... Tough one.

I can say that calling glReadPixels should be avoided if at all possible because it causes a round trip to and from the hardware which can be very expensive in terms of performance.

Maybe it might make sense to draw into your own buffer and then glTexSubImage2D that after every new splat of paint?
Quote this message in a reply
Member
Posts: 21
Joined: 2008.10
Post: #7
Sounds good. I will look up how to do that.
Quote this message in a reply
Member
Posts: 21
Joined: 2008.10
Post: #8
Alright, so I think I have it mostly figured out.

I have a chunk of memory devoted to the pixel data

pixelData = (GLubyte *) malloc(width * height * 4);

and I can create a texture from that

glGenTextures(1, &canvasTexture);
glBindTexture(GL_TEXTURE_2D, canvasTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);


From there, I can update texture when I change the pixel data

pixelData[ place to change ] = desired color;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);


Does that look right? Everything works fine, but I'm concerned that I'm creating some memory leak by not deleting the old texture.
Quote this message in a reply
Member
Posts: 87
Joined: 2006.08
Post: #9
glTexImage2D will replace the previous storage for the texture. However, TexSubImage might provide better performance. Instead of replacing storage, it should just modify the contents.
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #10
That looks like it should work. I'd use glTexSubImage2D instead though, so that you don't have to re-upload the entire image if you're just splatting paint on one portion.

[Edit] nevermind, Frogblast beat me to it...

[Edit2] actually, I wasn't thinking in terms of how much it'd cost to replace storage. That makes it even more attractive to use glTexSubImage2D
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL ES sprite alpha Holmes 27 15,503 Mar 16, 2009 01:06 PM
Last Post: Holmes