Bitmap rotation

Started by
6 comments, last by SpazBoy the Mitey 22 years, 1 month ago
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!!!!)
I''m not interested, really.
Advertisement

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?

Please help, I'm not just grubbing for free code here, I'm actually trying very hard.

Edited by - SpazBoy the Mitey on February 20, 2002 1:19:05 AM
I''m not interested, really.
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.
Keys to success: Ability, ambition and opportunity.
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.
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().
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 .
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.
Take back the internet with the most awsome browser around, FireFox
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  

This topic is closed to new replies.

Advertisement