How to do this FadeOut Effect with openGL on iPhone?
Hello,
I'm playing around with the GLPaint Example from Apple. GLPaint
But I don't know how to create an effect which fades the already drawn stuff out. I created an example in Flash which shows the effect I'm looking for: Flash Example
In Flash I'm drawing a texture on a BitmapData and in every frame I'm adding a ColorTransform to the BitmapData which fades out the old drawn data.
I guess there must be a similar solution in openGL. Something with the renderBuffer or frameBuffer but I didn't find any solution.
When you draw a texture in OpenGL, you can specify an alpha value for each vertex.
To accomplish what you're doing in that demo, I would maintain a list of ~20 "history state" nodes for the moving object. Each history state would consist of:
The location of the object
The lifetime of the object
The current alpha value of the object
In my app's update function, I would iterate these nodes, updating each node's current alpha and removing any expired nodes, based -- of course -- on how much time has elapsed since the last update.
In my app's draw function, I would again iterate the node list (drawing from oldest to newest), drawing the object at the location specified in the node, applying each node's current alpha value to the vertices being drawn.
When you draw a texture in OpenGL, you can specify an alpha value for each vertex.
To accomplish what you're doing in that demo, I would maintain a list of ~20 "history state" nodes for the moving object. Each history state would consist of:
The location of the object
The lifetime of the object
The current alpha value of the object
In my app's update function, I would iterate these nodes, updating each node's current alpha and removing any expired nodes, based -- of course -- on how much time has elapsed since the last update.
In my app's draw function, I would again iterate the node list (drawing from oldest to newest), drawing the object at the location specified in the node, applying each node's current alpha value to the vertices being drawn.
just remember to set your blend mode appropriately.
That would definately be a solution for my problem. But isnt there a way to just draw the current position of my object and keep the old already drawn positions in the buffer and fade it out in every frame a bit more? That means not clearing the buffer in every frame.
This would be the approach people use in ActionScript 3, which I like because you don't need to store the old positions of an object and you dont need to draw them in every step..
But actually I don't know if this concept is realizable in OpenGL.
That would definately be a solution for my problem. But isnt there a way to just draw the current position of my object and keep the old already drawn positions in the buffer and fade it out in every frame a bit more? That means not clearing the buffer in every frame.
This would be the approach people use in ActionScript 3, which I like because you don't need to store the old positions of an object and you dont need to draw them in every step..
But actually I don't know if this concept is realizable in OpenGL.
Cheers,
Raphael
in actionscript, you are changing the state of an object. the rendering engine for flash takes care of the actual rendering. since you don't have the flash renderer doing the work for you, you're writing your own rendering engine. if you don't tell the video chip to draw what you need drawn, in the next frame it will be gone.
getting unrealisticly technical about it, if you don't have other things to draw on the screen, you could just draw an overlay the color of your background with a low alpha value over the top of the previous frame instead of clearing the screen between frames and then draw the "head" of the trail you're leaving behind.
edit: i just thought of some other options which also fit the unrealistically technical category:
-a pixel shader post processing effect which darkens everything in the current context previous to the new element(s) being drawn
-running a filter over the entire image which does the same as the pixel shader mentioned above, but i'm not really sure if opengl es supports the same image processing effects as opengl...so maybe on the iphone it's not really a possibility
Last edited by wahness; 07-29-2010 at 06:35 PM.
Reason: more possibilities
getting unrealisticly technical about it, if you don't have other things to draw on the screen, you could just draw an overlay the color of your background with a low alpha value over the top of the previous frame instead of clearing the screen between frames and then draw the "head" of the trail you're leaving behind.
-a pixel shader post processing effect which darkens everything in the current context previous to the new element(s) being drawn
So I guess the solution of saving old states and drawing them in every frame is too much processing overhead. Its more like keeping everything I've already drawn and like you said, applying a shade prior to draw new stuff or draw a color over all with low alpha.
My problem is now: How to write that in OpenGL ES? Do you have a code snippet or something like that I could experiment with?
So I guess the solution of saving old states and drawing them in every frame is too much processing overhead. Its more like keeping everything I've already drawn and like you said, applying a shade prior to draw new stuff or draw a color over all with low alpha.
My problem is now: How to write that in OpenGL ES? Do you have a code snippet or something like that I could experiment with?
Thank you,
Raphael
try apple's opengl sample project to get you started. also look at jeff la marche's blog or do another search for opengl es help. this is gonna take a lot of learning and practicing.
I hope this is a good solution. If you pro guys know a better way to achieve this effect, please let me know.
For all others I hope this might help people looking for a similar solution.