masked image and shaded blend doesnt work?

BlitzMax Forums/BlitzMax Beginners Area/masked image and shaded blend doesnt work?

Nate the Great(Posted 2009) [#1]
hi

I have been trying forever to get a masked image to shade blend correctly but it always ends up choosing to multiply the black pixels. does anyone know why this is or a way around this?


ImaginaryHuman(Posted 2009) [#2]
MASKBLEND doesn't work at the same time as SHADEBLEND, but there is a way to make it work...

Here is some code from glmax2d.mod outlining how it works out what to do in the various blend modes:

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

In masked blend, your sprite's alpha channel values have to be 128 or higher (0.5 to 1.0) in order to be drawn. Blending is also switched off so it will absolutely not do shadeblend or lightblend or alphablend at the same time.

Alphablend multiplies the sprite's RGB values by its alpha channel values, and then adds that to the backbuffer's RGB values multiplied by the inverse of the sprite's alpha values. In alphablend you can't do lightblend or shadeblend or maskblend at the same time. To get a pixel to not be drawn from the sprite, it doesn't matter what color the pixel is so long as the alpha channel has a 0 in it.

In lightblend mode, the sprite's RGB values are multiplied by its alpha channel values - similar to alphablend, but then instead of inversely scaling the backbuffer's values, it simply adds it to the backbuffer's values - which is why the colors get lighter and lighter until reaching white. You can't do alphablend or shadeblend or maskblend while you're doing lightblend. However, you can fake masking in lightblend by setting the `do not draw` pixels to black with 0 for the alpha value - it will end up adding 0 to the destination pixel, thus not changing it, thus making it seem like you're masking at the same time. But you can't have alpha's of 255 or 128 and expect it to do that.

Shadeblend multiplies the sprite's RGBA values (including the alpha channel), by the values in the backbuffer pixels. It doesn't do any further addition, and merely replaces the backbuffer pixel with the result of this multiplication. It won't do lightblend or alphablend or maskblend at the same time. However, a white pixel in your sprite will appear to have no effect on changing the background pixels because it will be multiplying 1.0 x 1.0, which leaves the pixel as it was - giving the impression of masking. So pixels you don't want to draw with shadeblend you should set to white - it doesn't matter what the alpha values are because they aren't used.

Shadeblend always multiplies EVERY pixel, black or otherwise. But if your pixel is black, it should not change the background color.


Nate the Great(Posted 2009) [#3]
my pixel is 0,0,0 aka black so when shadeblend multiplies it by the background color, white, it goes completely black. I am trying to set the shadeblend mode while masking the completely black pixels out of the image but I still can't figure it out. thanks for the explanation though.

so maybe this should clear it up

img = loadimage("myimage.png",MASKEDIMAGE)

setblend shadeblend

drawimage img

.. thats what im trying to do but the image doesn't retain its MASK from the masked image flag when I use shadeblend


plash(Posted 2009) [#4]
Use transparency on the image instead of a mask?


Nate the Great(Posted 2009) [#5]
I dont know how to do transparency cuz I dont have anything like photoshop to do that in.. all I have is MS paint for now :p


skidracer(Posted 2009) [#6]
Have you read the docs?

I think you are confused over meaning of shadeblend (it's for subtractive (multiply) effects such as shadows).

http://blitzbasic.com/bmdocs/command.php?name=SetBlend&ref=2d_cat

If you must use shadeblend white pixels not black will leave destination unmodified.


plash(Posted 2009) [#7]
I dont know how to do transparency cuz I dont have anything like photoshop to do that in.. all I have is MS paint for now :p
GIMP.

EDIT:
@REDi: :D


REDi(Posted 2009) [#8]
I dont know how to do transparency cuz I dont have anything like photoshop to do that in.. all I have is MS paint for now :p

Use gimp its free! http://www.gimp.org/

*EDIT* DOH :)


Nate the Great(Posted 2009) [#9]
@skidracer - I know what it does I just thought that using a mask would stop it from even looking at the black pixels


ImaginaryHuman(Posted 2009) [#10]
0 multiplied by white is 0!

White pixels turn black. That is normal. Make your pixels in your sprite WHITE if you want them to do masking.


Nate the Great(Posted 2009) [#11]
oh... ok thanks IH I should have thought of that... doh!


ImaginaryHuman(Posted 2009) [#12]
As described previously ;-)

Shadeblend multiplies, lightblend adds.


ImaginaryHuman(Posted 2009) [#13]
All of the blend types - maskblend, solidblend, lightblend, alphablend, shadeblend, these are all *exclusive*, you cannot do two of them at the same time. You can't do maskblend at the same time as shadeblend, for example. But as I and Skidracer said, white pixels will have the effect that a mask would have when using shadeblend. Black pixels turn the background black. Lightblend is the reverse - black pixels act like a mask and white pixels turn it white