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.