PDA

View Full Version : Rotate image around an arbitrary point in OpenGL?



jdarling
12-10-2007, 01:19 PM
Ok, one of my developers thought if funny to pose this question to me. I thought I had the answer and once I showed him the code it didn't work at all :(. This made me think quite a bit and I still can't come up with an answer (other then switching from QUAD rendering to triangle rendering and using the triangle center point as the image 0 point).

Given:
glBegin(GL_QUADS);
glTexCoord2f( textLeft, textTop );
glVertex2f( 0, 0 );

glTexCoord2f( textLeft, textBottom );
glVertex2f( 0, dstH);

glTexCoord2f( textRight, textBottom );
glVertex2f( dstW, dstH);

glTexCoord2f( textRight, textTop );
glVertex2f( dstW, 0 );
glEnd;
where dstH and dstW are the Height and Width of the pattern being displayed (not the image but the pattern within the image).

You translate by simply adding glTranslatef(dstX, dstY) easy enough.

But, if I want to rotate the image around the point (17, 34) how do I do it?

My initial answer was simply translate to -point rotate translate to dst+point. This of course doesn't work. Neither does any combination of this :(. So I'm guessing that you have to perform the matrix translation by hand and then render the quad in the appropriate rotation. Is there a pure OpenGL way to handle this besides moving to Triangle rendering?

User137
12-10-2007, 01:34 PM
Your first logics are correct actually, just fine tuning.

1) Move view to wanted center point, place that image rotates around
2) Rotate
3) Move to the inverse coordinates of image rotate point
4) Draw normally (0,0,Width,Height)

jdarling
12-10-2007, 04:49 PM
Thanks for the answer. After looking back through the sample I gave him I had my translations in the wrong order :). Now everything is working just fine and I look like I know something again :).