GL_ARB_texture_env_combine

Any ideas how could I implement the following fragment code using GL_ARB_texture_env_combine?

!!ARBfp1.0										
#
# Composite 
#

TEMP    tex,newc;

ATTRIB  clr = fragment.color; 
TEX tex, fragment.texcoord, texture, 2D;

MUL newc,clr,tex;

MUL newc.r,newc.r,newc.a;
MUL newc.g,newc.g,newc.a;
MUL newc.b,newc.b,newc.a;

MOV result.color,newc;
				
END

The reason is that if I use the fragment code above it takes much more time to render one image (It is about 2.5 times slower). And I’m having a hard time trying to understand this OpenGL extension…

First of all, try using this shader:

!!ARBfp1.0
OPTION ARB_precision_hint_fastest;

TEMP tex;

ATTRIB clr = fragment.color;

TEX tex, fragment.texcoord, texture, 2D;
MUL tex, clr, tex;
MUL result.color.rgb, tex, tex.a;
END

This must be the fastest… It does the same you wrote, but it involves only one temporate register, and has few instructions.

Anyway, you could do the same thing without any shaders.

Do you have blending turned on? Turn it on with glEnable(GL_BLEND);

If these fragments must be transparent, simply set it up to additive mode with glBlendFunc(GL_SRC_ALPHA, GL_ONE).
If you wish not to have transparency, set it up to glBlendFunc(GL_SRC_ALPHA, GL_ZERO).

Texture env mode must be set to glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE).

By the way, do you have nVidia hardware? Cause if it is so, shader may be done a little bit faster ))

Well, it didn’t work but I modified my code:

!!ARBfp1.0				
OPTION ARB_precision_hint_fastest;
TEMP    tex,newc;
ATTRIB  clr = fragment.color; 
TEX tex, fragment.texcoord, texture, 2D;

MUL newc,clr,tex;
MUL result.color,newc,newc.a;
MOV result.color.a,newc.a;
END

So it is a little bit faster. When I use
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

by itself is much faster (for back-to-front rendering), but it doesn’t work for front-to-back. My current solution is:

B2F:
glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE);

F2B:
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

then I have to pre-multiply the color to the alpha. However, it doesn’t work like that; if I multiply color*alpha in my texture I think I lose precision, and I get some patches of white.

Anyway, I think that if I use the GL_COMBINE I may get what I want faster…