formulas for "lightblend and alphablend

BlitzMax Forums/BlitzMax Programming/formulas for "lightblend and alphablend

Najdorf(Posted 2005) [#1]
Do you know exactly the formulas used to calculate the color of pixels resulting? here are my guesses:

Bacground color: (r1,g1,b1)
Object color: (r2,g2,b2)
Object alpha=a

resulting object color in alphablend=( (1-a)*r1+a*r2, (1-a)*g1+a*g2, (1-a)*b1+a*b2)

resulting object color in lightblend=( r1+a*r2, g1+a*g2, b1+a*b2)

Are these correct?


Another question: how do you make so that, when compiling, blitz inserts the pictures in the exe?


Vertex(Posted 2005) [#2]
http://www.mevis.de/~uwe/opengl/glAlphaFunc.html
http://www.mevis.de/~uwe/opengl/glBlendFunc.html

	Method SetBlend( blend )
		If blend=state_blend Return
		state_blend=blend
		Select blend
		Case MASKBLEND
			glDisable GL_BLEND
			glEnable GL_ALPHA_TEST
			glAlphaFunc GL_GEQUAL,.5
		Case SOLIDBLEND
			glDisable GL_BLEND
			glDisable GL_ALPHA_TEST
		Case ALPHABLEND
			glEnable GL_BLEND
			glBlendFunc GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA
			glDisable GL_ALPHA_TEST
		Case LIGHTBLEND
			glEnable GL_BLEND
			glBlendFunc GL_SRC_ALPHA,GL_ONE
			glDisable GL_ALPHA_TEST
		Case SHADEBLEND
			glEnable GL_BLEND
			glBlendFunc GL_DST_COLOR,GL_ZERO
			glDisable GL_ALPHA_TEST
		Default
			glDisable GL_BLEND
			glDisable GL_ALPHA_TEST
		End Select
	End Method


cu olli


Tibit(Posted 2005) [#3]
Another question: how do you make so that, when compiling, blitz inserts the pictures in the exe?


You use Incbin "ImageFolder/Filename.png" at the start of your file. Do this for every picture, sound, map, whatever.

Then when you load the images/whatever,
Car:TImage = Loadimage("incbin::ImageFolder/Filename.png")


Najdorf(Posted 2005) [#4]
thx


ImaginaryHuman(Posted 2005) [#5]
Lightblend has a couple of steps involved. For each pixel of the source image, the value of the pixel is scaled according to the alpha value which can be from 0.0 to 1.0. The pixel color is multiplied by the alpha value, from 0.0 to 1.0. So when the alpha is $FF (which is 1.0 in floating point), you see the image `as is`. When the alpha is less than 1.0, the image gets progressively darker down towards black, which you can interpret to mean it gets progressively fainter and more transparent. Usually you will have an alpha of 1.0 which I think BlitzMax sets up behind the scenes by default. The destination pixel value is then multiplied by 1.0 because of the GL_ONE mode, so it stays as is. Then, after these scaled source and destination values have been calculated, the source value is added to the value of the destination with a simple addition ie source+dest. If that final value is more than 1.0 it gets cropped at 1.0, so in other words it maxes out at white where the two pixels combine to make a value that is too high to be stored. So the overall formulae should be Min(1.0,(Source*Alpha)+Dest).

With alphablend a similar thing happens except that rather than keep the destination `as is` before the addition, it is multiplied by a value which is derived from the simple sum 1.0-SourceAlphaValue. This is done before the source and destination values are added together. The effect is that as the source image becomes progressively more transparent, the destination image contributes more and more to the overall color. And vice versa, as the source value becomes more solid, there is less of the destination color contributed. It is inversely proportional. Again, after the addition, if the value is greater than 1.0 it is cropped to 1.0, but it shouldn't because both source and destination colors are scaled so you only ever get a 100% shared color.
The formulae should be Min(1.0,(Source*Alpha)+(Dest*(1.0-Alpha))).

I hope I got that right.

GL_SRC_ALPHA means that the source pixel value is multiplied, per component individually, by the value of the source alpha.

GL_ONE means keep the value of the destination pixel.

GL_ONE_MINUS_SRC_ALPHA means that the destination pixel is multiplied by the inversed source alpha value.

So Lightblend is basically an `add the two pixels together` mode, and alphablend is an `add the two pixels together after changing how much each pixel contributes to the overall total by multiplying them by alpha and inverted alpha values.

I hope that helps.


Najdorf(Posted 2005) [#6]
hey thx! So I forgot the Min stuff in my formulas.

when one sees a transparency for the first time, it really looks magic and one wonders how the hell that works. Then one discovers that there is just simple pixel math behind it ;-)