title: How to render a LWJGL game scene to a ByteBuffer?
body: <p>I am currently working on a project that involves rendering LWJGL game scenes to a video stream instead of a window. I believe I can achieve that if I render a game scene to an intermediate format, such as a ByteBuffer. I am trying to extend LWJGL <code>VoxelGame</code> <a href="https://github.com/LWJGL/lwjgl3-demos/blob/main/src/org/lwjgl/demo/game/VoxelGameGL.java" rel="nofollow noreferrer">demo</a> as a proof of concept.</p>
  <p>I have found a <a href="https://stackoverflow.com/questions/35118081/how-do-i-write-to-a-video-file-from-an-lwjgl-display">similar SO question</a> and a <a href="http://forum.lwjgl.org/index.php?topic=5128.0" rel="nofollow noreferrer">forum post</a> but I was not able to make that work. I am a beginner on OpenGL and LWJGL and I am struggling on finding comprehensible documentation on that.</p>
  <p>On the start of the render loop (<code>runUpdateAndRenderLoop</code>) the function <code>glBindFramebuffer</code> is called. To my understanding it binds FBO to the current context so that any rendering will be directed to it.</p>
  <p>I have tried using <code>glGetTexImage</code> and <code>glReadPixels</code> to populate a ByteBuffer but it didnt work. I have also tried that after <code>glBlitFramebuffer</code> since I want to get the full rendered image to the ByteBuffer.</p>
  <p>How can I render the current game scene to a ByteBuffer? Is there a better way of going about rendering game scenes to a video stream instead of an intermediate ByteBuffer?</p>
  <pre class="lang-java prettyprint-override"><code>private void runAndUpdateRenderLoop() {
      // ...
      glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
      glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
      byte[] pixels = new byte[width * height * 4];
      ByteBuffer buffer = ByteBuffer.allocateDirect(pixels.length).order(ByteOrder.nativeOrder());
      glGetTexImage(GL_TEXTURE_BUFFER, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
      glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
      glfwSwapBuffers(window);
  }
  </code></pre>