Rotating a Bitmap

Started by
13 comments, last by Lafo 24 years, 4 months ago
OK, I'm assuming you are using DirectX to do this crap, so here goes nothing. Assuming these variables:

// backbuffer (you'll flip it)
LPDIRECTDRAWSURFACE lpDrawOnMe;
// source bitmap
LPDIRECTDRAWSURFACE lpOhYES;
// DD blit effects structure
DDBLTFX ddbltfx;
// two rectangles, one source and one dest
RECT source;
RECT dest;

here's the procedure:

memset(&ddbltfx,0,sizeof(DDBLTFX);
ddbltfx.dwSize=sizeof(DDBLTFX);
ddbltfx.dwRotationAngle=rotate;

// now blit that sucker
lpDrawOnMe->Blt(&dest,lpOhYES,&source,DDBLT_ROTATIONANGLE, &ddbltfx);


There you go, hope that helps!

------------------
That's how you do it, but that's just the opinion of a MADMAN!!! BWAHAHAAHAHA! :D :D :D

That's how you do it, but that's just the opinion of a MADMAN!!! BWAHAHAAHAHA! :D :D :D
Advertisement
That won't work if the hardware doesn't support it (DirectDraw won't emulate it).
What will work then?
Did you also tell him the fun news that BLT will be 10% slower than bltfast?

You might as well do 3 things.

1: go 3d and save yourself now
or
2: draw the flipped object into another bmp and just load it when you need it.
or..
3: make another surface.. and after you load the bmp up.. you do you rotation.. and save the rotated surface into a new one.. and basically cache your image. So when you need to rotate the bmp you just load up the surface that you calc'd from before and display it.. this way the CPU calcs are done during game launch and not realtime each loop during the game.

-DL

BLT is only 10% slower than BLTFAST if the display adapter doesn't support hardware blitting, almost all of today's display adapters do support hardware blitting
I got the same problem as this guy.
There was a lot of replies on this issue, but the question remains: How the **** will i be able to rotate the sprite while blitting?? Yes, Madman's code shall work
IF THE GRAPHIC CARD SUPPORTS IT!!!
Supose i WON'T, then what?
Write your own!
http://exaflop.org/docs/cgafaq/
William Reiach - Human Extrodinaire

Marlene and Me


Here's what I found on rotating bitmaps in the faq that Gromit quoted above:
=====================================
Subject 3.01: How do I rotate a bitmap?
The easiest way, according to the comp.graphics faq, is to take the rotation transformation and invert it. Then you just iterate over the destination image, apply this inverse transformation and find which source pixel to copy there. A much nicer way comes from the observation that the rotation
matrix:

R(T) = { { cos(T), -sin(T) }, { sin(T), cos(T) } }
is formed my multiplying three matrices, namely:

R(T) = M1(T) * M2(T) * M3(T)
where

M1(T) = { { 1, -tan(T/2) },
{ 0, 1 } }
M2(T) = { { 1, 0 },
{ sin(T), 1 } }
M3(T) = { { 1, -tan(T/2) },
{ 0, 1 } }
Each transformation can be performed in a separate pass, and because these transformations are either row-preserving or column-preserving, anti-aliasing is quite easy.
=====================================
Now, the question is for those who are just learning, is this enough information to "write your own!"? Maybe, but maybe not for some people. I really tire of supposed experts on subjects yelling at newbies and throwing a URL at them telling them to figure it out for themself. Some people are able to take the above and make something out of it. Some people need more help, perhaps a code example doing something similar. If you are an expert and can't be bothered to help teach, don't yell at the students who are looking for such a teacher.

Cya,
Dave

Gromit does seem a little egdey today, as that locked message on the general forum proves, but I see his point. No, that extract from the FAQ alone may not be enough information for some people. But the whole point of this (programming, writing games, etc.) is to learn.

Sometimes its nice to be able to tell a black box (DirectDraw) to blit something for you and just assume the result will happen, but when the time comes to do something unique, unless you actively learn topics of interest like 2D bitmap rotation, you will have no basis on which to implement it. You will come running back to this forum and forums like it to ask your question and get your code most everytime. This is not the way to program. The sharing of knowledge is only really useful when you learn from your mistakes.

Its this knowledge I think that seperates the true hobbyists from those who probably could, if they so please, go into programming professionally (or already are). I certainly will go into programming, hopefully *crosses fingers* game programming, as soon as I get out of high school and finish my education in college.

- Splat

[This message has been edited by Splat (edited November 23, 1999).]

Oh yeah, forgot to help you

The above rotation matrix (2x2) is used to rotate a pixel coordinate around the origin a certain amount (depends on what your trig functions take as a parameter, radians, degrees, etc.).

However, if you were to just go over the source image each pixel, and use that to figure out where that pixel goes, you would have many gaps in the destrination image where the pixels were not filled in. The alternative, much better solution is to invert the matrix (look that one up, not enough space to explain) so that you have a matrix that, when multiplied by a 2D coordinate, will tell you which source pixel was rotated to get there.

So essentially, take the inverted matrix, go through each destination pixel, and copy the source data from the pixel indicated by the matrix operation. That is the simple rotation. However, the source pixel indicated will generally by a floating point (ie not integer). You can get away with simply rounding the result, but that does not look as good as combining the pixels around the subpixel in the source image in proportion to the amount that the floating point coordinate indicates. That will get you more accurate results, and is not THAT slow.

Calculating the rotations in-game is not the best method however. You'd be best calculating a bunch of images with the orientation of the image in the upper right quadrant, maybe at 5 degree increments (that would yield 18 images), then flip it in-game to get it pointing at the other 3 quadrants. Flipping across the X and Y axises is VERY fast and has perfect reproduction, because you are simply swapping pixels.

- Splat

[This message has been edited by Splat (edited November 23, 1999).]

This topic is closed to new replies.

Advertisement