I want to add Sprites to my 3D-Engine. But how can I remove the black color outside of the bitmap? Is there a mask-command or something?

Theres a tutorial at nehe.gamedev.net ESPECIALLY for removing the black stuff outside of the sprite. Check it out, it is perfect for what you need.

Yes, but I don’t want to make a sepperate masking-bitmap, thats silly…

Just saturate the alpha component of the black part, then enable alpha testing, and set the alpha test to reject pixels with high alpha.

Thanx, but could you give me a code example? Sorry, I’m a newbie.


This could help me too. Do you know how much alpha testing hurts performance on GeForce hardware? I’m working with medical images that contain noise that I have to mask out. Currently I’m doing this kind of stuff as a preprocessing step.


[This message has been edited by ffish (edited 09-08-2001).]

First of all you need to loop through the data in the bitmap that you have loaded before you send it to opengl making it an RGBA format and making all opaque areas have an alpha of 255 and the areas (black areas etc.) you want transparent have a alpha of 0

for (xx = 0; xx < dib.Info->biWidth; xx++){ // loop through bitmap
for (yy = 0; yy < dib.Info->biHeight; yy++){
nIndex = 3*(dib.Info->biWidth * yy + xx);
red = dib.bits[nIndex]; //red
green = dib.bits[nIndex+1]; //green
blue = dib.bits[nIndex+2]; //blue
b = 4*(dib.Info->biWidth * yy + xx);
dib2.bits[b] = red;
dib2.bits[b+1] = green;
dib2.bits[b+2] = blue;
if (red == 255 && blue == 255 && green == 255)alpha = 0;
dib2.bits[b+3] = alpha;
alpha = 255;
} // for yy
} // for xx
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, dib.Info->biWidth, dib.Info->biHeight, GL_RGBA, GL_UNSIGNED_BYTE, dib2.bits);

There is some very messy code I have lifted but it should work you yhen want to add:
glAlphaFunc(GL_GEQUAL, 0.05f);
In your init code. This was ruched and so might be wrong but it shouldd help.

Your code doesn’t work. “Acces violation”, sorry.
Nobody can help me? Is OpenGL so bad that you can’t make sprites without an extra Masking-Image?

Yes you can, just like I said earlier. In fact ignore that masking tutorial on NeHe, I think that tutorial was only presented for academic purposes as alpha testing is a standard part of OpenGL and must be supported at some level. First change your sprite so that it uses an RGBA texture. You can either do this with image editing software or procedurally in a fashion similiar to what Tim showed. In the alpha channel of the texture is where you’ll put the sprite’s mask. And since there are many ways you can prepare the mask, let’s assume that you want pixels with alpha greater than 0.5 to not be drawn. Then when you render the sprites, first enable alpha testing by using glEnable(GL_ALPHA_TEST), then you setup the alpha test function. Again, assuming we want pixels with alpha greater than .5 to be rejected, you’d use glAlphaFunc(GL_LEQUAL, 0.5f). After alpha testing is prepared, draw all your sprites in one swoop. Then disable alpha testing.

[This message has been edited by DFrey (edited 04-29-2001).]

Thanx, but one thing I don’t understand on Tim’s code: What are the objects dib and dib2?
I’m using this:

memset (TextureImage,2,sizeof(void *) *1);
if (TextureImage[2] = LoadBMP("textur3.bmp"))
if (TextureImage[2])
	if (TextureImage[2]-&gt;data)
		free (TextureImage[2]-&gt;data);

free (TextureImage[2]);