Using multiple textures on the same FrameBuffer

Hello, I’m making a drawing board-like application and i need help in clearing something up… My current standing is that i have a frameBuffer with a rendertexture attached to it… I draw vertices to the render texture and i show that texture on a quad, this is how my drawing application is working. Now i want to implement a selection tool where i cut out a portion and move it and drop it where i want. I have done the cutting and storing pixels in a data structure… but i cant display it without ruining my current texture on the rendertexture.

So i think the solution is to get a secondary texture and overlap that on the first quad. I can move the stored pixels onto the second quad and move them as i like and when i’m done with it, i transfer the final pixels (at new locations) to the primary render texture. So the problem i’m getting is that i cant seem to get my second texture binded to the framebuffer. Here is the code for my primary texture


    glGetIntegerv(GL_FRAMEBUFFER_BINDING, &screenFrameBuffer);
    glGenFramebuffers(1, &renderFrameBuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, renderFrameBuffer);
    checkGLError("glBindFramebuffer");

    // The texture we're going to render to
    glGenTextures(1, &renderedTexture);
    checkGLError("glGenTextures");

    // Primary Texture
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, renderedTexture);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //GL_LINEAR
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //GL_NEAREST
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _Width, _Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, primaryTextureDataStorage);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderedTexture, 0);
    checkGLError("glFramebufferTexture2D");

Here is what i’m doing right after it for the secondary texture


    // Secondary Texture
    glActiveTexture(GL_TEXTURE1); // activating the 2nd texture
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //GL_LINEAR
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //GL_NEAREST
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _Width, _Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, secondaryTextureDataStorage);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, renderedTexture, 0);
    checkGLError("glFramebufferTexture2D Secondary");

From my understanding, we can bind multiple textures on the same framebuffer but at different color attachment slots. I even tried changing the texture buffer by generating, binding another texture buffer and attaching that to the color attachment 1… i dont know if i did that wrong or not, i changed back to using a single texture buffer because my current understanding says color attachments can work around that.

Here is the code i use to draw:


    GLuint texPos = glGetAttribLocation(glProgram.textureProgram, "a_tex_texCoord");
    GLuint vertPos = glGetAttribLocation(glProgram.textureProgram, "a_tex_position");
    glVertexAttribPointer(vertPos, 2, GL_FLOAT, GL_FALSE, 2*sizeof(GLfloat), squareVertices);
    glEnableVertexAttribArray(vertPos);
    glVertexAttribPointer(texPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), textureVertices);
    glEnableVertexAttribArray(texPos);

    glDisable(GL_BLEND);
    glActiveTexture(GL_TEXTURE0);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);;
    checkGLError("glDrawArrays-PrimaryTexture");

    // drawing secondary on top the primary
    glEnable(GL_BLEND);
    glActiveTexture(GL_TEXTURE1);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    checkGLError("glDrawArrays-SecondaryTexture");

    glDisableVertexAttribArray(vertPos);
    glDisableVertexAttribArray(texPos);

EDIT:I changed uniform location as well between the two glDrawArrays method… 0 when i need the primary and 1 when i need secondary

You can, but I don’t think that’s of much use for your application. Having more than one colour attachment is only useful if you have a fragment shader with multiple outputs.

I suspect that you just need to change which texture is bound to the FBO (or have multiple FBOs and change which FBO is bound).

Thanks for correcting me… When you said not changing attachment and still changing texture made figure it out… i was confusing color attachment with active texture… i thought for texture 0 i have to use attachment 0 and 1 for 1. Now it seems to appear correctly i have attachment 0 (which i dont change) and i only change the active texture number.

Tho i seem to have encountered another problem… can we select which texture on a render texture do we write to? Like i want to write on texture 0 with the renderframebuffer technique. But it seems to be writing over both the textures.

Rendering operations write to the texture(s) bound to the current framebuffer, and read from textures bound to texture units.

Thanks for the help, i had found the issue. I was wrongly rebinding another render texture on the same frame buffer… I only had to do


    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _Width, _Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, secondaryTextureDataStorage);

when ever i had to change the texture… Tho it didnt fullfil my later need because i had to read from it later, so i ended up making another fbo as well.