OpenGL ES: Undo in a Pixel Painting App

2 Solutions Collect From Internet About “OpenGL ES: Undo in a Pixel Painting App”

you can try :

    NSData *data = [NSData dataWithBytes:vertexBuffer length:vertexCount * sizeof(GL_FLOAT) * 2] ;
if (self.vertexBuffers == nil) self.vertexBuffers = [[NSMutableArray alloc] init];
[self.vertexBuffers addObject:data];

save every draw point to a array;

if undo 
  1. clear old buffer

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    
  2. remove point from array;

    for (int i = 0; i < 50; ++i)
    {
    [self.vertexBuffers removeLastObject];
    }
    
  3. render

    for (NSData *point in self.vertexBuffers)
    {
        NSUInteger count = point.length / (sizeof(GL_FLOAT) * 2);
        glVertexPointer(2, GL_FLOAT, 0, point.bytes);
        glDrawArrays(GL_POINTS, 0, count);
    }
    
  4. display buffer

     glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
     [context presentRenderbuffer:GL_RENDERBUFFER_OES];
    

other solution:
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.

See OpenGL ES Simple Undo Last Drawing

how can I store the rectangles I use as pixels?

I’m not sure you’ve got the basic setup right. You should be using a big texture acting as the canvas. Any user painting operations should affect only this texture (which you will be updating with glTexSubImage2D). Then on every frame you should redraw this texture on the screen.

A simple N-steps undo system would consist on a circular list of N textures / canvases.