Hi all,
I am implementing mouse rotations to rotate a small scene consisting of a sphere and a plane. The scene rotates around its local axes, which gives weird rotations, when I am using the code below. This code is rotating the scene on the object’s local axes (I guess). the rotations works fine for a while, and then starts rotating incorrectly.
I want to rotate the scene in a fixed coordinate system, such that the rotations work well. Please suggest any ideas. I am attaching the code below:
float stateX, stateY, rotateX = 0.0, rotateY = 0.0;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyex, eyey, eyez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(rotateY, 0.0, 1.0, 0.0);
glRotatef(rotateX, 1.0, 0.0, 0.0);
drawScene();
glutSwapBuffers();
glFlush();
}
void drawScene(void)
{
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_amb_diff[] = {0.4, 0.0, 0.0, 1.0};
GLfloat mat_shininess[] = {100.0};
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glutSolidSphere(0.5, 30, 30);
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(-2.0, -1.0, 2.0);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(2.0, -1.0, 2.0);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(2.0, -1.0, -2.0);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(-2.0, -1.0, -2.0);
glEnd();
}
void mouse(int button, int state, int i, int j)
{
if(state == GLUT_DOWN)
{
switch(button)
{
case GLUT_LEFT_BUTTON:
{
check = 1;
stateX = i;
stateY = j;
}
break;
case GLUT_RIGHT_BUTTON:
check = 2;
break;
default:
break;
}
}
}
void motion(int i, int j)
{
if(check == 1)
{
if(j!=stateY)
rotateX = rotateX + (j - stateY) * 0.1;
if(i!=stateX)
rotateY = rotateY + (i - stateX) * 0.1;
stateX = i;
stateY = j;
glutPostRedisplay();
}
}