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;
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.