rotate a polygon around an axis


Ok, I have a triangle, and I want to rotate it around its “tail” (the oposite of the base). I get that glRotate rotates the triangle around the origin, in the vector I give it. So I figured, I have to translate the triangle, rotate it, then translate back, but it doesn’t work, because the origin is also translated. If I build the triangle, so that is points to the origin, and then translate it works, but that’s not an option… Here is the code…

glRotatef(ang1, 0, 1, 0);

glColor3b(127, 0, 0);
glVertex3f(3, 0, 0);
glVertex3f(4, -1, 0);
glVertex3f(4, 1, 0);

Any tips?


So, you mean you are doing an arbitrary amount of transformations before you want to rotate your triangle, and therefore don’t know how much to trabslate it to move it to the origin?

If so, you can try two things.
First is to do the transformations yourself. Download MESA and have a look at it’s rotate source (unless you already know how to construct an axis/angle rotation matrix of course). When you have this matrix, do the transformation yourself.
I don’t know if this second suggesion works, but you can get the current modelview matrix from OpenGL, and extract the translation part. I think it’s the [12]th, [13]th and [14]th element. This is the X, Y and Z coordinate you need to pass to glTranslatef to move the triangle to the origin.