Mouseposition on heightmaps ?

I like to read the mouseposition on a heightmap but it doesn’t work.

Here is the code to read the mouseposition after drawing:

GLdouble winz;
glReadPixels(MouseX, MouseY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winz);
GLdouble objx, objy, objz;
GLint viewport[4];
GLdouble modelMatrix[16];
GLdouble projMatrix[16];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
gluUnProject((GLdouble)MouseX, (GLdouble)MouseY, winz, modelMatrix, projMatrix, viewport, &objx, &objy, &objz);
double x = objx;
double y = objy;
double z = objz;
SetWindowText(g_window->hWnd, (FloatToStr(float(x)) + " - " + FloatToStr(float(y)) + " - " + FloatToStr(z)).c_str());  

If I follow a line with my mouse the Y-Position shouldn’t change but it does. (Even if there is no different height on the heightmap)

Is there maybe an example how to read the mouseposition ?

In your message handler mouse postion can be queried like this:

  point.x = LOWORD(lParam);
  point.y = HIWORD(lParam);
  return 0;

And somewhere else in the code this is the unproject:

  glGetIntegerv(GL_VIEWPORT, viewport);
  glGetDoublev(GL_MODELVIEW_MATRIX, dmatModelview);
  glGetDoublev(GL_PROJECTION_MATRIX, dmatProjection);
  // Adjust handedness.
  y = viewport[3] - 1 - point.y;
  glReadPixels(point.x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &fWindowZ);
  gluUnProject((GLdouble) point.x, (GLdouble) y, (GLdouble) fWindowZ,
               dmatModelview, dmatProjection, viewport,
               &dWorldX, &dWorldY, &dWorldZ);

“If I follow a line with my mouse the Y-Position shouldn’t change but it does. (Even if there is no different height on the heightmap)”

Did you rotate the object?

[This message has been edited by Relic (edited 05-07-2003).]

Originally posted by Relic:
Did you rotate the object?

Yes! How can I get the position even if I turn it (or turn the camera) ?

The unproject code from above returns the object coordinates before the transformation.
Sounds more like you want the world coordinates after the modelview transformation?
Just multiply the returned point by the modelview matrix then. Is that it?
(Sorry, for the confusing dWorldXYZ parameter names in my previous post. Should have been object coordinates.)

Sorry - Don’t know really how you mean it. I’m a beginner with OpenGL (using it for 3D) ^^"

The Modelview is an array of 16 doubles.
What should I multiply with what ? o.O

But thanks already ^^

Waah x.x why doesn’t it work.
I now found the function
you told me. But it doesn’t work.

Now I always get the same value on X and Z. Y is the pixelposition of the mouse like in a 2D game.

Or - if I move the position of the positioncheck I get a overflow.

Would you have a look on it when I send the code to you (It isn’t the longest currently and I would say you where to find what)
I really need it and I can’t find any examples or tutorials for it.