PDA

View Full Version : copying texture to an array



brett01
08-13-2009, 10:26 AM
Hi,

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.

Regards,
brett.

ZbuffeR
08-13-2009, 12:17 PM
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 :
http://www.opengl.org/sdk/docs/man/xhtml/glGetTexImage.xml

brett01
08-13-2009, 02:04 PM
Hi Thank you zbuffer for the reply.I can use gltexsubImage2D but does it work for any rectangle,i.e for a rectangle that I want to fill may be a rotated rectangle w.r.t texture.Since I can supply only the offset , height and width for glTexsubImage2d() How can I use gltexsubimage2d for such a rectangle??All I have is four texture coordinates of that rectangle.

scratt
08-13-2009, 08:23 PM
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...

brett01
08-13-2009, 09:35 PM
Is there a simpler approach.I cannot rotate the texture all I have is 4 points of rectangle inside a texture.It can be in any orientation!

scratt
08-13-2009, 09:48 PM
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.. :)

brett01
08-13-2009, 11:45 PM
Can you give more detail about rendering to a FBO technique please.

brett01
08-13-2009, 11:53 PM
ok I got it.rendering to the fbo is all I have to do.

Heiko
08-14-2009, 12:02 AM
Hi Thank you zbuffer for the reply.I can use gltexsubImage2D but does it work for any rectangle,i.e for a rectangle that I want to fill may be a rotated rectangle w.r.t texture.Since I can supply only the offset , height and width for glTexsubImage2d() How can I use gltexsubimage2d for such a rectangle??All I have is four texture coordinates of that rectangle.

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:

(0,1)
(1,2)
(2,1)
(1,0)

(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.