2
votes

I'm trying to load multiple textures (from 8x8 *.bmp images) into multiple FBOs, by the following snippet:

glTexImage3D(GL_TEXTURE_2D_ARRAY_EXT, 0, GL_RGB8I_EXT, TEXTURE_WIDTH, TEXTURE_HEIGHT, TEXTURE_NUM, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

for( int i = 0; i < TEXTURE_NUM; i++ )
{
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, RBO_ID[i]);
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, TEXTURE_WIDTH, TEXTURE_HEIGHT);
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO_ID[i]);
    glFramebufferTextureLayerEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, TexArray_ID, 0, i);
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, RBO_ID[i]);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

checkGLErrors("begin check");
for ( int i = 0; i < TEXTURE_NUM; i++ )
{
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO_ID[i]);
    char fileName[32];
    sprintf(fileName, "./img/%02d.bmp", i);
    unsigned char* imgData;
    imgData = loadBMP(fileName);
    if (imgData)
        printf("imgData %s is successfully loaded\n",fileName);
    glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, 8, 8, 1, GL_RGB, GL_UNSIGNED_BYTE, imgData);
    checkGLErrors("end check");
    free(imgData);      
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, NULL);
}

However, I cannot get these textures attached using glTexSubImage3D function. The CheckGLErrors() indicates that the error is in the following line, stating "Invalid Value" error.

glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, 8, 8, 1, GL_RGB, GL_UNSIGNED_BYTE, imgData);

After reading the manual , i think the error is associated with the following statement:

GL_INVALID_VALUE is generated if xoffset < - b , xoffset + width > w - b , yoffset < - b , or yoffset + height > h - b , or zoffset < - b , or zoffset + depth > d - b , where w is the GL_TEXTURE_WIDTH, h is the GL_TEXTURE_HEIGHT, d is the GL_TEXTURE_DEPTH and b is the border width of the texture image being modified. Note that w, h, and d include twice the border width.

Which left me no clue on how to get these textures work. Any Ideas? Thanks in advance.

1

1 Answers

4
votes

Attaching textures to an FBO only makes sense if you want to render a new image into the texture. However you already have the images in form of image files, so there's absolutely no reason to attach the textures to an FBO (unless you want to waste memory and CPU cycles on loading images that are going to be "sacrificed to the FBO").

Get rid of that misuse of FBOs and you should be fine.

Maybe you confused FBOs (Frame Buffer Objects) with PBOs (Pixel Buffer Objects), which are are different kind of thing and actually make sense to use if you want to accelerate texture generation.

EDIT due to comment

What you actually need is a Texture Object

GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_3D, textureID);

glTexImage3D(GL_TEXTURE_2D_ARRAY_EXT, 0, GL_RGB8I_EXT, TEXTURE_WIDTH, TEXTURE_HEIGHT, TEXTURE_NUM, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

checkGLErrors("begin check");
for ( int i = 0; i < TEXTURE_NUM; i++ )
{
    char fileName[32];
    sprintf(fileName, "./img/%02d.bmp", i);
    unsigned char* imgData;
    imgData = loadBMP(fileName);
    if (imgData)
        printf("imgData %s is successfully loaded\n",fileName);
    glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, 8, 8, 1, GL_RGB, GL_UNSIGNED_BYTE, imgData);
    checkGLErrors("end check");
    free(imgData);
}