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?

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();

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.