Results 1 to 6 of 6

Thread: Moving selected objects in OpenGL

  1. #1
    Member Contributor
    Join Date
    Jun 2009
    Posts
    60

    Moving selected objects in OpenGL

    Hi,
    I am trying to develop a CAD like application using OpenGL and wxWidgets... I have reached at a stage where i can draw a circle, square, load an image, polygon etc.. am also able to zoom and pan..

    as a further step, i want to move object, scale it and rotate.. to do this i have written a code to select and pick object that lies under mouse pointer.. Now what do i have to do to move the picked object..?? I tried by finding the difference of old and new points and recalculating the points of the object..


    C.x += dx;
    C.y+= dy;

    where dx and dy are differences of old and new x, and y values resp..

    This is showing some movement. but not as desired.. it isnt moving along with the mouse movement.. For a small mouse movement, it shows a huge displacement and also very fast... so what can be done to make a selected object move along wiith the mouse pointer...??

    Also i'm not able to zoom with reference to the mouse pointer.. it zooms with reference to the origin (0,0,0) only.. please help me..

    Thanks in advance..

    Regards
    Rakesh Patil

  2. #2
    Member Contributor
    Join Date
    Jun 2009
    Posts
    60

    Re: Moving selected objects in OpenGL

    I guess my way of explaining problem is not clear.. DO i need to explaing again..??

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2009
    Posts
    14

    Re: Moving selected objects in OpenGL

    Are you using the glutMotionFunc ?

    What you need to do is check whenever the mouse is moved AND if the player is holding the button you use to move objects. You also need to check if the user has already clicked on an object to move and is then holding the mouse to move it.

    Here's the steps:
    1. User mouse clicks down on object
    2. Call function with mouse down
    3. User keeps mouse held down and moves mouse
    4. Call function with mouse motion
    5. User stops holding mouse down
    6. Call function with mouse up


    Does that help?

  4. #4
    Member Contributor
    Join Date
    Jun 2009
    Posts
    90

    Re: Moving selected objects in OpenGL

    void reshape(int w, int h)
    {
    glViewport(0, 0, (GLint) w, (GLint) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if ( h==0 )
    gluPerspective(45, (GLdouble)w, 1.0, 2000.0);
    else
    gluPerspective(45, (GLdouble)w/ (GLdouble)h, 1.0, 2000.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

    void mouse ( int button, int state, int x, int y )
    {
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
    moving = 1;
    startx = x;
    starty = y;
    }
    if ( button == GLUT_LEFT_BUTTON && state == GLUT_UP )
    moving = 0;

    if ( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN )
    {
    moving = 2;
    starty = y;
    }
    if ( button == GLUT_RIGHT_BUTTON && state == GLUT_UP )
    moving = 0;
    }


    void motion ( int x, int y )
    {
    if ( moving == 1 )
    {
    angle = angle + ( x - startx );
    angle2 = angle2 + ( y - starty );
    startx = x;
    starty = y;
    glutPostRedisplay ( );
    }
    if ( moving == 2 )
    {
    zoom = zoom + ( y - starty );
    starty = y;
    glutPostRedisplay ( );
    }
    }



    And make sure you call
    glutMouseFunc ( mouse );
    glutMotionFunc ( motion );

    after init() in your main loop


    And needed globals

    float angle, angle2, cloud_rot;
    float zoom = -25.50;

    int moving, startx, starty;

  5. #5
    Member Contributor
    Join Date
    Jun 2009
    Posts
    60

    Re: Moving selected objects in OpenGL

    @ Salbris
    am not using glutMotionFunc..

    Infact am using VC++ 2008 along with wxWidgets and OpenGL.. So I got MouseEvents through which i can manage various mouse events..

    @SeanAustin
    Thanks for the trouble but what does that code does for right down event.?? zooming..?? If so does it gives me the desired effect.?? Secondly I'm using gluOrtho instead of gluPerspective.. So would der be any change in that case.?? Is 'glustPostRedisplay function similar to refreshing the canvas..?? what are angle and angle2..?? what should be their initial values..?? can they be used for rotating.??

    THanks again for both of you guys..

    Kind Regards
    Rakesh Patil

  6. #6
    Junior Member Newbie
    Join Date
    Jul 2009
    Posts
    14

    Re: Moving selected objects in OpenGL

    After reading you first post again it seems you might just have the speed set too high. You said: "For little mouse movement there is a huge displacement and very fast."

    There could be two problems here:

    1. The speed that the mouse moves the object is too fast.

    2. Or your not updating the position of the object every tick.

Similar Threads

  1. Replies: 2
    Last Post: 11-08-2017, 03:59 PM
  2. moving selected object with mouse
    By sumaia in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 09-18-2015, 06:19 AM
  3. How to implement in OpenGL selected of 3D objects
    By ddguan in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-13-2015, 03:59 PM
  4. Moving selected objects in OpenGL
    By alucard_ad in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-31-2012, 02:26 PM
  5. Moving a selected object in 3D space.
    By etharmon in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 06-25-2000, 11:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean