Archived

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

fakemind

drawing rotated bitmap

Recommended Posts

hey all, im starting on a game and i was wondering if anybody knew of any webpages / source / tutorial i could look at to quickly draw a rotated bitmap ? ive got some source that i understand on how to draw a rotated polygon, but since my bitmaps are 32x32 and always square then i should be able to do it faster since i have information i can assume to be correct. ie. if bitmap rotated x degrees, where 90 > x > 0, then i will already know the top-right corner is the highest vertex.

Share this post


Link to post
Share on other sites
I''m sorry, but I don''t know of any websites ect covering this. I can give you a few pointers though. First is to go the other direction. For each pixel in the destination find the source rather than for each pixel in the source find the destination. A 32 by 32 bitmap has 1024 pixels, but if you rotate 45 degrees you only have 1012 pixels. So in general you are either going to plot some pixels multiple times or not at all going from source to destination.

Second instead of 0 to 90 degrees use 0 to 45. That will keep the x increment at one with the y increment being 0 or 1 for each iteration. Bresham''s line drawing algorithm will generate the points for you. Using that algorithm means each successive pixel is a distance of one or sqrt(2) from the last pixel. So which side of the if statement you go through tells you how to update your position in the source.

The pixels in the destination generally don''t fall directly on the pixels in the source. You could ignore this fact, but the resulting bitmap might not look as good as you would like if you do. It might be acceptable so it doesn''t hurt to try it that way and see how it looks. If you want a better quality then you have to recognize that fact. You can''t view a bitmap as a contineoous image. Rather the pixels are points at the intersection of grid lines. A pixel in the destination generally falls somewhere in a square of the grid rather than on the intersection of two grid lines. So a pixel in the destination should be a blend of four pixels in the source. If it was in the exact center then it would be one quarter of each of the four corner pixels. That is one half times one half. I believe it is called a bilinear interpolation, but I''m not certain.

Basically you are a certain percentage of the way from left to right and top to bottom. If you are 25 percent from top to bottom and 40 percent from left to right then the weight of the top left would be (1-0.40)*(1-0.25), top right 0.40*(1-0.25), bottom left (1-0.40)*0.25 and bottom right 0.40*0.25. If you add all those up you get one.

An obvious optimization since you need integers is fixed point. 16 binary decimal places would put you within 0.0014% of sqrt(2). So rather than adding one or sqrt(2) to your position in the source scan line you would add 92681 or 65536 (2^16). Your weights are then the position & 0x0000ffff and 65536 minus that value. When you multiply them together you are going to get 32 binary decimal places, i.e. x*2^16*y*2^16=x*y*2^32 so you need to shift the result to the right 16 places before multiplying the color component by the weight or you will overflow. If you want to round, though it is unneeded here, you would add 2^15 before the shift. Then after you multiply the color component by the weight you sum them together and then shift right 16 decimal places to get the integer color component. If you choose to round just remember it is only before would have truncated a value, i.e. shift, not before additions or multiplications.

Finally remember that only in the first row is the destination row right on the source row. After that it is between rows just like the pixels after the first in a row is between columns. That is why you have to do a bilinear interpolation. The interpolation is going to have the effect of blurring the image. So you may need to run the resulting image through a sharpening filter. There is a good article on this site under Articles & Resources that goes into a great deal of detail on digital filtering. I think it is more generally referred to as image processing so you might use that to search for more referances.

Share this post


Link to post
Share on other sites