Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: how to use 6 2d textures as 1 cube texture

  1. #1
    Intern Contributor
    Join Date
    Jun 2011
    Posts
    82

    Question 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?

  2. #2

  3. #3
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    906
    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.
    Last edited by thokra; 05-11-2012 at 06:06 AM.

  4. #4
    Intern Contributor
    Join Date
    Jun 2011
    Posts
    82
    Quote Originally Posted by Alfonse Reinheart View Post
    Thanks. I followed your instruction but there is an error I cannot get through.

    The cube texture object is defined as:
    Code :
    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:
    Code :
    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:
    Code :
    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...

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    906
    Hmm, can you check if the invalid op is perhaps generated before attaching the images? Normally the error shouldn't be generated.

  6. #6
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,732
    You missed a line from the wiki:

    Quote Originally Posted by 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.

  7. #7
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    906
    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?

  8. #8
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,732
    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.

  9. #9
    Intern Contributor
    Join Date
    Jun 2011
    Posts
    82
    Thanks all.

    I replaced
    Code :
    glFramebufferTextureLayer(GL_FRAMEBUFFER, fboAttachment[i], cubeTex, 0, i);
    with
    Code :
    glFramebufferTexture2D(GL_FRAMEBUFFER, fboAttachment[i], cube[i], cubeTex, 0);
    where cube[] is defined as:
    Code :
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •