Skip to content

Instantly share code, notes, and snippets.

@kaadmy
Created December 29, 2019 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaadmy/3f2e7008494379e73918dca84ec6abca to your computer and use it in GitHub Desktop.
Save kaadmy/3f2e7008494379e73918dca84ec6abca to your computer and use it in GitHub Desktop.
```c
#define BLOOM_LEVELS 7
int size_bloom[BLOOM_LEVELS][2];
size_bloom[0][0] = MAX(viewport_size_quarter[0], 64);
size_bloom[0][1] = MAX(viewport_size_quarter[1], 64);
for(int i=1; i<BLOOM_LEVELS; i++) {
size_bloom[i][0] = MAX(ize_bloom[i - 1][0] / 2, 1);
size_bloom[i][1] = MAX(size_bloom[i - 1][1] / 2, 1);
}
GLuint textures[2]; // Initialize etc
GLuint framebuffers[2][BLOOM_LEVELS]; // Initialize etc
for(int i=0; i<2; i++) {
// Bind texture[i]
// You can use glTexImage2D as well if you can't use GL4 extensions
// http://docs.gl/gl4/glTexStorage2D
// `size_bloom` must match the sizes that OpenGL expects
glTexStorage(GL_TEXTURE_2D, BLOOM_LEVELS, GL_RGB, size_bloom[0][0], size_bloom[0][1]);
}
for(int i=0; i<2; i++) {
for(int j=0; j<BLOOM_LEVELS; j++) {
// Bind framebuffer[i][j]
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textures[i], j);
}
}
/* RENDER POSTPROCESS FOR A SINGLE FRAME */
glViewport(0, 0, size_bloom[0][0], size_bloom[0][1]);
// Bind framebuffer[1][0]
// Bind bloom extract shader (the shader would have to output a texture where black = no bloom)
glDrawArrays(fullscreen_quad, ...);
// Bind blur shader
for(int i=0; i<BLOOM_LEVELS; i++) {
glViewport(0, 0, size_bloom[i][0], size_bloom[i][1]);
// Bind framebuffer[0][i]
// Set uniform to signify X axis gaussian blur
// Set uniform to specify which texture mipmap to source from (see next line)
int i_previous = ((i == 0) ? i : (i - 1))
// Set uniform for pixel scale of source texture (see next line)
float pixel_scale = {1.0 / size_bloom[i_previous][0], 1.0 / size_bloom[i_previous][1]};
glBindTexture(GL_TEXTURE_2D, textures[1]);
glDrawArrays(fullscreen_quad, ...);
// Bind framebuffer[1][i]
// Set uniform to signify X axis gaussian blur
// Set uniform to specify which texture mipmap to source from (in this case, the value of `i`)
glBindTexture(GL_TEXTURE_2D, textures[0]);
glDrawArrays(fullscreen_quad, ...);
}
glViewport(0, 0, size_bloom[0][0], size_bloom[0][1]);
// Bind framebuffer[0][0]
// Bind bloom merge shader (take all source images, additively blend all mipmaps with bicubic filtering)
glBindTexture(GL_TEXTURE_2D, textures[1]);
glDrawArrays(fullscreen_quad, ...);
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment