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?

 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) && (ox < objects[i].Objmax.x)) &&
               ((oy > objects[i].Objmax.y) && (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

 
void mouse(int button, int status, int x, int y)
{  int i;

   if ((button == GLUT_LEFT_BUTTON) && (status == GLUT_DOWN))
    {LMPressed = true;
}

void processHits(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);
      processHits(hits, selectBuf, x , y);
      glutPostRedisplay();
    }
} 
if (((ox > objects[i].Objmax.x) && (ox < objects[i].Objmax.x)) &&
               ((oy > objects[i].Objmax.y) && (oy < objects[i].Objmin.y)))

you’re using objects[i].Objmax.x twice. i think one should be objects[i].Objmin.x

Hi RigidBody,

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

 if (((ox > objects[i].Objmin.x) && (ox < objects[i].Objmax.x)) &&
               ((oy > objects[i].Objmin.y) && (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.

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.