I have a terrain surface and i wanna make pan on it.
I want to click in the terrain and move it,
but i want to keep this point always under the pointer of the mouse.
I have tried to calculate it using the projection and modelview matrix. What i tried was
to get the point where i click (the point of the
mesh) and then try to calculate what translation i needed to make the projection of this point in the position of the pixel of the mouse pointer. But i didn’t be able to do it.
Any help would be appreciated, thanks in advance.
(Sorry for my horrible english)
assuming you want to pan in the x-y-plane. use gluProject to compute the screen coordinates of the following points:
O = (0, 0, 0) -> Screen_O = (Ox, Oy, Oz)
X = (1, 0, 0) -> Screen_X = (Xx, Xy, Xz)
Y = (0, 1, 0) -> Screen_Y = (Yx, Yy, Yz)
this gives you information about how the global x and y axis unit vectors are mapped on the screen:
X = [Xx-Ox, Xy-Oy]
Y = [Yx-Ox, Yy-Oy]
now, if you move the mouse pointer by
delta_mouse = [dx, dy]
you can calculate how to split delta_mouse into the directions of X and Y:
delta_mouse = [dx, dy] = aX + by
you have 2 equations for the coefficients a, b and can calculate them.
finally, translate your scene by
glTranslate(a, b, 0);
hope that helps
A lot of thanks! Now i’ll be able to make a correct pan.
did you already make it work, or are you just optimistic
two more remarks: first, i used this with an ortho view. it may not work with perspective views. a vector (1, 0, 0) drawn in the middle of the screen will look different from a vector (1, 0, 0) drawn at the top left corner of the screen.
second, for accuracy reasons it may be better to map the points X=(10, 0, 0) and y=(0, 10, 0) instead of the ones with unit length. in that case, the translation would be glTranslatef(a/10., b/10., 0.)
Well, i think i have a solution
I know what are you saying. But my idea is make it for each pixel i have to move. The problem is that the size of a unit depends on the distance from de camera (10 units at 50 units of distance will not take the same pixels that 10 units a 100 units of distance).
But i think i can use the idea whit a little difference. I’ll try to calculate each time the
units i have to translate for move 1 pixel (i think i can calculate it).
Then i’ll do it recursively for each pixel i have to move for all delta distance.
Find the distance as i only wanna move for 1 pixel, and do it for n pixels.
Do you think that it works?