How do I use framebuffer as texture for the next frame in moderngl?

115 Views Asked by At

I'm trying to animate Mandelbrot set slow step-by-step appearance. Here is my approximate code for fragment shader:

#version 330

uniform sampler2D previousIteration;
in vec2 uv; /* from -1 to 1 */

out vec4 color;
layout (location=1) out vec4 zValue;

void main() {
  vec4 state = texture(previousIteration, uv);
  float newReal = state.x * state.x - state.y * state.y + uv.x;
  float newImag = 2 * state.x * state.y + uv.y;
  float newAbs2 = newReal * newReal + newImag * newImag;
  if (newAbs2 < 4) {
    color = vec4(0.0, 0.0, 0.0, 1.0);
  } else {
    color = vec4(1.0, 1.0, 1.0, 1.0);
  }
  zValue = vec4(newReal, newImag, 0.0, 0.0);
}

Then I have to output color framebuffer to the screen in moderngl-window (a trivial task) and also pass zValue framebuffer as previousIteration texture for the next frame (I have no idea how to do it).

I've seen various examples like slimes, but it uses image2D rather then texture; for my purposes I'd be interested in using texture. I'd also prefer to use simple saving with zValue = ... rather then using imageStore.

Using this question and answers I can copy framebuffer content to a numpy array and then send the array to GPU as texture; however, this includes copying to RAM and back to GPU, which is something that should be avoided.

Also, this answer states I do not "copy FBO to texture"; however, I don't actually understand what does it mean.

This answer using glCopyTexImage2D() function seems to be what am I looking for, however, it's C and I can't find something like that in moderngl python API; moreover, the function seems to be deprecated or rarely used in modern code.

0

There are 0 best solutions below