Zoom / zoom the openGL camera around the point and pan

I implemented a camera (model view) in my 2D opengl application, and I have some problems with zooming around a point, as well as zooming around a point in combination with panning. Both work with the mouse. Panning works by dragging the mouse and zooming to the point of increase / decrease around the current location of the mouse.

Panning works fine while I haven't scaled around the point yet. If I scale around a point, that is, the initial โ€œjumpโ€ in panning, but then it works smoothly. Scaling around a point works fine for the first point, but for subsequent points the scene is converted once, and then it is scaled around the current mouse point. The scale around the point code looks like this:

public void scaleToPoint(float scale, float x, float y, float z) { root.markAsDirty(); y = drawableHeight - y - 1; // (S(Inverse(T)) matrix.m03 = -x; matrix.m13 = -y; matrix.m23 = -z; matrix.m00 = matrix.m11 = matrix.m22 = scale; matrix.m03 *= matrix.m00; matrix.m13 *= matrix.m11; matrix.m23 *= matrix.m22; matrix.m03 += x; matrix.m13 += y; matrix.m23 += z; } 

The panning code simply sets up the matrix translation component. It seems that in both cases I need to somehow cancel the previous translation in order to avoid the jump, but I donโ€™t see how to do it. Thanks.

Nick

Posted in response to comment:

Matrix

is javax.matrix.Matrix4f .

Bread Code:

 public void pan(Vector3f pan) { root.markAsDirty(); matrix.setTranslation(pan); } 

where matrix.setTranslation sets the translation component of the matrix ie:

 matrix.m03 = trans.x; matrix.m13 = trans.y; matirx.m23 = trans.z; 

The matrix is โ€‹โ€‹loaded using glLoadMatrix () to set the topmost matrix on the stack. The expected output is the usual smooth panorama without additional transition in the case of panning and without a jump to scaling / scaling in case of scaling. By jump, I mean that the scene changes unexpectedly. So, for example, if the scene is one big square, and the mouse is located in the center of the square, the square should move in the direction of drag by the mouse, while the center of the square remains under the mouse. When jumping, the mouse will begin to center the square, but the beginning of the panorama will move the scene so that the other part of the square is now under the mouse. The leap in scaling works in a similar way. The first zoom moves the scene so that the location that was originally under the mouse is now no longer under the mouse.

Note that panning works fine if zooming around the point did not happen. Panning also works great if scaling is not around a specific point.

+4
source share
1 answer

Your problem is that instead of doing a delta translation, you just install the translation. However, this works when it comes to panning, however, when you scale, you donโ€™t store the translation because of the scale, and it is deleted the next time the translation is done. So, what would I do, instead of installing the translation, I would increase it. Therefore, when you drag the mouse, get the delta from the two gluUnProject calls, and then add this delta to the matrix transform values. This will prevent it from deleting the minor translation created by the scale around the point.

So:

 public void pan(Vector3f delta_pan) { root.markAsDirty(); matrix.m03 += delta_pan.x; matrix.m13 += delta_pan.y; matirx.m23 += delta_pan.z; } 
+1
source