Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: Clyde on October 17, 2012

Title: [ogl] Omitting Black Pixels
Post by: Clyde on October 17, 2012
Hi.


I really need a hand with making a texture on an object to not display black pixels / opaque
I don't want any colour blending, just for the background / mask colour to be invisible. The only sort of way to do it is to use glBlend, but that will blend. The aim is to display a logo on a quad, but it looks very lame with the background colour showing up.


The code is in freebasic format, but is gl related. Apologies for the slight mess, have been head scratching and trying different things for weeks for ideas on how to do it. Maybe there is something in the setting up of the texture, or it's the blend function types. There's quite a selection up gl's sleeves. It also uses 8 bit images and converts to 32 bit, have done this so later ron the exe size isn't so large when using bmp2raw.


Hugest Of Thanks,
Clyde.
Title: Re: [ogl] Omitting Black Pixels
Post by: zawran on October 17, 2012
Would it not be easier to use .png image and make use of the alpha channel? .bmp does not have any alpha information stored, so with those you will have to mess with some kind of masking, but with .png you do not, you simply upload the texture and make sure that you have setup opengl to use alpha blending.

I do no longer have freebasic available, have not used it for a few years, so I am unable to assist with your actual code. The above is just a suggestion as to what might make it easier to work with.
Title: Re: [ogl] Omitting Black Pixels
Post by: Raizor on October 17, 2012
Clyde, what zawran suggests should do the trick. You could use a PNG or manually process the pixel data in memory and set the alpha for the black pixels to 0. If you're working with 32bit buffers, you should have alpha channel available to play with. Shaders would make this pretty easy to do, but you mentioned before you've not used them in Freebasic yet.

OpenGL Stencil Buffers may also do the trick, it's worth having a read up on them. I'll try and put together a freebasic example using shaders for blur and a few other bits in the near future. I'm tied up for the next couple of weeks but should have time after that.
Title: Re: [ogl] Omitting Black Pixels
Post by: Clyde on October 17, 2012
Thanks for the replies guys!! I guess Im too old for coding.

I've only just worked out how to load 8bit bitmaps from a file. And it's the method I'm most familar with, as it makes small exe's when seperated into ubytes with rbz's bmp2raw / bin2bas. As these are going to be loaded from memory eventually; I had to make investigate in a loader, as I've lost a huge amount of my graphics stuff.

I want to stick with 8 bits for the moment, and I'm adding the alpha ontop when it's being generated. Unless that is tits up as well.

I can load shaders and glUseProgram them, I just don't know how to design or find any of them that work for pixel shaders 2.

Anyhows, thanks for the suggestions.
Title: Re: [ogl] Omitting Black Pixels
Post by: Jim on October 18, 2012
If you're using 8bit palette stuff, make the alpha component of the transparent colour = 0 in the palette before you load the texture, and make all the others = 255
Then enable alpha blending.  The default mode is the one you want, so no need to change anything.  You don't need shaders.
Jim
Title: Re: [ogl] Omitting Black Pixels
Post by: Clyde on October 18, 2012
In my code above this is the part that reads in the palette.
it confuses me as usually in basic languages it would be: a r g b

Code: [Select]
        image_data->pal     =new ubyte [256*4]

        for a=0 to 255
            image_data->pal[a*4+3]=255
        next


        if ( info_header.biBitCount=8 ) then
           
            '============
            ' pal power.
            '============
            for a as integer=0 to 255
                   
                get #file_data, ,bmp_palette
               
                image_data->pal[a*4+0]=( bmp_palette shr 16 ) ' red
                image_data->pal[a*4+1]=( bmp_palette shr  8 ) ' grn
                image_data->pal[a*4+2]=( bmp_palette shr  0 ) ' blu
                image_data->pal[a*4+3]=255'0'&hff             ' alpha
           
            next

If I set all of the red, green and blue parts to 255 wont they be all white?
And by enable blending, do you mean glBlend() like i am doing in the draw quad sub, or in the texture setting.

    glTexImage2D    ( GL_TEXTURE_2D,_
                      0,_
                      GL_RGBA,_
                      wwidth,_
                      height,_
                      0,_
                      GL_BGRA_EXT,_'RGBA,_
                      GL_UNSIGNED_BYTE,_
                      @pixels[0] )

I also read somewhere while using gooogle that a geforce 7 card can't do 32 bit textures. Is that nonsense?

