Results 1 to 2 of 2

Thread: Problem rendering to texture for both color and depth.

  1. #1
    Newbie Newbie
    Join Date
    Jul 2015
    Posts
    1

    Question Problem rendering to texture for both color and depth.

    Hello,
    I have some code that I need to render to a image file, and I also need to save the depth buffer. I have the rendering to file working using glGetTexImage, but I tried to add a depth texture, and get the depth image, but once I create a depth texture, I can't even get the rgb image. Does anyone see anything wrong with this?

    Code :
    glGenFramebuffers(1, &FramebufferName);
        glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
     
        // The texture we're going to render to
        glGenTextures(1, &renderedTexture);
     
        // "Bind" the newly created texture : all future texture functions will modify this texture
        glBindTexture(GL_TEXTURE_2D, renderedTexture);
     
        // Give an empty image to OpenGL ( the last "0" means "empty" )
        glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, TEXTURE_SIZE, TEXTURE_SIZE, 0,GL_RGB, GL_UNSIGNED_BYTE, 0);
     
        // Poor filtering
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     
        // The depth buffer
        GLuint depthrenderbuffer;
        glGenRenderbuffers(1, &depthrenderbuffer);
        glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, TEXTURE_SIZE, TEXTURE_SIZE);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer);
     
        // Create a Depth Texture.
        GLuint depthTexture;
        glGenTextures(1, &depthTexture);
        glBindTexture(GL_TEXTURE_2D, depthTexture);
        glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT24, TEXTURE_SIZE, TEXTURE_SIZE, 0,GL_DEPTH_COMPONENT, GL_FLOAT, 0);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     
        // Set "renderedTexture" as our colour attachement #0
        glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexture, 0);
        glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthTexture, 0);
     
     
        GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
        glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
     
        // check OpenGL error
        for (GLenum err; (err = glGetError()) != GL_NO_ERROR; ) {
          std::cerr << "OpenGL error: " << err << std::endl;
        }
     
        // Always check that our framebuffer is ok
        if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
          return;
      }
     
      // Switch to new framebuffer 
      glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
      glViewport(0,0,TEXTURE_SIZE,TEXTURE_SIZE); // Render on the whole framebuffer, complete from the lower left corner to the upper right
      // Clear the screen
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
      ... Render Bit ...
     
    glGetTexImage(GL_TEXTURE_2D,0,GL_RGBA,GL_UNSIGNED_BYTE,maColorTest);
     
      // check OpenGL error
      for (GLenum err; (err = glGetError()) != GL_NO_ERROR; ) {
        std::cerr << "OpenGL error: " << err << std::endl;
        DebugBreak();
      }
     
      ofstream fout("image.dat",ios::out | ios::binary);
      fout.write((const char *)maColorTest,TEXTURE_SIZE * TEXTURE_SIZE * 4);
      fout.close();
     
      glGetTexImage(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT,GL_UNSIGNED_INT,maZBufTest);
     
      // check OpenGL error
      for (GLenum err; (err = glGetError()) != GL_NO_ERROR; ) {
        std::cerr << "OpenGL error: " << err << std::endl;
        DebugBreak();
      }
     
      fout.open("depth.dat",ios::out | ios::binary);
      fout.write((const char *)maZBufTest,TEXTURE_SIZE * TEXTURE_SIZE * 4);
      fout.close();

  2. #2
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,674
    As the name suggests, glGetTexImage is for reading from textures, not renderbuffers[/i]. If you want to read the contents of a renderbuffer, you must bind the framebuffer that the renderbuffer is attached to and use glReadPixels to read from a buffer in the framebuffer.

Similar Threads

  1. Replies: 0
    Last Post: 12-02-2015, 09:03 AM
  2. Replies: 1
    Last Post: 12-26-2014, 04:57 PM
  3. Replies: 1
    Last Post: 03-10-2014, 12:51 PM
  4. Problem about rendering to depth texture
    By 991060 in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 11-01-2004, 11:32 AM
  5. Problem with rendering the depth buffer to a texture. . .
    By Ostsol in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 02-27-2003, 01:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean