Question

I'm using the fragment shader to render to a texture. I would like the rendering of the previous iteration of the shader to be used by the next one. How is that possible?

Was it helpful?

Solution

To re-use the texture in the fragment shader:

either

  • unbind the current framebuffer object (bind the null framebuffer object or bind another framebuffer object) to which the texture is attached,

or

  • detach the texture from the framebuffer object (and attach null, attach another texture with framebufferTexture2D or attach a renderbuffer instead).

and then bind the texture as a normal texture to be used in the fragment shader (via sampler2D and texture2D in the shader code).

To re-use the texture in the vertex shader, it is roughly the same, except that this feature (vertex textures) is non-standard (and has some limitations). Check MAX_VERTEX_TEXTURE_IMAGE_UNITS for support.

If you have an iterative algorithm that requires multiple rendering passes, one typical approach is to switch between two textures 1 and 2: during odd rendering passes, 1 is attached to the current framebuffer object and 2 is bound as a normal texture and during even rendering passes, it's the opposite. See also this answer on whether texture attachments should be changed in a single framebuffer object or whole framebuffer objects should instead be swapped.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top