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 escribió:
Basically, in the mentioned example you compute a resulting gl_FracColor which is what is called the "source color".
Well well, I am far from sure this is correct, this is just my understanding, and I don't even know whether for example lighting information is applied before or after this stage, but in the simplified case of a flat rectangle with no lighting, that becomes irrelevant...
You can do arbitrary blend modes in PD with GLSL as you suspect. take
a look at http://001.vade.info/?page_id=20 which has shaders for video
mixers (a through a+b to b blending, not just a+b) with most
photoshop blend modes.
You can use them verbatim with PD (Marius Schebella has some patches I
think).
Feel free to use them as you wish, or hack them apart :)
On Dec 25, 2009, at 3:07 PM, Matteo Sisti Sette wrote:
Matteo Sisti Sette escribió:
Basically, in the mentioned example you compute a resulting
gl_FracColor which is what is called the "source color".Well well, I am far from sure this is correct, this is just my
understanding, and I don't even know whether for example lighting
information is applied before or after this stage, but in the
simplified case of a flat rectangle with no lighting, that becomes
irrelevant...-- Matteo Sisti Sette matteosistisette@gmail.com http://www.matteosistisette.com
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
vade escribió:
You can do arbitrary blend modes in PD with GLSL as you suspect. take a look at http://001.vade.info/?page_id=20 which has shaders for video mixers (a through a+b to b blending, not just a+b) with most photoshop blend modes.
Hi,
I just had a very quick look, but it seems to me that your mixers use 2 textures, very much like the "multi texture" example I mentioned.
To use them, you need two textures and you obtain the blended picture.
I was looking for a way to replace the blend function, the very same function whose parameters you can modify using glBlendFunc and glBlendEquation.
In order to use a two-texture mixer for my purpose (which is to change how the object blends with the background), I would need to repatch everything so that I snap the scene into a texture before blending. In my case it would be quite a radical repatching.
However, after a little bit of googling, it seems that what I would like to do is simply impossible, isn't it? If I understand correctly, with glsl one can "only" modify the vertex and fragment transformations, while the blending phase cannot be modified. Is it correct?
If so, is there a way I could do "difference" blending, that is the absolute value of the difference between source and destination colors? I have already done additive and multiplicative blending with glBlendFunc alone, and I should be able to do subtractive with glBlendEquation... can anybody suggest a way to get the absolute value?
Thanks m.
Ah, sorry, I had missed that you wanted to use fixed function and not
require more than one texture. I can tell you that you cannot get all
of the arbitrary blend modes using just glBlendFunc and
glBlendEquation. GLSL is infinitely more flexible.
I would highly suggest doing a render to texture so you can post
process your scene or mix with multi-textures. You can also do fun
things like full scene blur, etc. Its how almost all modern games and
graphics systems work these days, using render to texture and post
processing pipelines and lets you do lots of things 'on top' of your
fixed function rendering if you decide you want to add something else
to your scene.
As for difference, Im pretty sure you *can* do that blend mode, but I
have not done it myself, sorry I cant be more helpful.
On Dec 26, 2009, at 5:18 AM, Matteo Sisti Sette wrote:
vade escribió:
You can do arbitrary blend modes in PD with GLSL as you suspect.
take a look at http://001.vade.info/?page_id=20 which has shaders
for video mixers (a through a+b to b blending, not just a+b) with
most photoshop blend modes.Hi,
I just had a very quick look, but it seems to me that your mixers
use 2 textures, very much like the "multi texture" example I
mentioned.To use them, you need two textures and you obtain the blended picture.
I was looking for a way to replace the blend function, the very same
function whose parameters you can modify using glBlendFunc and
glBlendEquation.In order to use a two-texture mixer for my purpose (which is to
change how the object blends with the background), I would need to
repatch everything so that I snap the scene into a texture before
blending. In my case it would be quite a radical repatching.However, after a little bit of googling, it seems that what I would
like to do is simply impossible, isn't it? If I understand
correctly, with glsl one can "only" modify the vertex and fragment
transformations, while the blending phase cannot be modified. Is it
correct?If so, is there a way I could do "difference" blending, that is the
absolute value of the difference between source and destination
colors? I have already done additive and multiplicative blending
with glBlendFunc alone, and I should be able to do subtractive with
glBlendEquation... can anybody suggest a way to get the absolute
value?Thanks m.
-- Matteo Sisti Sette matteosistisette@gmail.com http://www.matteosistisette.com
vade escribió:
I would highly suggest doing a render to texture so you can post process your scene or mix with multi-textures. You can also do fun things
Sure, it's a pity that I haven't done it like that from the beginning, but now, the patch is pretty complex (and already has a few gemframebuffers) and that would represent a serious re-patching.
As for difference, Im pretty sure you *can* do that blend mode,
Yeah, I only need the most basic blend modes for this application, and indeed the only one I haven't found how to achieve is the so-called "difference" (i.e. abs(source-dest)) and I almost can't believe I can't do it without rendering to textures.