FramebuffersGL 3.0
glFramebufferTextureLayer
Attaches one single layer of an array (or 3D) texture to a framebuffer.
Signature
void glFramebufferTextureLayer(GLenum target, GLenum attachment, GLuint texture, GLint level, GLint layer);
How it works
An array texture holds several same-sized images under one ID. This attaches just one of them, so you can render into each layer in turn. Cascaded shadow maps keep all cascades in one GL_TEXTURE_2D_ARRAY and loop: attach layer i, set cascade i's light matrix, draw the scene.
Parameters
targetGLenum
GL_FRAMEBUFFER, GL_DRAW_FRAMEBUFFER or GL_READ_FRAMEBUFFER.
attachmentGLenum
GL_COLOR_ATTACHMENTi, GL_DEPTH_ATTACHMENT…
textureGLuint
An array, 3D or cubemap-array texture.
levelGLint
Mip level.
layerGLint
Which layer (for a cubemap array: layer = 6 × cube + face).
Tips & pitfalls
Switching the attachment between draws is cheap; there is no need for one framebuffer per layer.
Errors
When the call is invalid it does nothing and sets an error, which glGetError returns:
- GL_INVALID_VALUE
- layer is out of range for the texture.
- GL_INVALID_OPERATION
- texture is not an array, 3D or cubemap-array texture.
Example
example.cpp
1for (int i = 0; i < cascades; ++i) {
2 glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, shadowArray, 0, i);
3 glClear(GL_DEPTH_BUFFER_BIT);
4 drawSceneDepth(lightMatrix[i]);
5}