开发者

OpenGL ES: Undo in a Pixel Painting App

开发者 https://www.devze.com 2023-03-11 02:58 出处:网络
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.

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开发者_C百科 pixels?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号