OpenGL ES: Undo in a Pixel Painting App

ⅰ亾dé卋堺 提交于 2019-12-02 11:32:11
charse

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!