PDA

View Full Version : Sprites



04-27-2001, 08:48 AM
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?

MrShoe
04-27-2001, 06:04 PM
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.

04-28-2001, 03:01 AM
Yes, but I don't want to make a sepperate masking-bitmap, thats silly...

DFrey
04-28-2001, 03:03 AM
Just saturate the alpha component of the black part, then enable alpha testing, and set the alpha test to reject pixels with high alpha.

04-28-2001, 03:51 AM
Thanx, but could you give me a code example? Sorry, I'm a newbie.

ffish
04-28-2001, 11:24 PM
DFrey,

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.

Thanks.

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

Tim Stirling
04-28-2001, 11:54 PM
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 &amp;&amp; blue == 255 &amp;&amp; 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:
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GEQUAL, 0.05f);
In your init code. This was ruched and so might be wrong but it shouldd help.

04-29-2001, 08:59 AM
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? http://www.opengl.org/discussion_boards/ubb/frown.gif

DFrey
04-29-2001, 09:52 AM
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).]

04-29-2001, 10:04 AM
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"))
{
glGenTextures(1,&texture[2]);
glBindTexture(GL_TEXTURE_2D,texture[2]);
glTexImage2D(GL_TEXTURE_2D,0,3,TextureImage[2]->sizeX,TextureImage[2]->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,TextureImage[2]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
}

if (TextureImage[2])
{
if (TextureImage[2]->data)
{
free (TextureImage[2]->data);
}

free (TextureImage[2]);
}