PDA

View Full Version : how to use 6 2d textures as 1 cube texture



shapeare
05-11-2012, 05:09 AM
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?

Alfonse Reinheart
05-11-2012, 05:41 AM
Why not attach the 6 faces of a cube texture using MRT? (http://www.opengl.org/wiki/Framebuffer_Object#Attaching_Images) Or even better, attach the cubemap as a whole with layered rendering (http://www.opengl.org/wiki/Framebuffer_Object#Layered_Images)?

thokra
05-11-2012, 05:45 AM
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.

shapeare
05-11-2012, 09:02 AM
Why not attach the 6 faces of a cube texture using MRT? (http://www.opengl.org/wiki/Framebuffer_Object#Attaching_Images) Or even better, attach the cubemap as a whole with layered rendering (http://www.opengl.org/wiki/Framebuffer_Object#Layered_Images)?
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...

thokra
05-11-2012, 10:11 AM
Hmm, can you check if the invalid op is perhaps generated before attaching the images? Normally the error shouldn't be generated.

Alfonse Reinheart
05-11-2012, 12:37 PM
You missed a line from the wiki:


When attaching a cubemap, you must use the Texture2D function, and the textarget must be one of the 6 targets for cubemap binding.

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

thokra
05-11-2012, 02:47 PM
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?

Alfonse Reinheart
05-11-2012, 04:09 PM
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.

shapeare
05-11-2012, 08:15 PM
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.