PDA

View Full Version : Help with Rotation Algorithm



cairnswm
21-04-2006, 02:36 PM
Hi All

Can anyone help with some rotation logic. I have the following code:

procedure DrawRectangle(x,y,w,h:single); Overload;
begin
glBegin( GL_TRIANGLE_STRIP ); // Drawing Using Triangles
glTexCoord2f(0,0);
glVertex2f( x, y );

glTexCoord2f(0,1);
glVertex2f( x, y+H );

glTexCoord2f(1,0);
glVertex2f( x+W, y );

glTexCoord2f(1,1);
glVertex2f( x+W, y+H );
glEnd; // Finished Drawing The Triangle
end;

Now I would like to add a 'RotateDegrees : Single' parameter and the Image will be drawn rotated by RotateDegrees. If possible I want it rotated around the center of the item.

I have tried glRotatef(45,1,1,0) and it doesnt work properly. It seems the image is rotated but the left bit is clipped off. Also it draws the image offset as per screen rotation instead of the original location with only an image rotated.

Thanks

grudzio
21-04-2006, 03:44 PM
You should rotate around z axis i.e. glRotatef(45,0,0,1). To rotate around images's center try this code

glPushMatrix;
glTranslatef(x+W/2,y+H/2,0); //transate to the center of the image
glRotatef(45,0,0,1); //rotate
glBegin(GL_QUADS); //draw
glVertex2i(-w/2,-h/2);
glVertex2i(w/2,-h/2);
glVertex2i(w/2,h/2);
glVertex2i(-w/2,h/2);
glEnd;
glPopMatrix;

cairnswm
21-04-2006, 07:18 PM
Thanks - I got it working :)

procedure DrawRectangle(ox,oy,ex,ey,x,y,w,h,a:single); Overload;
begin
glPushMatrix;
glTranslatef(x+W/2,y+H/2,0); //transate to the center of the image
glRotatef(A,0,0,1); //rotate
glBegin( GL_TRIANGLE_STRIP ); // Drawing Using Triangles
glTexCoord2f(ox,oy);
glVertex2f( -w / 2, -h / 2);
glTexCoord2f(ox,ey);
glVertex2f( -w / 2, +H / 2);
glTexCoord2f(ex,oy);
glVertex2f( +W / 2, -h / 2);
glTexCoord2f(ex,ey);
glVertex2f( +W / 2, +H / 2);
glEnd; // Finished Drawing The Triangle
glEnd;
glPopMatrix;
end;


Not I have Rotation working. Got Blending Working - and I've learnt a lot about OpenGL and the way it does things this week.

I am busy getting my wrappers together in a set of documented libraries and will make it publically available again soon. (Probably just after the contest ends)