Dragging a vertex with the mouse.


My goal is to enable a user to “drag” a vertex with the mouse from the displayed mesh, in the X/Y plane only (Z is not changed).

That mesh may be zoomed (Zoom), rotated along Z or X axis (RotZ and RotX) and translated along X or Y axis to [CX,CY].
It is drawn with these transformations :

glScalef(Zoom, -Zoom, -Zoom);
glRotatef(RotX, 1,0,0);
glRotatef(RotZ, 0,0,1);
glTranslatef(-CX, -CY*RatioHL, 0);
glScalef(1, RatioHL, ZScale);

(RatioHL and ZScale are constants from the mesh defines, RotX is only from 90° to 180° to give view from side view to top view)

I succesfully find the vertex that is near the mouse by using GL_SELECT.

After a long time searching in helps and forums, I though I success to compute the new X/Y coordinates of the vertex from the mouse position (in top view only). Halas! there is a small error, that grows while the mouse goes far from the viewport center.

At zoom = 100 to 1000, the error is quite insignifiant. When zoom grows until 20000 the error become visible.
At zoom = 50000 the vertex move twice less than the mouse, especially when the point is far from the viewport
At zoom = 100k, result is wrong enough to disable any drag…

I compute the X/Y vertex coordinate from the mouse position with that :

glGetIntegerv(GL_VIEWPORT, @ViewPort);
glGetDoublev(GL_MODELVIEW_MATRIX, @ModelViewMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, @ProjectionMatrix);

gluProject(Pov.X, Pov.Y, Pov.Z, @ModelViewMatrix, @ProjectionMatrix, @Viewport, Ax,Ay,Az);

gluUnProject(Mouse.X, Viewport[3]-Mouse.Y, Az, @ModelViewMatrix, @ProjectionMatrix, @Viewport, Result.X, Result.Y, Dummy);

  • Pov is the point of view of the camera = (0, 0, 60) as used by gluLookAt.
  • Ax,Ay,Az are temp variable
  • Result.X, Result.Y are the new vertice coord.

I missed something. Probably something related to “zoom” value, but what ? Isn’t it fully integrated in the ModelView & Projection matrix ?

Of course, I even missed two things because the computed coordinates are wrong if the view is not exactely from top (RotX<>180).

Any help would be welcome…