[PD] GEM: writing an arbitrary blend function (glsl)?

Matteo Sisti Sette matteosistisette at gmail.com
Fri Dec 25 20:45:59 CET 2009


Hi,

Short version:
I was wondering if there is a way to write an arbitrary blending 
function in glsl. I see I can write the function that computes the 
texture's "source color", which is then blended (or not) with the 
"destination color" (i.e. the background) using the blend function. But 
can I override the blending function with an arbitrary one written in 
glsl in some way?


Long version:

I have some rectangles with textures (most of which come from 
[pix_video]) that are being rendered over a background (which is another 
rectangle with a texture).

I'm trying to have the front rectangleS (which could be indeed any 
objects) blend with the background in some of the most "classical" 
blending modes such as: add, multiply, subtract, difference.

Also, it would be fine to experiment with arbitrary or more complex 
blending modes relatively simple to describe in terms of component-wise 
math operations with the source and destination colors.

The blending mode is the same for all objects, i.e., I don't need to 
have some objects blend in a given mode and others in another mode 
(though now that I think about it, it shouldn't add much to the 
difficulty, but I don't need it).

By using [gemGLBlendFunc] I have already been able to reproduce the 
"add" and "multiply" blending modes, by using appropriate source and 
destination blend factors.

Now I've discovered the glBlendEquation which should permit me to obtain 
the "subtract" blending mode. But not "difference" which actually means 
the absolute value of the difference. (also I'm not sure whether I can 
use glBlendEquation since it requires arb imaging which I don't know 
what it is)

So, I was thinking to use glsl, with which I am not very skilled to use 
a generous euphemism, but this thing I'm trying to do sounds pretty simple.
I've already played around with the examples (namely example 
"05.multitexture") and managed to mix two textures with any desired 
blending function.
But what I want to do is not blend 2 textures, it is blend the single 
texture with whatever is behind.

Basically, in the mentioned example you compute a resulting gl_FracColor 
which is what is called the "source color". Then the source color is 
blended with the "destination color" (the background) with a blending 
function determined by glBlendFunction, while I would like to write an 
alternative blend function which combines source color and destination 
color the way I want to.

Is it possible in glsl? Can anybody point me to some example code?

By the way, I know I could use a gemframebuffer to obtain a texture from 
the scene, so that I can use the multitexture example to belnd the two 
textures, but I'd try to avoid this workaround if possible.

Thanks in advance.
Hope I have explained myself...


-- 
Matteo Sisti Sette
matteosistisette at gmail.com
http://www.matteosistisette.com




More information about the Pd-list mailing list