Using jpg as alpha channel


(Max1) #1

Hey,

I tried to use a jpg image instead of an alpha channel in one of my shaders. I actually found a way to achieve this and i got some good results, but the bad side is that i need 4 stages. Does someone maybe know a more efficent way? In case someone is interested how i did it here is the shader:

textures/minigame_2/jpg_alpha
{
qer_editorimage textures/minigame_2/blendfunc_test
{
map $whiteimage
blendfunc gl_one_minus_dst_color GL_ZERO
}
{
map textures/minigame_2/test_texture.jpg
blendfunc gl_one gl_one
}
{
map textures/minigame_2/test_alpha.jpg
blendfunc gl_one_minus_dst_color gl_zero
}
{
map textures/minigame_2/test_texture.jpg
blendfunc gl_one gl_one
}
}

The mathematical idea behind the shader is the following:

little Substitution:

1 = $whiteimage
D = Destination
T = Texture
A = Alpha

First stage gives:

1*(1-D) + 0

The second stage just adds the texture:

T + (1-D)

The third stage does this:

A*(1-(T+1-D)) + 0

So as the final result we get:

T + A*(D-T)

which is equal to good old gl_one_minus_src_ alpha gl_src_alpha but with a jpg texture instead of alpha channel:

T*(1-A) + D*A

One problem i could think of is that if a stage creates color values above 1 the value will be written to framebuffer as 1. (I’m not quite sure about this point, maybe some experts here?)

Best regards,

Max1


(Krischan) #2

Hmm old thread but I think I need to dig this out because I have a solution for this. I had the problem that I’m using large (512x512) WW2 Posters with worn edges and I need to fade them the sides to look more realistic. One Solution is a 32bit TGA with alpha channel but this increased the PK3 size too much. So I experimented with the image in JPG format and the alphamap as an external TGA with alpha channel. The images must be in this condition:

  • JPG texture image RGB 24bit with blended (Multiply Filter) alphamap on top!
  • TGA alphamap image RGB 32bit, filled with black (0,0,0) and alphachannel with the corresponding alphamap

It is very important that the alphamap blending of the texture matches the used one - so all my posters use the same alphamap but this is ok. Here is the shader I am using and it seems to work (I am not an expert in shader programming but looks the way I like and uses only two render passes and small images).

textures/ffm_poster/verdunkeln
{
nocompress

{
	clampmap textures/ffm_poster/poster_alpha.tga
	blendFunc GL_ZERO GL_ONE_MINUS_SRC_ALPHA
}

{
	map textures/ffm_poster/verdunkeln.jpg
	blendFunc GL_SRC_ALPHA GL_ONE
	rgbGen vertex
}

}

See the screenshot and the included ZIP file here. Comments welcome.