Rotating about a single axis

Not particularly sure if the title of this thread is correct…but what i a want to achieve is to be able to rotate the object about the x, y or z axis .

i already got the models rotating in all the axis but the point is they tend to move away from their original position when rotating, what i want is for them to stay in the same position while i rotate them in any angle i want



GLfloat rotMast[3];



void Draw(){
...........
glPushMatrix();
			glTranslatef(positions[0].x, positions[0].y, positions[0].z);
			glRotatef(rotMast[0], 1.0f, 0.0f, 0.0f);
			glRotatef(rotMast[1], 0.0f, 1.0f, 0.0f);
			glRotatef(rotMast[2], 0.0f, 0.0f, 1.0f);

			drawMast();
		glPopMatrix();
..........
}
void OnKey(unsigned char key, int x, int y) {
	switch(key){
    case 'k': case 'K':
		if (selected_index==0){
				rotMast[0]+= amount;
				rotMast[1]+= amount;
				rotMast[2]+= amount;
				}
}
}

i would also like them to rotate in any angle so i could achieve any rotation desired…seems my method cannot achieve that.
please your ideas and possibly a short snippet would be very helpful…
thanks

My first suggestion is to use a different key for each rotation.
That will give you more control over the rotations.
The code below would do that.
It won’t solve all your problems, but it’s a big set in the right direction.


GLfloat amount = 2.0, xr = 0.0, yr = 0.0, zr = 0.0;

void Draw(void)  {

   glPushMatrix();

      glTranslatef (positions[0].x, positions[0].y, positions[0].z);
      glRotatef (xr, 1,0,0);
      glRotatef (yr, 0,1,0);
      glRotatef (zr, 0,0,1);
      drawMast ();

   glPopMatrix();
}

void OnKey (unsigned char key, int x, int y) {

   switch (key)  {
      case 'x':  xr += amount;  break;
      case 'X':  xr -= amount;  break;
      case 'y':  yr += amount;  break;
      case 'Y':  yr -= amount;  break;
      case 'z':  zr += amount;  break;
      case 'Z':  zr -= amount;  break;
   }
}

thanks MaxH, i made your adjustments and also put the rotation functions before the translation function…but i still moves out of position while rotating…any ideas how i could fix that…

thanks for your help

thanks MaxH,
how do i get the objects center??

The two ‘glTranslate’ commands I added to the code above do the centering. You have to know, or figure out, what (xc, yc, zc) is, which are simply the coordinates of the point you want to rotate you object around. It’s probably going to be a point somewhere in the object. Do you know what the coordinate ranges of each object are? Where did the objects come from? Did you create them? If so, you should know what (xc,yc,zc) are. Good luck.

problem solved…i calculated the center in opengl and just followed exactly your method …thanks alot MaxH