Archived

This topic is now archived and is closed to further replies.

Clancy

Creating a bitmask quickly?

Recommended Posts

Clancy    124
What is the best way to create a bitmask? I have all my sprites in 1 file, and I have a alpha channel for each sprite in same file. I figure that I could read the alpha, then convert that to bitmask, but the examples I have seen are all API specific, and I am using multiple APIs, so that is out. The way I am trying to do it is read .tga file, then scan alpha channel where it is 8-bit color, then I try to convert that to 1 bit, but so far no luck. I need some pushing in the right direction. (dunno what happened, this part got snipped off before) Right now, I read 4 bytes from the alpha channel, and then shift them over to the high byte of the bitmask byte, and then OR with the next 4 bytes which would be the low order byte. Now, after that conversion, I try to dump the data, and look at it in binary, and it doesn't look correct, so I figured I am doing something wrong. Edited by - Clancy on January 15, 2002 4:29:33 PM

Share this post


Link to post
Share on other sites
nickm    122
I don''t really get what you are saying, but what do you want to achieve with this? I mean, you can just read the alpha channel from a tga file, and then blit (or render) the image, so there''s no need for bitshifting and things like that.

Share this post


Link to post
Share on other sites
Clancy    124
I am using the bitmask for pixel perfect collision detection.

I am just not able to get the correct mask, since I am getting strange results.

Guess I spend more time with debugger.


Share this post


Link to post
Share on other sites
nickm    122
All right! Say you have an 32x32 pixel image, with an alpha channel, and you want to store the bitmask of this whole image in an array of 32 longints. Supposing that you have stored the image as an array of chars (like for example: char image[32][32][4] and that the alpha channel value is stored in image[x][y][3]), I would probably do this this way:

  
char image[32][32][4];
unsigned long bitmask[32], mask, n;
int x, y;

for (y = 0; y<32; y++)
{
mask = 0;
n = 1;

for (x = 0; x<32; x++)
{
// if there''s a pixel here..

if (image[x][y][3]==0)
mask |= n;

n <<= 1;
}

bitmask[y] = mask;
}


Good luck with it!

Share this post


Link to post
Share on other sites
Clancy    124
Thanks for the code, will try it out when I get back home in 2 days.

I also think I know what my problem was, since I am from 68k world, I forgot that the x86 world swaps the low/high bits around. Oops.

Share this post


Link to post
Share on other sites
Clancy    124
Why do you think you can change resolutions?

Although you do bring up a good point, since I would have to scale the collision bitmask up/down based on what res I am using. But for now, it is hardcoded to 640x480.

Share this post


Link to post
Share on other sites
dfawcett    122
i use opengl,i dont know how how DX does it but, i have 2 routines called loadtextures(), makemask()

in the makemask routine i read in the r/g/b and make them either b/w and feed the new info into the texture generator.

Edited by - dfawcett on January 18, 2002 12:09:54 AM

Share this post


Link to post
Share on other sites