How to dynamically affect alpha of sprite?
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?
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?
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);
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);
is there a way to change arbitrary pixels values within a sprite overtime?
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.
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.
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.
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?
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?
Sounds good. I will look up how to do that.
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.
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.
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.
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
[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
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| OpenGL ES sprite alpha | Holmes | 27 | 12,996 |
Mar 16, 2009 01:06 PM Last Post: Holmes |
|

