Vasiliy Stavenko
Vasiliy Stavenko

Reputation: 1214

Is it possible to render 3D texture with depth buffer in opengl

I'm trying to implement some program and using this classic code:

glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

Bind depth buffer.

glGenRenderbuffers(1, &depthbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width,
 height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,  GL_RENDERBUFFER, depthbuffer);

Bind several 3D textures:

glGenTextures(targets.size(), textures);
for (auto &target : targets) {
  glBindTexture(GL_TEXTURE_3D, textures[ix]);
  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA32F, width, height, depth, 0, GL_RGBA, GL_FLOAT, NULL);
}
  glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + ix, textures[ix], 0);
  buffers[ix] = GL_COLOR_ATTACHMENT0 + ix;
  ++ix;
}
glDrawBuffers(targets.size(), buffers);

And it do not work, indicating GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS error. I supposed, that, since my textures are layered, there could be a problem with not layered depth buffer. I have created it in the same way as I did it for 3D textures, so I removed depth buffer. This solved task, now my framebuffer is working, rendering completes and so on.

But what if I need to render each layer, having depth buffer attached? Is this functionality supported, are there some seldom api calls to achieve that?

Upvotes: 0

Views: 1271

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473272

If one attached image is layered, then all attached images must be layered. So if you want to do layered rendering with a depth buffer, the depth image must also be layered.

So instead of using a renderbuffer, you should use a 2D array texture with a depth format.

Upvotes: 1

Related Questions