#### Archived

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

# Bitmap rotation

This topic is 5984 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Yes I am looking for a reasonably quick routine for rotating bitmaps in 2d. I have had a bit of a go at coding one, but I''m a bit slow today and I would like to get my project finished soon. OR maybe you have a software texture mapping routine that I can adapt. (c++ / DirectDraw 7 preferred, but ANYTHING will help!!!!)

##### Share on other sites

So far I have tried the following approaches, and have had no
success.

1. Loop through the source image, rotating every pixel by the
angle.

PROBLEM: When rotated, the shape expands and contracts, and
gaps appear in the image.

2. Rotate the corners of the image, then run a bressenham line
between the top and bottom points, and another through the
left & right points.

PROBLEM: When rotated, gaps appear in the image, but it does
not change size.

My next approach I guess is to write what is essentially a 2d
texture mapper. Does anyone know of a good tutorial?

Edited by - SpazBoy the Mitey on February 20, 2002 1:19:05 AM

##### Share on other sites
You need to drive it by the destination. You want to plot every pixel in the destination and do so only once. A horizontal or vertical line has more pixels within a given length than any diagonal line. A line from 0,0 to 0,100 has 100 pixels as does a line from 0,0 to 100,100, but the second line is longer. It is difficult to see how you would miss a pixel, but I don''t feel up to proving it can''t happen. You can definitely overplot though. Rotation by multiples of 90 degrees are special in that you have the same number of pixels so handle those seperately. How you do those gives you a good clue as to how you do other angles except that it makes no differance whether the source or destination drives it.

A given pixel in the destination generally falls somewhere between four pixels in the source. Those pixels form a square. There is a top-left, TL, top-right, TR, bottom-left, BL, and bottom-right, BR, pixel. Your destination pixel, DP, is a weighted average of those four pixels. The weight is determined by the percentage of the distance from left to right, PR, and top to bottom, PB. So if you calculated that you need a pixel at (10.25,9.75) then PR=0.25 and PB=0.75. DP = TL*(1-PR)*(1-PB) + TR*PR*(1-PB) + BL*(1-PR)*PB + BR*PR*PB.

I would suggest you use that for enlarging an image first. The reason being that the optimizations are more obvious without the rotation. If you think about how you do an enlargement and a rotation by 90 degrees and how that relates to doing a rotation by other angles then many of the optimizations should be fairly obvious. The biggest thing to remember is that this is an image on a computer screen. All that really matters is that it looks good so you only need to be close.

##### Share on other sites
I just posted that as a reply to a post in another Forum on gamedev (dunno if that was you too) but the best way to rotate in realtime is to use your bitmap as a texture on a 3D primitve as those are easy to rotate. You can use filters on them, too, and scale, and alphablend, so it''s well worth the work.

##### Share on other sites
If you''re using DirectDraw, then when you do the blit, all you have to do is set up the DDBLTFX structure with the rotation angle field to an angle (in degrees), and then make sure you have the correct flag set when you call Blt().

##### Share on other sites
Oh yeah, and also, Andre'' LaMothe has a software renderer with free source (just make sure to give him credit) at http://www.xgames3d.com/games/tmap.zip .

##### Share on other sites
How do you set up the DDBLTFX structure to handle rotations for you? I was going to try and use this for my game.

Can some one show a short sample of How you rotate a bitmap with DDBLTFX? Just a small sample of defining what needs to be defined and then showing a quick sample of how you use it.

Something that you would see in Windows Game Programming if you have that book. If not then I mean something thats about 15 to 20 lines.

##### Share on other sites
I''m don''t know if this would work, because I haven''t tried it out yet.
  // source is the source surface (a LPDIRECTDRAWSURFACE), dest is the destination surface (another LPDIRECTDRAWSURFACE), and source_rect and dest_rect are rectangles specifying where the blit will be.// angle is the rotation angle, in degrees. The dwRotationAngle is an integer, specified in hundrethds of degrees.DDBLTFX ddbltfx;memset(&ddbltfx, 0, sizeof(DDBLTFX));ddbltfx.dwSize = sizeof(DDBLTFX);ddbltfx.dwRotationAngle = (int)(angle * 100);dest->Blt(&dest_rect, source, &source_rect, DDBLT_WAIT | DDBLT_ROTATIONANGLE, &ddbltfx); // Add the DDBLT_KEYSRC flag for source color keying

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631706
• Total Posts
3001833
×