Thanks.
Title: Re: [ogl] Omitting Black Pixels
Post by: staticgerbil on October 18, 2012
Hi Clyde,

I think Jim means to set the alpha component of black pixels to 0 and then set the alpha component of any other pixels to 255.

That way when you enable blending with glEnable(GL_BLEND) the pixels you have marked with a 0 value for the alpha channel won't display but the others will.

Hope that helps :)
Title: Re: [ogl] Omitting Black Pixels
Post by: hellfire on October 18, 2012
I also read somewhere that a geforce 7 card can't do 32 bit textures. Is that nonsense?
They were probably talking about 32bit per color channel (float r,g,b,a) which has some restrictions.
Title: Re: [ogl] Omitting Black Pixels
Post by: Jim on October 18, 2012
Clyde, let's say it is colour 10 in your palette that needs to be transparent
Code: [Select]
for a as integer=0 to 255
                   
                get #file_data, ,bmp_palette
               
                image_data->pal[a*4+0]=( bmp_palette shr 16 ) ' red
                image_data->pal[a*4+1]=( bmp_palette shr  8 ) ' grn
                image_data->pal[a*4+2]=( bmp_palette shr  0 ) ' blu
                image_data->pal[a*4+3]=255
           
next

'set colour 10 to be the transparent one
image_data->pal[10*4+3]=0


Then you're looking for
Code: [Select]
glEnable(GL_BLEND)
glBlendFund(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

Then you can do even more cool stuff - let's say you wanted colour 5 to be 50% transparent
Code: [Select]
image_data->pal[5*4+3]=127

Jim
Title: Re: [ogl] Omitting Black Pixels
Post by: Clyde on October 18, 2012
Greatest and Thanks Jim.

I added the following to the mix now

Code: [Select]
if ( image_data->pal[a*4+0]=0 ) and ( image_data->pal[a*4+1]=0 ) and ( image_data->pal[a*4+2]=0 ) then
     image_data->pal[a*4+3]=0
else
     image_data->pal[a*4+3]=255
end if


Cheers!!
Title: Re: [ogl] Omitting Black Pixels
Post by: Clyde on October 21, 2012
I've noticed a strange thing with this, and don't know if it's curable, but when I rotate an object at certain intervals the colours that are supposed to be invisible are ignored.

And if I have an object behind another object, this technique doesn't work, and anything black will be shown.
Title: Re: [ogl] Omitting Black Pixels
Post by: hellfire on October 21, 2012
And if I have an object behind another object, this technique doesn't work, and anything black will be shown.
That's because all pixels are stored in the depth-buffer and will block polygons which are further away.
Enable the alpha-test function. All pixel which don't pass the alpha-test won't be written to the depth-buffer.



Title: Re: [ogl] Omitting Black Pixels
Post by: relsoft on October 22, 2012
Yeah, enable alpha testing like hellfire said.  Jim's also works. Here's an example of it ;*)

http://rel.phatcode.net/junk.php?id=115

PS. Include "glext.bi" in the header.

Title: Re: [ogl] Omitting Black Pixels
Post by: Clyde on October 22, 2012
Great Success with the transparency!! I also needed glAlphaFunc(GL_GREATER, 0.0f )

@Relsoft: when I open the example sources it sayz implicit variable allocation to anything with FB dot
Title: Re: [ogl] Omitting Black Pixels
Post by: Clyde on October 24, 2012
Any ideas why with the Alpha Test stuffs the textures are much darker than they are normally? ???

Code: [Select]
glEnable   ( GL_ALPHA_TEST )
 glEnable   ( GL_BLEND )
 glBlendFunc( GL_SRC_COLOR, GL_ONE_MINUS_SRC_ALPHA )
 glAlphaFunc( GL_GREATER, 0.0f )

I've tried to add a snapshot of what's a foot, but it the screenie is a pure black image. Which is a bit wierd. Never happend to me before.

Muchos Gracias,
Clyde.
Title: Re: [ogl] Omitting Black Pixels
Post by: hellfire on October 24, 2012
Any ideas why with the Alpha Test stuffs the textures are much darker than they are normally? ???
Because with the first parameter of glBlendFunc (GL_SRC_COLOR) you told the blending to multiply the source color with the source color (which doesn't make any sense and basically increases contrast).
You want to use GL_ONE or GL_SRC_ALPHA instead.
Note that blending and alpha-test are completely separate things.