Rotation ?

What I’m trying to do simply is if x is pressed the rotation should be around x-axis and so on with y and z. This is the code




#  include <GL/glut.h>


using namespace std;

static int angle(0);
static float x(0.0), y(0.0), z(0.0);

// Drawing routine.
void drawScene(void)
{
   glClear(GL_COLOR_BUFFER_BIT);
   glColor3f(0.0, 0.0, 0.0);
   glLoadIdentity(); 
   
   // Modeling transformations.
   glTranslatef(0.0, 0.0, -15.0); 
   glScalef(1.0, 1.0, 1.0);
   glRotatef(angle, x, y, z);

   glutWireTeapot(5.0); 

   glFlush();
}

// Initialization routine.
void setup(void) 
{
   glClearColor(1.0, 1.0, 1.0, 0.0);  
}

// OpenGL window reshape routine.
void resize(int w, int h)
{
   glViewport(0, 0, (GLsizei)w, (GLsizei)h); 
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 100.0);

   glMatrixMode(GL_MODELVIEW);
}

// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
   switch(key) 
   {
      case 27:
         exit(0);
         break;
	  case 'x': case 'X':
	  {
		 x = 1.0; y = 0.0; z = 0.0;
		 angle += 25;
		 if ( angle > 360 )
			 angle = 0;
	  }
	  case 'y': case 'Y':
	  {
		 x = 0.0; y = 1.0; z = 0.0;
		 angle += 25;
		 if ( angle > 360 )
			 angle = 0;
	  }
	  case 'z': case 'Z':
	  {
		 x = 0.0; y = 0.0; z = 1.0;
		 angle += 25;
		 if ( angle > 360 )
			 angle = 0;
	  }
      default:
         break;
   }
   glutPostRedisplay();
}

// Main routine.
int main(int argc, char **argv) 
{
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
   glutInitWindowSize(500, 500);
   glutInitWindowPosition(100, 100); 
   glutCreateWindow("box.cpp");
   setup(); 
   glutDisplayFunc(drawScene); 
   glutReshapeFunc(resize);  
   glutKeyboardFunc(keyInput);
   glutMainLoop(); 

   return 0;  
}


Why it doesn’t work as I expected?

hi,

thats simple…
you are missing the breaks in the switch.

      case 'x': case 'X':
      {
         x = 1.0; y = 0.0; z = 0.0;
         angle += 25;
         if ( angle > 360 )
             angle = 0;
             
         break; // <<<<< MISSING !!!
      }

so always the last ( z ) is happening.
But when you change it the behavior for x and y won´t be what you expect …
why ? because of the order of translate rotate …

just play arround with the order and values of these lines to get a better feeling for it:

   glTranslatef(0.0, 0.0, -15.0); 
   glScalef(1.0, 1.0, 1.0);
   glRotatef(angle, x, y, z);

cu
uwi

I might also suggest the small changes to your code shown below.
You’ll need a static angle variable for each axis (xangle, yangle, zangle).
I’m guessing that even after you make these corrections the rotations still
won’t work as you hoped. Let us know.


case 'x':
case 'X':
{
   x = 1.0; y = 0.0; z = 0.0;
   xangle = (xangle + 25) % 360;
   
   break;
}

@uwi2k2 & Carmine,
thank you guys.

@Carmine,
What do you mean that I need angle variable for each axis. The code works only if I set for x glRotatef(angle, 1.0, 0.0, 0.0) and y glRotatef(angle, 0.0, 1.0, 0.0) and so is z. I mean without using variables inside glRotatef().
Does this affect the function?

Up up up up up

I’ve solved the problem and this is the final code

#include <iostream>
#include <GL/glut.h>


static int xangle(0), yangle(0), zangle(0);

 
// Drawing routine.
void drawScene(void)
{
   glClear(GL_COLOR_BUFFER_BIT);
   glColor3f(0.0, 0.0, 0.0);
   glLoadIdentity(); 
 
   // Modeling transformations.
   glTranslatef(0.0, 0.0, -15.0); 
   glScalef(1.0, 1.0, 1.0);
   glRotatef(xangle, 1.0f, 0.0f, 0.0f);  // rotate about x
   glRotatef(yangle, 0.0f, 1.0f, 0.0f);  // rotate about y
   glRotatef(zangle, 0.0f, 0.0f, 1.0f);  // rotate about z
   glutWireTeapot(5.0); 
 
   glFlush();
}
 
// Initialization routine.
void setup(void) 
{
   glClearColor(1.0, 1.0, 1.0, 0.0);  
}
 
// OpenGL window reshape routine.
void resize(int w, int h)
{
   glViewport(0, 0, (GLsizei)w, (GLsizei)h); 
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 100.0);
   glMatrixMode(GL_MODELVIEW);
}
 
// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
   key = toupper(key);
   switch(key) 
   {
      case 27:
         exit(0);
         break;
	  case 'X':
	  {
		 yangle = 0.0; zangle = 0.0;
		 xangle += 25;
		 if ( xangle > 360 )
			 xangle = 0;
		 break;
	  }
	  case 'Y':
	  {
		 xangle = 0.0; zangle = 0.0;
		 yangle += 25;
		 if ( yangle > 360 )
			 yangle = 0;
		 break;
	  }
	  case 'Z':
	  {
		 xangle = 0.0; yangle = 0.0;
		 zangle += 25;
		 if ( zangle > 360 )
			 zangle = 0;
		 break;
	  }
      default:
         break;
   }
   glutPostRedisplay();
}
 
// Main routine.
int main(int argc, char **argv) 
{
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
   glutInitWindowSize(500, 500);
   glutInitWindowPosition(100, 100); 
   glutCreateWindow("box.cpp");
   setup(); 
   glutDisplayFunc(drawScene); 
   glutReshapeFunc(resize);  
   glutKeyboardFunc(keyInput);
   glutMainLoop(); 
 
   return 0;  
}