Take the 2-minute tour ×

I'm using Apple's sample application GLPaint as a basis for an OpenGL ES painting application, but I can't figure out how to implement undo functionality within it.

I don't want to take images of every stroke and store them. Is there any way of using different frame buffer objects to implement undo? Do you have other suggestions for better ways of doing this?

share|improve this question
add comment

4 Answers

To undo in a graphical application, you can use coreData.

here is a detailed blogpost and read this one as well.

share|improve this answer
add comment

Either you can use NSUndoManager, class provided by iOS

Or you can save current state of screen area by:

CGContextRef current = UIGraphicsGetCurrentContext();

You can have one array as stack with screen image objects and on undo action you can pop value from stack and on each change push value into the stack.

share|improve this answer
The asker is looking for a solution involving OpenGL ES, and graphics states as you describe only work for Core Graphics. They would not apply here. –  Brad Larson Feb 15 '12 at 22:56
add comment

I would recommend using NSUndoManager to store a list of the actual drawing actions undertaken by the user (draw line from here to here using this paintbrush, etc.). If stored as a list of x, y coordinates for vector drawing, along with all other metadata required to recreate that part of the drawing, you won't be using anywhere near as much memory as storing images, vertex buffer objects, or framebuffer objects.

In fact, if you store these drawing steps in a Core Data database, you can almost get undo / redo for free. See my answer here for more.

share|improve this answer
Saving all the steps and then redrawing them -last step (undo) can get very slow when you have many steps to redraw, am i missing something? –  Yogev Shelly Jul 25 '12 at 18:07
I would suggest storing a raw image every 10 or so check points so you don't have to redraw the last 100 steps or something crazy –  PsychoDad Oct 14 '12 at 22:13
add comment

Use vertex buffer objects (VBO) to render your content. On every new stroke copy the last VBO to some least recently used (LRU) list. If your LRU is full, delete the least recently used VBO. To restore (undo) the last stroke just use the most recently used VBO of the LRU and render it.

VBO: http://developer.apple.com/iphone/library/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/TechniquesforWorkingwithVertexData/TechniquesforWorkingwithVertexData.html

LRU: http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used

share|improve this answer
Could you please provide some sample code on how to implement this? –  antalkerekes Feb 9 '12 at 19:14
Shake to undo :-) Does this work for you? github.com/larsxschneider/Undo-in-OpenGL-ES –  Lars Schneider Feb 15 '12 at 23:09
Works perfectly, thank you so much! I will be in touch to show you what I did with it. Bounty well deserved. –  antalkerekes Feb 16 '12 at 3:40
@antalkerekes I am interested and I am looking forward to it :-) –  Lars Schneider Feb 16 '12 at 10:02
Unfortunately this doesn't work at all. Comparing with the original code of GLPaint, it really didn't undo all in once. But the undo is not working as expected. It doesn't undo the last action and the color changed wrong after undo. –  bagusflyer Jul 19 '12 at 0:57
show 1 more comment

Your Answer


