Copying texture to an array


I want to copy a previously assigned texture image to an array so that I can dynamically modify and update the texture.Thanks in advance.


This is not a good practice, it is better to copy directly the image data to the array before creating a texture with it. Then use glTexSubImage2D to update part of (or the whole) existing texture.

But if you really want to do copy back data from texture to an array, you can :

You can’t.

You can only work with textures which are defined as per glTexSubImage2D, and then use geometry and texture coords to place that texture on the screen rotated or otherwise… or use texcoords to pull non-square shapes out of the texture for use on screen. You cannot write them to textures rotated.

If you really must draw to an image rotated into another texture then you are going to need to create an FBO with a texture. Then get the image you want to place in that texture into another texture (or define it as pure geometry - perhaps a point array generated from a jpg or png) and use the OpenGL pipeline to draw it into the FBO the same way you would with geometry and textures to the screen… But that’s really only useful for procedural texture generation on mobile hardware where texture uploading is too slow otherwise…

I think you will be able to find a much simpler way by rethinking your overall problem…

It’s not clear what you are saying… I suspect that you have some misconceptions about OpenGL.

If you want to draw a texture on the screen rotated then you use a QUAD and texture coordinates, and a texture as your source for the image you are putting on that QUAD. It’s that simple.

If you want to draw at an angle into a texture then the method I outline above is about the only way. And that’s what you seemed to be asking for. Although I have not idea why you’d want to do that.

Can I make a suggestion? Go read The Red Book cover to cover. It’ll take you about half a day and will save you a lot of time… :slight_smile:

You could use gltexsubImage2D, but you’ll need some tricks. If the rectangle you need to update is rotated, you could update a slightly larger rectangle in the texture that is not rotated but includes the entire rotated rectangle. This is not hard, say for example the coordinates of the rotated rectangle are:


(in other words: it is rotated 45 degrees)

Use gltexsubImage2D to get a slightly larger non-rotated rectangle that includes the complete rectangle. All you need is minimum and maximum x and y coordinates:

(min_x, max_y) -> (0, 2)
(max_x, max_y) -> (2, 2)
(max_x, min_y) -> (2, 0)
(min_x, min_y) -> (0, 0)

This rectangle includes the complete rotated rectangle. Now you have the array with pixels and you have to calculate on the cpu which pixels you have to change.