Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Rotating an object around any desired point using mouse.

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    23

    Rotating an object around any desired point using mouse.

    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.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    You need to translate the origin to the point, apply the rotate then translate the origin back

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    23
    Quote Originally Posted by tonyo_au View Post
    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?

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •