Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: ninogenio on December 22, 2006
-
hey folks im having a spot of bother im blitting a 2d image on my screen and that goes fine but when i try to mask the image as the backgrounds black using gls alpha funcs the image looks washed out heres what im trying
sub blit_image( texture as GLuint , byval x as double , byval y as double , byval gfx_width as integer , byval gfx_height as integer )
glenable GL_TEXTURE_2D
glBindTexture GL_TEXTURE_2D,texture
GLENABLE GL_BLEND
'GLENABLE GL_ALPHA_TEST
'GLALPHAFUNC GL_LESS, 1
glBlendFunc( GL_SRC_COLOR , GL_ONE_MINUS_SRC_ALPHA )
'glDisable GL_LIGHTING
glDisable GL_DEPTH_TEST
glMatrixMode GL_PROJECTION
glPushMatrix
glLoadIdentity
glOrtho 0, sWIDTH, 0, sHEIGHT,-1, 1
glMatrixMode GL_MODELVIEW
glPushMatrix
glLoadIdentity
glBegin GL_QUADS
glcolor4ub 255,255,255,10
glTexCoord2f 0.0 , 0.0 : glVertex2f x, y
'glcolor4ub 0,0,0,255
glTexCoord2f 1.0 , 0.0 : glVertex2f x+gfx_width, y
'glcolor4ub 0,0,0,255
glTexCoord2f 1.0 , 1.0 : glVertex2f x+gfx_width, y+gfx_height
'glcolor4ub 0,0,0,255
glTexCoord2f 0.0 , 1.0 : glvertex2f x, y+gfx_height
glEnd
glMatrixMode GL_PROJECTION
glPopMatrix
glMatrixMode GL_MODELVIEW
glPopMatrix
glEnable GL_DEPTH_TEST
gldisable GL_TEXTURE_2D
'GLDISABLE GL_ALPHA_TEST
'GLALPHAFUNC GL_GREATER, 0.1
GLDISABLE GL_BLEND
end sub
-
ohh yeah forgot to say im setting an alpha value in my image loader of 255 when r g b = 0 and 0 on an else case.
-
Looks OK, what's the problem? Sorry, I don't understand.
Jim
-
Using blending like that I think you're modulating the source color value by itself which would cause all but the brightest colors to wash out, you could maybe try:
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA,GL_SRC_ALPHA)
or try it without the blending but using the alpha test:
glAlphaFunc(GL_NOTEQUAL,255)
glEnable(GL_ALPHA_TEST)
-
@stonemonkey cheers mate but that doesnt work either no image is displeyd or it still gets washed out.
@jim im simply just trying to get rid of the black pixels in my image withoutalphaing the other pixels just really a mask like in 2d.
-
both:
glEnable(GL_BLEND)
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA,GL_SRC_ALPHA)
and
glenable(GL_ALPHA_TEST)
glalphafunc(GL_EQUAL,0) or glalphafunc(GL_NOTEQUAL,255)
work for me, but use glcolor4ub 255,255,255,255 as it appears that the color (including the alpha value) is modulated before the alpha test is done.
EDIT: this is maybe still not right, I'll look into this some more.
-
Normal blending is
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
Source pixel is multiplied by source alpha, dest pixel is multiplied by (1-source alpha) and then they're added together.
You're right, the alpha compare is done at the very end.
Jim
-
The order of the tokens in the blendfunc is dependant on whether high/low alpha values are used for opaque/transparent and that seems to work fine, the thing i've noticed that's confusing me is that using the alpha test with GL_NOTEQUAL the ref value can be anything non zero (1-255) and it still seems to work even though I'm only writing either 0 to 255 into the texture so why is it still masking with glAlphaFunc(GL_NOTEQUAL,10) when that should always pass.
EDIT: Doh! ref is a value between 0.0 and 1.0 and is clamped so it's working as it should:
glEnable(GL_BLEND)
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA,GL_SRC_ALPHA)
or
glenable(GL_ALPHA_TEST)
glalphafunc(GL_EQUAL,0) or glalphafunc(GL_NOTEQUAL,1)
should both work.
-
doh it was a stupid mistake on my behalf in the image loader :) thanks though guys.
btw this was the one that worked for me simply because of the way i load my textures.
glAlphaFunc(GL_EQUAL,1)
glEnable(GL_ALPHA_TEST)
-
The blendfunc that Jim gave should work too then and might be better depending if you'd want to do some blending at the edges to smooth them out.
-
The reason I use SRC,1-SRC is because when you texturemap you normally have alpha set to 0xff where it is opaque and 0x00 where it's transparent. If you do it the other way round (0x00 opaque) then you might run into problems later.
It's a shame you're not coding in C or C++, with the warnings set high enough it might have noticed that you passed (int)10 to a function that takes (float) in glAlphaFunc.
Jim