tigeradol
tigeradol

Reputation: 259

OpenGL Clear multiple 3D texture

I have 4 3D textures and I am writing on them using imageStore in a single call, everything works fine. The only problem is how I clear them in each frame. This is how I create them

 for (int i = 0; i < MIPLEVELS; ++i){
    glGenTextures(1, &volumeTexture[i]);
    glBindTexture(GL_TEXTURE_3D, volumeTexture[i]);
    glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, volumeDimensions / (1 << i), volumeDimensions / (1 << i), volumeDimensions / (1 << i), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
    glBindTexture(GL_TEXTURE_3D, 0);
    glGenFramebuffers(1, &volumeFbo[i]);
    glBindFramebuffer(GL_FRAMEBUFFER, volumeFbo[i]);
    glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, volumeTexture[i], 0);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

for (int i = 0; i < MIPLEVELS; ++i){
    glBindFramebuffer(GL_FRAMEBUFFER, volumeFbo[i]);
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
}

and this is what I do to clear them each frame

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

for (int i = 0; i < MIPLEVELS; ++i){
    glBindFramebuffer(GL_FRAMEBUFFER, volumeFbo[i]);
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
}

glBindFramebuffer(GL_FRAMEBUFFER, 0);

As you can see I am using 4 different FBOs which is a big waste. I tried using 4 targets but they don't get cleared. I did it this way: I binded a single FBO and created 4 targets using glFrameBufferTexture3D and then each frame I called

glBindFramebuffer(GL_FRAMEBUFFER, volumeFbo[0]);
    GLenum buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
    glDrawBuffers(4, buffers);
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);

But nothing happened.

Upvotes: 0

Views: 563

Answers (1)

lscandolo
lscandolo

Reputation: 11

I believe there are two ways of solving this issue.

You either do a loop where you attach each texture as GL_COLOR_ATTACHMENT0 and clear each texture.

Otherwise, you can also use the glClearBuffer family of textures: https://www.opengl.org/sdk/docs/man3/xhtml/glClearBuffer.xml

Upvotes: 1

Related Questions