Skip to main content
GameDev.net gamedev.net
🔒 Locked

Creating a bitmask quickly?

Started by Clancy Jan 15, 2002 at 2:23 PM 6 replies 1.1k views
Original Post
Clancy
Clancy
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
nickm
nickm
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.
Clancy
Clancy
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.


nickm
nickm
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!
Clancy
Clancy
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.

Shannon Barber
Shannon Barber
... pixel perfect CD seems... not-quite-right. It means CD will be different for different resolutions... Do point perfect, polygon-polygon CD if you want accuracy.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Clancy
Clancy
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.

dfawcett
dfawcett
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
http://www.lectersoft.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.