Blitting all but one color in OpenGL

Post: #1
I am relatively new to OpenGL, my previous games have all used good ol' CopyBits, but I decided to take the plunge and use OpenGL for my current project. Its a simple 2D side-scroller, basically just textured quads. I have it going nicely, but I can't figure out quite how to duplicate the CopyBits functionality that lets you blit all but pixels of a certain color.

Essentially I just want to not copy the black (or any other specified color) pixels, while blitting everything else at its normal color.

I have been messing around with blend modes for the past couple hours, but I can't seem to get one that will do what I want. Either everything is blitted, or it becomes translucent.

Whats the simplest and fastest way to do this? (I would prefer not having to resort to masks)

Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #2
The best way is to use a proper alpha channel. That way you can get the edges of the sprite looking right even in the face of texture filtering.

I don't know how to do what you're asking without a texture shader. I guess NV_register_combiners might be enough, but then you exclude all ATI cards...
Quote this message in a reply
Post: #3
So, how would I use a proper alpha channel in this case?

Quote this message in a reply
Post: #4
Have a look at the Color Keying demo at

I think all he does is generate an alpha channel (mask) on the fly though.
Quote this message in a reply
Posts: 79
Joined: 2002.08
Post: #5
You can use

glAlphaFunc( GL_GREATER, 0.3 );
glEnable( GL_ALPHA_TEST );

and have an alpha channel in your sprite textures. In the above example everything greater than 0.3 in the alpha channel is drawn on screen. Usually the alpha channel pixels are either 0 or 1 (0 or 255 with an 8 bit channel) so the value is not critical.

Quote this message in a reply
Post: #6
Thanks, doing an alpha test is quite simple, can't see why I didn't notice it before.

Working great now, thanks.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  iPhone OpenGL color bug agreendev 8 11,235 Aug 12, 2010 08:20 PM
Last Post: AnotherJake
  ? Find color value of 'pixel' in color buffer? Elphaba 1 6,262 Jul 22, 2009 01:23 PM
Last Post: Bachus
  OpenGL Color Buffer Overwrite Talyn 8 9,095 Jan 4, 2009 02:44 AM
Last Post: papillon68
  SDL Blitting KidTsunami 5 9,672 May 8, 2008 08:17 AM
Last Post: leRiCl
  OpenGL Color map nkpkd 4 6,228 Mar 30, 2007 10:47 AM
Last Post: nkpkd