Skip to content

Instantly share code, notes, and snippets.

@staafl
Last active February 1, 2020 00:34
Show Gist options
  • Save staafl/55fe3b658cdc30f4becca548ce35125b to your computer and use it in GitHub Desktop.
Save staafl/55fe3b658cdc30f4becca548ce35125b to your computer and use it in GitHub Desktop.
OpenGL shader chaining using FrameBuffer woes
// setting up two shaders
bufferPass = setupRenderPass("bufferA.frag", 1 /* toBuffer */)
imagePass = setupRenderPass("image.frag", 0 /* toBuffer */)
// inside setupRenderPass
renderPass setupRenderPass(shaderFile, toBuffer) {
// ...
renderPass.prog = getShader(shaderFile);
renderPass.iChannel0 = glGetUniformLocation(renderPass.prog, "iChannel0");
if (toFrameBuffer) {
GLuint FramebufferName = 0;
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
GLuint renderedTexture;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &renderedTexture);
glBindTexture(GL_TEXTURE_2D, renderedTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, winw, winh, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexture, 0);
GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
std::cout<<"Error setting up framebuffer!"<<std::endl;
exit(1);
return;
}
renderPass.FramebufferName = FramebufferName;
renderPass.texture = renderedTexture;
}
return renderPass;
// main loop
display(bufferA);
glBindFramebuffer(GL_FRAMEBUFFER, g_toy.bufferA.FramebufferName);
display(image);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// display function
void display(renderpass& r) {
glUseProgram(r.prog);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, r.texture);
glUniform1i(r.uloc_iChannel, 0);
glRecti(-1,-1,1,1);
}
// bufferA shader
layout(location = 0) out vec4 color;
...
color = vec4(1, 1, 0, 1);
// image shader
uniform sampler2D iChannel0;
...
color = texture(iChannel0, UV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment