how to use 6 2d textures as 1 cube texture

I want to create a cube map, so I plan to render the six faces into six 2d textures within one rendering pass using MRT. But how to form a cube map texture with these 2d textures?

Why not attach the 6 faces of a cube texture using MRT? Or even better, attach the cubemap as a whole with layered rendering?

You can’t directly use a GL_TEXTURE_2D object and attach it to a GL_TEXTURE_CUBE_MAP. You’ll have to create the cube map regularly and copy the contents of all 6 faces. What you can do, however, is attach a cube map to a FBO and write a simple pass-through geometry shader which is passed a uniform distinguishing the face of the cube you want to render to.

Edit: Actually Alfonse’s proposal to attach the layers of the cube is propably somewhat more efficient because you can choose the render target in the fragment shader and don’t have to use a geometry shader at all.

Thanks. I followed your instruction but there is an error I cannot get through.

The cube texture object is defined as:


glActiveTexture(GL_TEXTURE7);
glGenTextures(1, &cubeTex);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubeTex);
        


glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);  


for(int i=0; i<6; ++i)
    glTexImage2D(cube[i], 0, GL_RGB32F, 16, 16, 0, GL_RGB, GL_FLOAT, NULL);

and the code block used to bind cube-texture faces to framebuffer object is:


glGenFramebuffers(1, &cubeFbo);
glBindFramebuffer(GL_FRAMEBUFFER, cubeFbo);


for(int i=0; i<6; ++i){
    glFramebufferTextureLayer(GL_FRAMEBUFFER, fboAttachment[i], cubeTex, 0, i);
}


glBindFramebuffer(GL_FRAMEBUFFER, 0);

where fboAttachment is defined as:


GLenum fboAttachment[6] = {GL_COLOR_ATTACHMENT0,
                    GL_COLOR_ATTACHMENT1,
                    GL_COLOR_ATTACHMENT2,
                    GL_COLOR_ATTACHMENT3,
                    GL_COLOR_ATTACHMENT4,
                    GL_COLOR_ATTACHMENT5};

The program pops an error when it gets to glFramebufferTextureLayer, saying GL_INVALID_OPERATION.
I am not sure if the above setup is correct…

Hmm, can you check if the invalid op is perhaps generated before attaching the images? Normally the error shouldn’t be generated.

You missed a line from the wiki:

I don’t see you using glFramebufferTexture2D. You should have gotten an OpenGL error when you use glFramebufferTextureLayer on a cubemap.

Doh. Yeah, it clearly states in the API ref for glFramebufferTextureLayer:

texture must either be zero or the name of an existing three-dimensional texture, one- or two-dimensional array texture, or multisample array texture.

My bad. However, isn’t that a little inconsistent? Isn’t a cubemap supposed to be a layered texture as well? Only that the number of layers isn’t variable as in a 3D texture or an array?

However, isn’t that a little inconsistent?

It’s OpenGL; you’ll get used to it.

Isn’t a cubemap supposed to be a layered texture as well?

It is.

Thanks all.

I replaced


glFramebufferTextureLayer(GL_FRAMEBUFFER, fboAttachment[i], cubeTex, 0, i);

with


glFramebufferTexture2D(GL_FRAMEBUFFER, fboAttachment[i], cube[i], cubeTex, 0);

where cube[] is defined as:


GLenum  cube[6] = {  GL_TEXTURE_CUBE_MAP_POSITIVE_X,
                     GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
                     GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
                     GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
                     GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
                     GL_TEXTURE_CUBE_MAP_NEGATIVE_Z };

and the error disappears.