1

I'm trying to move objects with the mouse using the Gluunproject method with openGL 2.1 , but i'm really struggling here; here's the code i wrote :

int viewport[4];
double  modelview[16],
       projection[16],
       X1, Y1, Z1;
double realY;
    GLfloat depth[2];

    for(_compt=_OjebctScene.begin();_compt!=_OjebctScene.end();_compt++)
    {

    if ((*_compt)->IsSelected())
    {
        GLdouble mouseX=event.x;
        GLdouble mouseY=event.y;

      glGetIntegerv(GL_VIEWPORT, viewport);
      glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
      glGetDoublev(GL_PROJECTION_MATRIX, projection);
      realY = viewport[3] - (GLint) mouseY - 1;
      glReadPixels(mouseX, realY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, depth);
      gluUnProject(mouseX, realY, 0, modelview, projection, viewport, &X1, &Y1, &Z1);
      (*_compt)->setX(X1);
      (*_compt)->setY(Y1);
      (*_compt)->setZ(Z1);
    }
}

I use a loop to check all the objects on the scene (I've pushed them into a vector) , then when i find the selected object , i try to move it using the mouse.

I then set the coordinates of my object to the position of the mouse in the 3D space (X1 , Y1 , Z1); but this doesn't really work.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • What does "doesnt' really work" mean? – derhass Jul 18 '15 at 16:38
  • the objects do not move like expected , they "jump" from right to left , without really following the movement of the mouse. – HdjoWattever Jul 18 '15 at 18:59
  • Well. There is lot's of stuff unclear. What modelview matrix is actually set at the time when you call this? How does the position you set with these `setX()` etc. methods get transformed during rendering? Why do you read back the depth buffer value at the mouese position? Why do you unproject a point on the near plane? – derhass Jul 18 '15 at 19:29
  • Well , I'm just learning openGL by myself with what I can find on the net , and this is what I found after doing some researches about how to move an object with the mouse . I understand that the gluunproject gives you the coordinates of the mouse in the 3D space , but nothing more than this , i don't have a deep understanding in the matter to be honest. the modelview is the Id matrix i guess (I always make sure to pop it) the SetX() method modifies the coordinates of the object and every time i render my objects , i use those attributes via accessors . – HdjoWattever Jul 18 '15 at 20:54

1 Answers1

0

I think I've fixed it just by passing the depth[0] to the gluunproject method. here's what I've done , change this :

  gluUnProject(mouseX, realY, 0, modelview, projection, viewport, &X1, &Y1, &Z1);

by this :

  gluUnProject(mouseX, realY,depth[0], modelview, projection, viewport, &X1, &Y1, &Z1);

I don't understand what this means though , if someone could explain it to me it would be nice.