Hello,
I think I’m fairly close to getting framebuffers working as an input texture on a shader. I’ve definitely got two shaders working in parallel, as shown in the image, the mouse is drawing a red circle in one shader, and the 2nd shader is colouring the background green, it’s composited on top, so that’s why the mouse is now yellow.
I’m trying to feed the framebuffer into the shader via
glBindTexture(GL_TEXTURE_2D, openGLFrameBuffer.getFrameBufferID());
GLint id = openGLFrameBuffer.getTextureID();
shaderProgram->setUniform("texture01", id);
But I don’t think I’m doing it right. I’ve made a simple little paint shader here that I’m trying to port for debugging: https://www.shadertoy.com/view/3lfczf
and here’s the full code in the render loop if anyone can spot what I’m doing wrong:
void OpenGLComponent::renderOpenGL()
{
jassert (OpenGLHelpers::isContextActive());
// Scale viewport
const float renderingScale = (float) openGLContext.getRenderingScale();
glViewport (0, 0, roundToInt (renderingScale * getWidth()), roundToInt (renderingScale * getHeight()));
// Set background color
OpenGLHelpers::clear (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
////////////////////////////////////////////
// Select shader program
openGLFrameBuffer.makeCurrentRenderingTarget();
shaderProgram->use();
shaderProgram->setUniform("resolution", (renderingScale * getWidth()) ,(renderingScale * getHeight()));
glBindTexture(GL_TEXTURE_2D, openGLFrameBuffer.getTextureID()); // or openGLFrameBuffer.getFrameBufferID()? do they work the same?
GLint idb = openGLFrameBuffer.getTextureID();
shaderProgram->setUniform("texture01", idb); // this is setting fbo as the uniform texture01
openGLFrameBuffer.releaseAsRenderingTarget();
openGLContext.extensions.glBindVertexArray(VAO);
glDrawArrays (GL_TRIANGLES, 0, (int) vertices.size());
openGLContext.extensions.glBindVertexArray (0);
////////////////////////////////////////////
// Do 2nd pass
openGLFrameBuffer.makeCurrentRenderingTarget();
bufferAProgram->use();
glEnable(GL_DEPTH_TEST);
glBindTexture(GL_TEXTURE_2D, openGLFrameBuffer.getTextureID());
bufferAProgram->setUniform("texture01", 0); // What do I need to do to get the texture in here? is it another .bind() ??
// how to get this back into shader A as texture 01, and store pixels, do I need to render to an OpenGL texture?
bufferAProgram->setUniform("resolution", (renderingScale * getWidth()) ,(renderingScale * getHeight()));
//clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Draw Verts for the screen quad, triangle array defined in ShapeVertices.cpp
openGLContext.extensions.glBindVertexArray(VAO);
glDrawArrays (GL_TRIANGLES, 0, (int) vertices.size());
openGLContext.extensions.glBindVertexArray (0);
openGLFrameBuffer.releaseAsRenderingTarget();
glDisable(GL_DEPTH_TEST);
// clear all relevant buffers
//glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//glClear(GL_COLOR_BUFFER_BIT);
}
Can anyone spot what’s happening or going wrong? I suspect it’s not setting the right texture into the shader program.
If anyone has any thoughts I’d be greatful @bzhkl I started a new thread instead of bombarding yours, apologies!