Opacity Map Problem

Hey. I gave up and askin for help here for the 1st time. I’m stuck implementing opacity map without GLSL(with GLSL it took about 2.5 seconds). Because i just don’t get opengl blending or something is not working.

http://www.siggraph.org/education/materi…opacity_map.jpg

Here’s just a random picture from google of what i’m trying to do.
2 Textures. 2 glActiveTextureUnits. 1st - Diffuse map. 2nd - Opacity Map. FragmentColor.a = (OpacityMap.r + OpacityMap.g + OpacityMap.b)*0.3333;

Here’s some info about how i’ve tried to do it:

//tried on\off glLighting
//glFog Disabled


glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //Tried Almost Every Combination

//Some Stuff...
glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, DTxO);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);//GL_REPLACE/GL_MODULATE//GL_DECAL - did not help;

glActiveTextureARB(GL_TEXTURE1_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, ATxO);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);

//RENDER

Also tried different glTexEnvi combinations. I really spend most time just trying to guess right values. Because i can’t see any logic here and Google\RTFM did not help(or maybe wrong keywords), got only color-key examples or some totally unrelated topics
on-working code(total bs that worked accidentally because of bug’s in author’s application). Also looked at TextureCombiners, but not sure they can help.

If you use GL_BLEND and glBlendFunc you need to draw sorted vertices from back to front. But I guess you already did that… just a guess from my limited knowledge.
And welcome to the Forums.

It’s not a game. OpenGL used for GUI. So yep, elements are kinda manually sorted(at least with GLSL they look perfect). And it doesn’t look like problem with sorting. Because every element is opaque\messed up with different combinations. Code given in 1st post then executed, looks like inversed alpha-texture replaces diffuse-map.

If you already have a shader that works, post the shader and someone can convert that into texture environment calls.

BTW, what level of hardware are you trying to make this run on? GL 1.4? 1.3? What extensions are available?

2Alfonse Reinheart
The perfect goal is to make it run even on GF3 Ti\Radeon 8500 (GL 1.3, according to wiki).

uniform sampler2D colorMap;
uniform sampler2D alphaMap;
uniform float AbsAlpha;

void main(void)
{
vec4 Color = texture2D( colorMap, gl_TexCoord[0].st);
vec4 Alpha = texture2D( alphaMap, gl_TexCoord[0].st);

Color.a = (Alpha.r + Alpha.g + Alpha.b) * 0.33333 * AbsAlpha;
gl_FragColor = Color;
}

That’s how my shader will look only with opacity-map processing. AbsAlpha - overall object visibility 0.0 - 1.0. Not necessary.

Is there some reason why the alpha texture doesn’t just store the average alpha as a single value (for hardware this old, GL_INTENSITY8 works fine), rather than 3? Or why the RGB components are different if all you’re going to do is average them together?

That’s where your problem comes from: doing the average of the alpha. You can’t use DOT3_RGB, because it will assume that you’ve given it signed-normalized values instead of unsigned normalized, which will screw up the average you’re trying to compute.

If that was just a texture access, this would be a simple matter of using tex_env_combine. Even with the “AbsAlpha”, it’d just be multiplying the texture’s alpha by a constant texenv color, then putting that in the alpha component. It’d be simple.

But so long as that averaging is in there, you can’t do it and remain platform-agnostic. You can do it with NV_register_combiner, supported on GeForce3’s and 4’s. And you can do it with ATI_fragment_shader. But there’s no platform-neutral way to do it.

I think, i can make it a single value(cuz i rally don’t care, it was just fast way), if i’ll get working solution for single-valued non-boolean alpha. All i need is to switch from RGB to RGBA TGA and couple minor changes in code and fun time converting textures.

Mmkay, that was surprisingly straight-forward, i’ve just made texture with alpha, fixed glTexImage2D for possible alpha channel, set GL_BLEND Env mode for diffuse map(that has alpha channel), enabled glLighting and it works with standart BlendFunc(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) );

But i don’t think it is a right way. Because there are some bugs. For example, with some objects transparency is working, but they become very dark, almost black. With some objects it doesn’t work at all. Some objects are working correctly. So i’m not sure where’s my mistake. In blending implementation or other code.

Upd: Alpha works, but it makes RGB inverted(Same textures with GLSL work just fine).