Copy a randomly sized block of pixels into an OpenGL ES texture ... somehow?
I am writing a painting application and the painting canvas is an OpenGL texture. When you paint on the canvas, it detects which area of the canvas texture has been modified and copies that pixel data (using glReadPixels) before applying the changes you made.
To undo, I just want to revert to the previous state of the texture using the pixel data that was copied. However, OpenGL ES does not provide the glDrawPixels command. What's the best way to do this?
I've looked at two options, but I'm not sure if that's great too:
-
Create a temporary texture using the pixels I copied and paint over them. (However, the copied area cannot be two!)
-
Unlink the large canvas texture completely, manually change the texture bytes and then return it to OpenGL. I don't use any compression, so it might not be that bad. But does this look like a hack?
Does anyone have any ideas? I would really appreciate it!
a source to share
In case anyone stumbles upon this while trying to do something like this, I've come up with a solution that seems to work well.
- Take an image of the current texture by binding it to a framebuffer and then writing the framebuffer to CGImageRef.
- Create a new CGContext and draw the existing texture CGImageRef. Then, paint the old texture data into the part that the user changed, effectively "undoing" what changed to the image.
- Destroy the old OpenGL texture and create a texture from the CGContext.
I think this is a pretty slow way to get around things, but I don't need huge performance - my real concern was limiting the amount of data that is stored to represent the "old" texture.
If you need help with this (there is quite a bit of code there), email me.
a source to share