PDA

View Full Version : Manual modification of texture pixel data.



Kaelis
06-18-2008, 12:08 PM
Hello.

First of, let me say im a beginner with OpenGL.

What im trying to do is to render to target texture, then modify its pixels. From what i gather, for rendering to texture i need to use frame buffer object, and for texture modification - pixel buffer object. To be honest, im overwhelmed by all the information i need to gather and understand, wish there was some kind of tutorial for all this, but i guess ill manage.

What im really confused about is how to use PBO for modifying textures. The only reference ive found so far is this: http://www.songho.ca/opengl/gl_pbo.html#unpack
What i dont understand is how to copy pixels from texture to buffer, and how to actually modify said pixels while theyre in PBO.

Im also not sure how to copy pixel data from buffer to texture. Am i right thinking that i just need to use glTexSubImage2D, using pointer to the target of PBO from glBindBufferARB for const GLvoid *pixels parameter?

Zengar
06-18-2008, 12:31 PM
To update parts of the texture use glTexSubImage. It does not matter if you use PBO or not: the first may provide better performance in many cases. Using buffer objects is very simple: if a buffer is bound to an appropriate target (like PIXEL_UNPACK_BUFFER) then the data argument of the dependent functions (like TexSubImage), usualy pointer, is treated as an integer offset. So using buffer objects is a two-stage procedure: first you load the data into the buffer, then you copy it to the texture. But: you don't really need to use PBO for modifying the texture, a simple client-side update may be enough. You will have to find the approach mst suitable to you. My advice would be to implement the simplest algorithm first (without the PBO) and then improve it gradually.

Kaelis
06-18-2008, 12:39 PM
Thanks for answering.

Ill be honest, that didnt help much, i have even more questions now. I did mention im a beginner =)


It does not matter if you use PBO or not: the first may provide better performance in many cases.
Okay, so how do i modify pixels of a texture at runtime (every frame, to be exact) without PBO?


first you load the data into the buffer, then you copy it to the texture.
Point is, how exactly do i load data into the buffer from texture, how do i copy it back to the texture after modifying, and most importantly, how do i modify the data, pixel by pixel?


My advice would be to implement the simplest algorithm first (without the PBO) and then improve it gradually.
Could you elaborate on that, please?

Zengar
06-18-2008, 01:13 PM
Let me see if I get you right: you want to copy a part of the texture back to the CPU, change it there and copy it back? Or do you just want to update a part of a texture? Don't bother with the PBO for now, just use glTexSubImage2D to change (upload) a part of a texture and glGetTexImage to read it (or ReadPixels to read a part of the texture when it is bound as a rendering target to a FBO).

Kaelis
06-18-2008, 01:30 PM
To be precise, i want to render to texture, loop through every pixel changing it according to an enquation, then render said texture to screen. So that means first option, i think.

Zengar
06-18-2008, 01:39 PM
In this case, your best bet would be to use a fragment shader that performs your computation (if this computation is not too complicated). This is usualy done by rendering a fullscreen quad with the texture on, thus running each pixel through the fragment sahder.

If you absolutely must use the CPU, use the glGetTexture/glSetTexSubImage as I previously suggested.

Kaelis
06-18-2008, 01:59 PM
In this case, could you explain how to use glGetTexture/glSetTexSubImage to read, loop through and write back pixels of a texture? Ive already spent a few hours on searching a method for modyfying texture pixels directly, but i havent seen glGetTexture/glSetTexSubImage to be used like that.

Zengar
06-18-2008, 02:05 PM
What is there to explain? You read teh texture data using the glGetTexImage: this gives you an array containing the texels. Then you loop through the texels, compute new values and write them to the texture using the glTexSubImage2D...

P.S. The functions are called glGetTexImage and glTexSubImage2D, sorry for the irritation - I keep forgeting it all the time...

Kaelis
06-18-2008, 02:09 PM
P.S. The functions are called glGetTexImage and glTexSubImage2D, sorry for the irritation - I keep forgeting it all the time...

Heh, thats why i couldnt find any reference on them =)


Many thanks for your replies, youve helped greatly. Id problably have to spend another few hours searching to figure out i can just use glTexSubImage and glGetTexImage.