I’m trying to implement a multipass render using framebuffers (so i need firstly to render the scene on a texture using a shader, than i need to use the result texture as input in another shader, and render with this last one).
What i have done is the following:
I created a framebuffer, an empty texture and attached it to the framebuffer
glGenFramebuffers(1, &this->_id);
glBindFramebuffer(GL_FRAMEBUFFER, this->_id);
// create a color attachment texture
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
glTexImage2D(
GL_TEXTURE_2D,
0,
this->_internalTextureFormat,
_frameBufferSize.width,
_frameBufferSize.height,
0,
this->_textureFormat,
this->_pixelValueType,
NULL
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texID, 0);
}
Now, from what i understood, everything i render after attaching a texture to the framebuffer, gets rendered on the texture.
So in the main loop i do:
glBindFramebuffer(GL_FRAMEBUFFER, this->_id);
glViewport(0, 0, this->_frameBufferSize.width, this->_frameBufferSize.height);
glEnable(GL_DEPTH_TEST); // enable depth testing (is disabled for rendering screen-space quad)
glClearColor(refreshColor.coordinates.x, refreshColor.coordinates.y, refreshColor.coordinates.z, refreshColor.coordinates.w);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
RenderScene();
With this code i rendered the scene on the texture, so the texture now contains the scene.
Now i’m not sure what to do. I tried implement something like this, right after the RenderScene()
:
tfmShader->use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texID);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_DEPTH_TEST); // disable depth test so screen-space quad isn't discarded due to depth test.
glViewport(0, 0, _frameBufferSize.width, _frameBufferSize.height);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
In “tfmShader” i implemented a simple filter to the screen texture with id texID.
Then after binding the default framebuffer, i render the screen quad with a shader that simply shows the result.
this->_frameBufferShader->use();
glBindVertexArray(_screenQuadVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texID);
glDrawArrays(GL_TRIANGLES, 0, 6);
What i get from this is the scene rendered on the texture, without the filter applied with the first shader.
I’m not sure what i need to do, to apply a filter to the texture and use the result as input on a shader.
I also tried to use two textures and binding / unbinding them to the framebuffer, but nothing worked as expected.
Thanks