PDA

View Full Version : Rotating an object around any desired point using mouse.



qawded
05-11-2013, 03:33 AM
Hello everyone.
I want to know that how i can rotate an object about a point where I click on the screen.
Currently I am able to rotate it about the center of the screen.

My code is as following. Please suggest a simple way to modify this for the desired results.

void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
mouseDown = true;

xdiff = x - yrot;
ydiff = -y + xrot;
}
else
mouseDown = false;
}


void mouseMotion(int x, int y)
{
if (mouseDown)
{
yrot = x - xdiff;
xrot = y + ydiff;

glutPostRedisplay();
}
}

and these lines in my display function:
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(yrot, 0.0f, 1.0f, 0.0f);

Please help.

tonyo_au
05-11-2013, 06:43 PM
You need to translate the origin to the point, apply the rotate then translate the origin back

qawded
05-12-2013, 01:38 PM
You need to translate the origin to the point, apply the rotate then translate the origin back

ok. how to get the coordinates of that point?

tonyo_au
05-13-2013, 12:15 AM
A point on the screen is actually a line in 3D. You are best to do a google on picking in 3D because it is a topic with a lot of solutions all of which have limitations.