OpenGL and 2D Masking [Archive] - C Board

PDA

View Full Version : OpenGL and 2D Masking


dxfoo
03-08-2008, 12:08 AM
I created a 2D demo a while ago. It was pretty simple with just a sprite walking around. To make him transparent, I had to use a seperate sprite map that was black. I am assuming this is called masking. I got use to DirectDraw/Direct3D's way of doing 2D. There was no "seperate image," as it was an offscreen surface. I would just tell it not to blit a certain color to the back buffer. It did the trick. Is there anything like that in OpenGL? I'd hate having two seperate images just in hope for transparency purposes.

IdioticCreation
03-08-2008, 09:28 AM
You can use blending. If you have, say a png file with an alpha channel, then you can:

glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE );


But if the images overlap you get weird colors, so to get rid of that you have to use masking still.

I'm not really sure what I'm talking about, but I hope that was what you meant.

David

dxfoo
03-08-2008, 10:20 AM
In The Gimp, I remember you can set a transparent color to an image. If that's predetermined, and you load the image into the program, would a masking image now be unncessary?

IdioticCreation
03-08-2008, 10:33 AM
That will make the image transparent if you use blending, but to make sure the images overlap properly, you have to use masking. NeHe has some tutorials over blending and masking, if you haven't already checked them out.

Here in the game I'm working on, you can see what can happen if you don't mask an image.
http://i14.photobucket.com/albums/a346/Yddrasial/Screenshot-IdioticProductions-As-1.png
See how the asteroids overlap.

dxfoo
03-08-2008, 11:07 AM
Wow, that's just incredibly annoying ;) I tried PNG, tried my theory, and it didn't turn out nice. I guess I'm just use to working with directx surfaces. OpenGL seems to have another take on this. I always hated masking because if I make one change in the art, I have to remember to do it for the mask.

dxfoo
03-08-2008, 11:26 AM
I came across this small PNG library... it uses no masking :)
http://www.coolgroups.com/alpha/

IdioticCreation
03-08-2008, 11:33 AM
Hmm, cool library. Did you remember to use the function
glBlendFunc(GL_SRC_ALPHA, GL_ONE); ?

Because that one works for me.

dxfoo
03-08-2008, 01:00 PM
Am I stuck with masking though? I just find it really annoying. I don't know why OpenGL can't just ask what the RGB color is to ignore, and it would draw the image without that color.