I want to render the depth buffers of multiple views (in a single pass) into a texture array. I’m using a framebuffer with a texture2Darray attached to GL_DEPTH_ATTACHMENT. In the geometry shader I use gl_Layer to render to the different layers. It seems like although an array is attached to GL_DEPTH_ATTACHMENT, only the first 2D texture of the array receives depth information.
I don’t know if this is the right behavior (OpenGL 3.3), because I read that it is possible to render into a depth cubemap in a single pass.
Here is how I create my framebuffer:
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
// depthArray is a GL_TEXTURE_2D_ARRAY (GL_DEPTH_COMPONENT32)
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthArray, 0);
glCheckFramebufferStatus returns GL_FRAMEBUFFER_COMPLETE.
Here is the code for drawing:
glEnable(GL_DEPTH_TEST);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glDrawBuffer(GL_NONE);
glViewport(0, 0, framebufferWidth, framebufferHeight);
glClear(GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
glDrawElementsBaseVertex(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDrawBuffer(GL_BACK_LEFT);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDisable(GL_DEPTH_TEST);
Vertex shader:
#version 330
in vec4 position;
void main(void){
gl_Position = position;
}
Geometry Shader:
#version 330
layout(triangles) in;
layout(triangle_strip, max_vertices = 12) out;
void main(void){
int layer, i;
for(layer=0; layer<4; layer++){
gl_Layer = layer;
for(i=0; i<gl_in.length(); i++){
gl_Position = gl_in[i].gl_Position;
EmitVertex();
}
EndPrimitive();
}
}
The result is a texture array where the first 2D texture contains 0.0 and all others 1.0.
Am I doing something wrong or is this not possible in OpenGL 3.3? (I have an ATI HD4600 card.)