Results 1 to 4 of 4

Thread: Drag an object with mouse

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2006
    Location
    UK
    Posts
    2

    Drag an object with mouse

    The code below is supposed to drag the object when the mouse is clicked and moved. I've an array called "objects" that store the coordinates of the objects. Basically I just compared the value and find out the object I'm pointing at. But when I run the program, it's always the first object that is chosen, become a very small dot on the screen and finally disappeared. Would you please help me?

    Code :
     int ox, oy, j;
    int currObj;
     
    void mouse(int button, int status, int x, int y)
    {  int i;
     
       if ((button == GLUT_LEFT_BUTTON) && (status == GLUT_DOWN))
        {LMPressed = true;
         ox = x;
         oy = y;
         for (i=0; i< Objnum + 1; i++)
         { if (((ox > objects[i].Objmax.x) &amp;&amp; (ox < objects[i].Objmax.x)) &amp;&amp;
                   ((oy > objects[i].Objmax.y) &amp;&amp; (oy < objects[i].Objmin.y)))
            j = i;       
         }
        }
          else 
        LMPressed = false;
     
    }
     
    void mouseMotion(int x, int y)
    {
       if (LMPressed) {
         objects[j].Objmin.x = x;
         objects[j].Objmin.y = y;
         objects[j].Objmax.x = x + 0.4;
         objects[j].Objmax.y = y + 0.4;
         glutPostRedisplay();
       }    
    }
    I've actually tried to use the "Picking" as well but it doesn't work either, here's the code

    Code :
    void mouse(int button, int status, int x, int y)
    {  int i;
     
       if ((button == GLUT_LEFT_BUTTON) &amp;&amp; (status == GLUT_DOWN))
        {LMPressed = true;
    }
     
    void proces****s(GLint hits, GLuint buffer[], int x, int y)
    { 
         unsigned int i;
         int a;
         double z;
         GLuint names, *prt;
     
         prt = (GLuint *) buffer;
         for ( i = 0; i<hits; i++) 
         {
             names = *prt;
             a = (int) names;
             prt++;
             z = (double) *prt/0x7fffffff;
     
             objects[a].Objmin.x = (double) x;
             objects[a].Objmin.y = (double) y;
             objects[a].Objmin.z = z;
             objects[a].Objmax.x = (double) x + 0.4;
             objects[a].Objmax.y = (double) y + 0.4;
             objects[a].Objmax.z = z +0.4;
         }
    }     
     
    void mouseMotion(int x, int y)
    {    
        GLuint selectBuf[BUFSIZE];
        GLint  hits;
        GLint viewport[4];
     
        if (LMPressed == true)
        { 
          glGetIntegerv(GL_VIEWPORT, viewport);
          glSelectBuffer(BUFSIZE, selectBuf);
          (void) glRenderMode(GL_SELECT);
     
          glInitNames();
          glPushName(0);
     
          glMatrixMode(GL_PROJECTION);
          glPushMatrix();
          glLoadIdentity();
          gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y), 0.4, 0.4, viewport);
          gluOrtho2D(0.0, 3.0, 0.0, 3.0);
          drawObject(GL_SELECT, Objnum);
     
          glMatrixMode(GL_PROJECTION);
          glPopMatrix();
          glFlush();
     
          hits = glRenderMode(GL_RENDER);
          proces****s(hits, selectBuf, x , y);
          glutPostRedisplay();
        }
    }

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    659

    Re: Drag an object with mouse

    Code :
    if (((ox > objects[i].Objmax.x) &amp;&amp; (ox < objects[i].Objmax.x)) &amp;&amp;
                   ((oy > objects[i].Objmax.y) &amp;&amp; (oy < objects[i].Objmin.y)))
    you're using objects[i].Objmax.x twice. i think one should be objects[i].Objmin.x

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2006
    Location
    UK
    Posts
    2

    Re: Drag an object with mouse

    Hi RigidBody,

    Thanks for pointing out that mistake,I've now change it to

    Code :
     if (((ox > objects[i].Objmin.x) &amp;&amp; (ox < objects[i].Objmax.x)) &amp;&amp;
                   ((oy > objects[i].Objmin.y) &amp;&amp; (oy < objects[i].Objmax.y)))
    But the problems still remain. One thing I don't understand is, since I didn't change the value of z coordinate of the object, why the object will become so far away from camera? It's originally a cube and become a dot far away when I drag it! And again, it's always the first object the program has chosen.

  4. #4
    Member Newbie
    Join Date
    Jan 2002
    Location
    Steamboat Springs, CO, USA
    Posts
    43

    Re: Drag an object with mouse

    My guess is you're fouling up your modelview/projection matrices somewhere. I'd double-check all the matrix-related calls, including glMatrixMode().

    Also, use glGetError() to make sure you're not over/underflowing any matrix stacks.

Similar Threads

  1. Drag object based on mouse selection
    By gwartney in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 05-15-2016, 10:15 PM
  2. 3D Mouse Drag
    By bobGL in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 11-27-2008, 12:00 PM
  3. rotate on mouse click and drag in the direction of the mouse cursor
    By vamsee kanth in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 01-02-2005, 09:54 PM
  4. How to drag object in opengl by mouse?
    By tgtt in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 02-14-2004, 01:20 PM
  5. drag object with mouse?
    By detzel in forum OpenGL: Advanced Coding
    Replies: 9
    Last Post: 11-12-2002, 07:46 PM

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