Rotating a bitmap

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 4620 Views - Last Post: 29 September 2007 - 10:17 AM Rate Topic: -----

#1 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Rotating a bitmap

Posted 24 September 2007 - 11:51 AM

Ok, i am using SDL with C++ to create a very basic engine. And i want to draw a rotated image.
Here is my code

const float DEG2RAD = 3.14159/180;

void findcoord_circle(int x, int y, double radius, double degree, int &getx, int &gety)
{
 float degInRad = degree*DEG2RAD;
 getx=(int)((cos(degInRad)*radius)+0.5);
 gety=(int)((sin(degInRad)*radius)+0.5);
 getx+=x;
 gety+=y;
}

double distance(int x1, int y1,int x2, int y2)
{
 double xd = (double)x2-(double)x1;
 double yd = (double)y2-(double)y1;
 return(sqrt(xd*xd + yd*yd)); 
}

double finddegrees(int x1, int y1, int x2, int y2)
{
 double vx, vy;
 vx=(double)x2-(double)x1;
 vy=(double)y2-(double)y1;
 return atan(vy/vx)*(180/3.14159265);
}

void drawturnedimage(SDL_Surface *input, SDL_Surface *screen,int x,int y ,double degrees)
{
 int center_w=(int)(input->w/2);
 int center_h=(int)(input->h/2);
 for(int a=0;a<input->h;a++)
 {
  for(int b=0;b<(input->w);b++)
  {
   double degrees2;
   degrees2 = finddegrees(x+center_w,y+center_h,x+b,y+a)+degrees;
   int pixel_x, pixel_y;
	double radius=/*(int)*/distance(x+center_w,y+center_h,x+b,y+a);
   findcoord_circle(x+center_w,y+center_h,radius,degrees2,pixel_x,pixel_y);
   Uint8 r,g,b;
   SDL_GetRGB(getpixel(input,b,a),input->format,&r,&g,&b);
	DrawPixel(screen,pixel_x,pixel_y,r,g,b);
   }
  }
 }
}


I wanna draw a rectangular (136 on 136) blue bitmap and i get this if i rotate it on 30 degrees

Posted Image

Where did i go wrong?

EDIT:
You gotta love the patterns on the ouput though :D

This post has been edited by manhaeve5: 24 September 2007 - 12:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Rotating a bitmap

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Rotating a bitmap

Posted 24 September 2007 - 12:08 PM

View Postmanhaeve5, on 24 Sep, 2007 - 11:51 AM, said:

Ok, i am using SDL with C++ to create a very basic engine. And i want to draw a rotated image.
Here is my code

(EDIT - code and image removed)


Where did i go wrong?

EDIT:
You gotta love the patterns on the ouput though :D


try to avoid double posting, especially when they are both on the same forum.

so the issue is that you're only getting the half-height rectangle, rather than a square?

not really familiar with SDL, but when I'm doing bitmap stuff, i try to use images/textures that are assymetrical, so that I know what parts are being displayed. my standard is a smiley face with a row of black dots along the bottom and left edges, and a row of red dots along the top and right edges.

if you can display something assymetric, this might help tell you where the problem lies, and it would probably make it easier for others to help you.

and is there a mod/admin out there who could close/delete the previous post to reduce clutter?

-jjh

This post has been edited by jjhaag: 24 September 2007 - 12:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Rotating a bitmap

Posted 24 September 2007 - 12:18 PM

Ok, edited the image.

And sorry for the double post, didnt know how it got there
Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Rotating a bitmap

Posted 24 September 2007 - 12:32 PM

one thing you may want to avoid is using the variable b as both a row/col index in the bitmap and as a color component...maybe switch your for-loop variables to row and col...just a thought

-jjh

This post has been edited by jjhaag: 27 September 2007 - 12:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Rotating a bitmap

Posted 24 September 2007 - 12:41 PM

***** i always make the same **** mistake. Last time i made that mistake it took me 1 FULL DAY to find it. because i was always used to cycle trugh image i used 2 for loops a and b,but i never had to use the r, g and b colors. They were grouped in 1. Thx mate :D

Just 2 problems left
1: He only shows the green and the yellow bar.
2: The green and yellow bar arr rotated on their own

Here is the same imaged rotated for 45 degrees.

