1

I wanted to simulate 'glow dodge' effect on clip studio using opengl and its shader. So I found out that following equation is how 'glow dodge' works.

final.rgb = dest.rgb / (1 - source.rgb) 

Then I came up with 2 ways to actually do it, but neither one doesn't seem to work.

First one was to calculate 1 / (1 - source.rgb) in the shader, and do multiply blending by using glBlendfunc(GL_ZERO, GL_SRC_COLOR), or glBlendfunc(GL_DST_COLOR, GL_ZERO).

but as khronos page says, all scale factors have range 0 to 1. which means I can't multiply the numbers over 1. so I can't use this method cuz most of the case goes more than 1.

Second one was to bring background texels by using glReadPixel(), then calculate everything in shader, then do additive blending by using glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA).

regardless the outcome I could get, glReadPixel() itself takes way too much time even with a 30 x 30 texel area. so I can't use this method.

I wonder if there's any other way to get an outcome as glowdodge blending mode should have.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

With the extension EXT_shader_framebuffer_fetch a value from the framebuffer can be read:

This extension provides a mechanism whereby a fragment shader may read existing framebuffer data as input. This can be used to implement compositing operations that would have been inconvenient or impossible with fixed-function blending. It can also be used to apply a function to the framebuffer color, by writing a shader which uses the existing framebuffer color as its only input.

The extension is available for dektop OpenGL and OpenGL ES. An OpenGL extension has to be enabled (see Core Language (GLSL)- Extensions. The fragment color can be retrieved by the built in variable gl_LastFragData. e.g.:

#version 400
#extension GL_EXT_shader_framebuffer_fetch : require

out vec4 fragColor;

# [...]

void main 
{
    # [...]

    fragColor = vec4(gl_LastFragData[0].rgb / (1.0 - source.rgb), 1.0);  
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thanks for the fast answer & editing the post. I tried to add "#extension ~~~ require" line as you said, but this error comes out. "error C0202: extension EXT_shader_framebuffer_fetch not supported" I'm using GLFW 3.3.2 with visual studio. I wonder if this problem is caused by version mismatch. – Apple Popsicle Apr 15 '20 at 21:45
  • @ApplePopsicle Sadly my solution does not work for you, because your hardware does not support the extension `EXT_shader_framebuffer_fetch`. See [How to enable OpenGL extension GL_EXT_shader_framebuffer_fetch](https://stackoverflow.com/questions/40912060/how-to-enable-opengl-extension-gl-ext-shader-framebuffer-fetch-on-android) – Rabbid76 Apr 15 '20 at 21:47
  • 1
    In practice this extension will only be reliably available on mobile devices. Render-to-texture is a better plan for desktop. – Andrea Jan 06 '21 at 10:57