问题
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
clear old
buffer
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT);
remove point from
array;
for (int i = 0; i < 50; ++i) { [self.vertexBuffers removeLastObject]; }
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); }
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