Posted Image
Was This Post Helpful? 0
  • +
  • -

#6 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Rotating a bitmap

Posted 24 September 2007 - 02:10 PM

View Postmanhaeve5, on 24 Sep, 2007 - 12:41 PM, said:

***** i always make the same **** mistake. Last time i made that mistake it took me 1 FULL DAY to find it. because i was always used to cycle trugh image i used 2 for loops a and b,but i never had to use the r, g and b colors. They were grouped in 1. Thx mate :D

Just 2 problems left
1: He only shows the green and the yellow bar.
2: The green and yellow bar arr rotated on their own

Here is the same imaged rotated for 45 degrees.

Posted Image



as i said, no experience with SDL personally. however, it seems that since the individual colored rectangles within the blue square seem to be rotating independently, you must be passing several bitmap objects to the drawing function. if there's any way to do it, i think you want to perform some sort of "merge layers" function to get a single bitmap from the square+4 rectangles.

since the top of the square is getting clipped, have you checked out what your input object is returning for the height of the bitmap? you may also want to put input->h and input->w in parentheses whereever they show up - i notice that you do have it that way for the inner loop, but don't have it that way for the outer loop.

-jjh
Was This Post Helpful? 0
  • +
  • -

#7 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Rotating a bitmap

Posted 26 September 2007 - 05:55 AM

There is only 1 bitmap passed.

the height is always correct. (i checked it)

and what do u mean with parentheses
Was This Post Helpful? 0
  • +
  • -

#8 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Rotating a bitmap

Posted 26 September 2007 - 03:15 PM

View Postmanhaeve5, on 26 Sep, 2007 - 05:55 AM, said:

There is only 1 bitmap passed.

the height is always correct. (i checked it)

and what do u mean with parentheses



with the parentheses, i mean to change the lines like

int center_h=(int)(input->h/2);
for(int a=0;a<input->h;a++)

to

int center_h=(int)((input->h)/2);
for(int a=0;a<(input->h);a++)

it probably won't make a difference, but hey, some compilers are wonky, and i've had it fix problems for me in the past.

as for the rest, i'm still not sure. change the size of the yellow and green rectanlges so that each of the rectangles in the original image is a different size, and with a random slight offset from the exact corners. that won't fix anything in and of itself, but it might help to see exactly what portions of the bitmap are being displayed correctly or incorrectly.

and are you using a separate function for drawing the bitmap in its unrotated form? or just the given function with degrees=0? if you're using a separate function, try drawing the image using drawturnedimage() with degrees=0. if that gives strange results, comment out all of the lines
   double degrees2;
   degrees2 = finddegrees(x+center_w,y+center_h,x+b,y+a)+degrees;
   int pixel_x, pixel_y;
	double radius=/*(int)*/distance(x+center_w,y+center_h,x+b,y+a);
   findcoord_circle(x+center_w,y+center_h,radius,degrees2,pixel_x,pixel_y);


and see what happens. and post the code to the function for drawing the unturned image, if you have one.

-jjh

This post has been edited by jjhaag: 26 September 2007 - 03:19 PM

Was This Post Helpful? 0
  • +
  • -

#9 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Rotating a bitmap

Posted 27 September 2007 - 08:49 AM

This is the image at 0 degree
Posted Image
and the parentheses didnt work
Was This Post Helpful? 0
  • +
  • -

#10 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Rotating a bitmap

Posted 27 September 2007 - 12:31 PM

View Postmanhaeve5, on 27 Sep, 2007 - 08:49 AM, said:

and the parentheses didnt work


meh, the parentheses would have been too easy. it's always nicer to blame a faulty compiler; saves on serious debugging time.

So what does your regular old unrotate drawing function look like? What happens when you comment out the lines i suggested?

You've got a baseline for what this function should draw with degrees=0 : the unrotated drawing function. Now you need to go through each function call and figure out at what point the display changes to the incorrect figure, even when you're using degrees=0. And that's going to be tough for us to help you with, without the unrotated drawing function.

-jjh
Was This Post Helpful? 0
  • +
  • -

#11 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Rotating a bitmap

Posted 28 September 2007 - 06:14 AM

If i comment out the lines it doesnt work.

And the unrotated image function is this

