10

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?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Bazooka
  • 101
  • 1
  • 3

4 Answers4

10

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

Lars Schneider
  • 5,530
  • 4
  • 33
  • 58
6

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.

Community
  • 1
  • 1
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • 1
    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 – jjxtra Oct 14 '12 at 22:13
1

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

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

moxy
  • 1,634
  • 1
  • 11
  • 19
-1

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.

Mrunal
  • 13,982
  • 6
  • 52
  • 96
  • 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