OpenGL ES: Undo in a Pixel Painting App

匆匆过客 提交于 2019-12-02 18:26:41

问题


I'm currently working on an app that allows the user to draw pixelated images using OpenGL ES, but I don't know how to implement an undo function. How could I do it? I thought of using an image for every pixel and adding it to an array. Basically, how can I store the rectangles I use as pixels?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/6281789/opengl-es-undo-in-a-pixel-painting-app

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