I am developing an android app which distort the image.I am using opengl translate and scale function to distort the image.The image is distorting fine.Now I want to implement undo feature.But unable to implement the functionality.Any one can help how to implement undo functionality in opengl .Any suggestion or code will be helpful. Thanks in advance .
-
1This has nothing to do with OpenGL. OpenGL just draws what you tell it to draw. Any undo functionality is about changing what you tell it to draw to be what it used to be. – Nicol Bolas Feb 14 '13 at 11:28
-
@NicolBolas actually I am distorting image on finger touch. The image shrink or bulge . The question is, what should i store and how to reflect it on the view . – Nikhil Chaurasiya Feb 14 '13 at 13:02
-
I believe you should store current image on each touch start. Then when undo is done, simply drop current image and replace it with last image in queue. Some operations could be inverted using opengl, but I don't think you should use such features here. Just store original and each operation you did with finger, or image for each step. – Pihhan Feb 14 '13 at 13:27
-
2take a look at the command design pattern with undo: http://stackoverflow.com/questions/3448943/best-design-pattern-for-undo-feature – Ray Tayek Feb 14 '13 at 21:54
-
In case you're using a triangle strip, I would store the current matrix, with the command design pattern described above by Ray Tayek. – Daniel Monteiro Mar 27 '13 at 19:48
2 Answers
I suspect that the command pattern is not enough for your situation. This is based on my assumption that is is not easy to reverse the image distortion even when you know what distortion was applied.
Instead, you will probably need to push the image onto the stack just before applying the distortion. Then, the undo implementation will replace the current image with the image on the stack. This will lead to potentially interesting issues resulting from the combination of redo and undo.
You can optimize this be storing command objects for changes that are easily reversable and storing images for changes that are not easily reversable.

- 37,124
- 11
- 56
- 82
Ray Tayek is right. Use the command pattern. That is, for every action the user performs, you push that action (command) and any data related to that action on the Undo stack.
Let's look at a paint program for simplicity. The process is directly applicable to your OpenGL app. The user draws a line from x1, y1 to x2, y2. The command could be represented something like this:
command = DrawLine
data = x1, y1, x2, y2
You'd probably need at least two classes for the Undo data, but they'd be small and easily implemented. When the user wants to undo, just pop the top object off the Undo stack, look at it, and carry-out the associated undo action (specific to each type of command). Or, you could even include the undo action in each command object itself. Then push the undone command on the Redo stack.
This should be enough to get you started. HTH

- 1,660
- 2
- 21
- 52
-
I amend my answer above. For 3D graphics, DwB has a good approach. Use the unaltered image as part of your Undo data. – Frecklefoot Apr 02 '13 at 16:13