0

I created a texture by rendering to an FBO and I've been trying to read the texture with no success for quite some time. After my failed attempts with glReadPixels() and glGetTexImage()(access violation errors) I decided to try with a PBO.

Here is the code I use immediately after rendering to the framebuffer and creating the texture successfully:

    glReadBuffer(GL_FRONT);
    glBindBuffer(GL_PIXEL_PACK_BUFFER, PBO);
    glReadPixels(0, 0, 1024, 768, GL_RGB, GL_UNSIGNED_BYTE, 0);
    glBindBuffer(GL_PIXEL_PACK_BUFFER, PBO);
    GLubyte* ptr = (GLubyte*)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
    if (ptr)
    {
        //processPixels(ptr, ...);
        glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
    }
    glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);

However, I'm never entering the loop, meaning no values are read. Am I forgetting something?

John Katsantas
  • 571
  • 6
  • 20
  • Did you check if your FBO was successfullly created? – Stefan Scheller Jun 18 '20 at 08:48
  • I checked the texture I created rendering to the FBO, so I assumed the FBO was fine. I checked it now, it was successfully created. – John Katsantas Jun 18 '20 at 08:50
  • I suggest to call `glGetError` right after you created the FBO just to make sure that the texture/ rendering target actually exists. – Stefan Scheller Jun 18 '20 at 08:56
  • I checked it, no errors. I've also rendered the resulting texture on a rectangle so I now it's been created without a problem. – John Katsantas Jun 18 '20 at 08:58
  • Ok, did you also check that the PBO was successfully created and did you bind the FBO before reading? – Stefan Scheller Jun 18 '20 at 09:02
  • It seems there are no errors there either. glGenBuffers(1, &PBO); glBindBuffer(GL_PIXEL_PACK_BUFFER, PBO); – John Katsantas Jun 18 '20 at 09:05
  • Did you bind the FBO before reading? I am running out of ideas :( – Stefan Scheller Jun 18 '20 at 09:07
  • Yes, that is not the problem either. I just want to read the pixels and I've spent a day searching for the single line of code that will help me :(. Are you aware of any other methods? Here is another attempt of mine: https://stackoverflow.com/questions/62423688/cant-use-glgetteximage-to-read-a-depth-map-texture/62435639#62435639 – John Katsantas Jun 18 '20 at 09:09
  • Try : glBindFramebuffer(GL_READ_FRAMEBUFFER, fboId); glReadBuffer(GL_COLOR_ATTACHMENT0); glReadPixels(...); glBindFramebuffer(GL_FRAMEBUFFER, 0); – Rémi Jun 18 '20 at 09:13
  • @Rémi The parameters in glReadPixels remain the same? – John Katsantas Jun 18 '20 at 09:15
  • Yes, you can keep the same parameters. Your output buffer "ptr" need to be put at last argument of glReadPixels if you don't use pbo. Don't forget to allocate memory for "ptr" in this case. – Rémi Jun 18 '20 at 09:27
  • Another question : Did you attached your texture to your fbo with : glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureId, 0); ? – Rémi Jun 18 '20 at 09:32
  • 1
    I was using this code before to make screenshots of rendering results from FBOs: GLubyte* pixels = new GLubyte[3 * width * height]; glBindFramebuffer(GL_READ_FRAMEBUFFER, fboID); glReadBuffer(GL_COLOR_ATTACHMENT0); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels); – Stefan Scheller Jun 18 '20 at 09:41

0 Answers0