SDL_BlitSurface(input,null,offset,screen);
Was This Post Helpful? 0
  • +
  • -

#12 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Rotating a bitmap

Posted 28 September 2007 - 09:50 PM

View Postmanhaeve5, on 28 Sep, 2007 - 06:14 AM, said:

If i comment out the lines it doesnt work.

And the unrotated image function is this

SDL_BlitSurface(input,null,offset,screen);


my bad with the commenting suggestion - don't comment out the pixel_x and pixel_y declarations, and intialize them to the unrotated a/b coordinates. i think that is all that is required to allow the drawing function to occur, without going through the rotating step.

hwoever, after having looked through this a bunch of times, i think that the problem may be with your finddegrees() function. there is going to come a time in your routine in which the value of vx is zero, and hence the value passed to atan will be undefined. i'm not sure exactly what this will do, but I can only imagine that it's bad. it will happen when the pixel lies at the midpoint of the bitmap's x-domain. i'm not sure of the ordering of the arguments to the SDL functions, and i wasn't really following the x/y vs. row/col transformations all the way through your code, but i have a feeling that this might explain why the bitmap is cropped exactly halfway up the height.

you may want to call atan2() instead, which accepts the numerator and denominator separately, rather than just as a fraction. Unfortunately, a domain error will occur if you pass (0,0), so you may want to just check for this condition prior to the atan2() call, and set the return value to 0 in this case i.e.

if !vx) {
	return atan2(vy,vx)*(180/3.14159265);
} else {
	return 0;
}


one thing that you could do is, instead of displaying the bitmap to the screen, simply output the a/b/pixel_x/pixel_y values to a console or file to check what is going on in your drawturnedimage() function.

i have a feeling that this may be getting at the root of your problems, but who knows. give it a shot, and i hope it helps.

-jjh

This post has been edited by jjhaag: 28 September 2007 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#13 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Rotating a bitmap

Posted 29 September 2007 - 01:24 AM

Yes , it WORKS =D. Ur fantastics :D.

Posted Image

Just 1 problem left. There are still patterns on the output image.
And will this be useable for use in games? Or is it too slow?

This post has been edited by manhaeve5: 29 September 2007 - 01:31 AM

Was This Post Helpful? 0
  • +
  • -

#14 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Rotating a bitmap

Posted 29 September 2007 - 01:50 AM

View Postmanhaeve5, on 29 Sep, 2007 - 01:24 AM, said:

Yes , it WORKS =D. Ur fantastics :D.

Just 1 problem left. There are still patterns on the output image.
And will this be useable for use in games? Or is it too slow?



sweet! glad that you got it running.

as for speed, i'm not sure, not having used SDL for graphics work. however, i'd guess that 3 user function calls + 3 trig calls +1 sqrt per pi2xel, would probably be pretty slow.

if you just want to test is, try running a series of rotation/display sequences and timing it. include ctime or time.h, store the output from clock() before the calls, and then again after they're finished. if rotation is the most cpu-intensive step in the process, you'd want to be able to get at least a hundred calls per second. if you've got other stuff like AI, physics, etc., you'd be looking at a very choppy game at those rates.

you might want to try out openGL and glut - openGL has per-defined rotation functions, and using glut allows to you avoid a lot of the low-level graphics calls. if you can, you're better off letting the graphics card take care of the work for you, and free up the cpu for other stuff (physics, AI, etc.). if you're set on SDL, you may want to try a search for SDL rotate or SDL rotation - i've come across a couple of libraries that support a bunch of different drawing functions, and one of them may be what you need.

and again, glad you got it running.

-jjh



oh, and about the patterns - i have a feeling that they result from the rounding that occurs when rotating each pixel to its new position. because of the rounding, i think that those spots are simply not drawn to, and thus just show the background color. try changing the background color, and see if the background pattern changes color as well (zoom in a ways to see fer sure).

-jjh
Was This Post Helpful? 0
  • +
  • -

#15 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Rotating a bitmap

Posted 29 September 2007 - 04:30 AM

Yeah, i think it comes from the doubles and the int used together. But where lays the fault.
And if i use opengl i can only use images with dimesions to the power of 2, like this: 16.16, 32.32 ,256.256 so thats not an option

This post has been edited by manhaeve5: 29 September 2007 - 04:31 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2