Author Topic: image masking in ogl  (Read 4689 times)

0 Members and 1 Guest are viewing this topic.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
image masking in ogl
« 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

Code: [Select]
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
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: image masking in ogl
« Reply #1 on: December 22, 2006 »
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.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: image masking in ogl
« Reply #2 on: December 22, 2006 »
Looks OK, what's the problem?  Sorry, I don't understand.

Jim
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: image masking in ogl
« Reply #3 on: December 22, 2006 »
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:

Code: [Select]
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA,GL_SRC_ALPHA)

or try it without the blending but using the alpha test:

Code: [Select]
glAlphaFunc(GL_NOTEQUAL,255)
glEnable(GL_ALPHA_TEST)
« Last Edit: December 22, 2006 by Stonemonkey »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: image masking in ogl
« Reply #4 on: December 22, 2006 »
@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.
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: image masking in ogl
« Reply #5 on: December 23, 2006 »
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.
« Last Edit: December 23, 2006 by Stonemonkey »

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: image masking in ogl
« Reply #6 on: December 23, 2006 »
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
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: image masking in ogl
« Reply #7 on: December 23, 2006 »
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.
« Last Edit: December 23, 2006 by Stonemonkey »

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: image masking in ogl
« Reply #8 on: December 23, 2006 »
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.

Code: [Select]
glAlphaFunc(GL_EQUAL,1)
glEnable(GL_ALPHA_TEST)
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: image masking in ogl
« Reply #9 on: December 23, 2006 »
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.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: image masking in ogl
« Reply #10 on: December 23, 2006 »
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
Challenge Trophies Won: