views:

239

answers:

3

Hi Everyone,

I am trying to figure out how to implement a simple "undo" of last drawing action on the iPhone screen. I draw by first preparing the frame buffer:

[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

I then prepare the vertex array and draw this way:

glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);
glDrawArrays(GL_POINTS, 0, vertexCount);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

How do I simple undo this last action? There has to be a way to save previous state or an built-in OpenGL ES function, I would think.

Thanks

A: 

I think I figured it out, I just have to redraw up to the last step to a temporary buffer and present that to the screen. Is this the most efficient way?

Erika
A: 

You can grab an image from OpenGL ES context everytime you draw something and save in application's bundle as image file. This saves application's run memory.

When undo is pressed you just draw previous saved image into the context and that's it.

How to grab image from context you can find here: http://www.iphonedevsdk.com/forum/iphone-sdk-development/35281-grab-image-opengl-es-context.html

plug-in
A: 

Late answer I know, but incase anyone else comes upon this, I'll post this anyway.

You also have the option of storing the points in an array at every touchesBegan and touchesMoved call. As in here:

[currentStroke addObject:[NSValue valueWithCGPoint:point]];

And when touchesEnded, you can move this to another mutable array, such as:

[allPoints addObject:allCurrentStroke];

Then, you can iterate through allPoints array, passing each subarray to the rendering function. This method has advantages and disadvantages over the method of storing images. First, it saves on hard drive space... however at a cost of memory. Using GL_POINTS, as you are, you will notice it will take time to redraw your image after you hit undo... however you can undo all the way to the first touch easily! So, it depends if you want speed or flexibility... If anyone has a better method to undo, please let me know!

Ginamin