Hello. I am writing a 2D simulator for an machine learning algorithm and I’m trying to achieve panning and zooming. I know there are already many questions on this, but this is a bit more specific. I need to be able to zoom and pan like in Google Maps and Openstreetmap. I am also having trouble converting from window coordinates to world coordinates and vice versa.
Right now I am using glScalef and glTranslatef on the model view matrix to pan and zoom rather than changing the projection matrix because some elements in the simulator are GUI elements. Zooming/panning should not be applied to them. Panning works fine, but zooming only works on the first zoom point because after zooming, I cannot convert the window coordinates of the mouse to world coordinates properly. I noticed that even if the coordinates were correct, zooming in this way causes the whole world to jump to the new zoom point. Although I am trying to get it to zoom to one point, then the next.
My guess to apply this kind of zooming is keeping a list of zoom points and performing them in order, but this seems wasteful. I have also seen an answer on a Q&A site where code provided did not keep a list. Maybe there is a better way with a single glTranslatef and glScalef call?
Before I added zooming, I converted from window to world coordinates like this: window.x -= camPos.x, where camPos is used like this: glTranslatef(-camPos.x, -camPos.y, 0.0f). This was fine for just panning. For zooming (which is not working) I calculate the distance to the zoom point, then multiply by the zoom factor. Then I undo the panning effect like in the first example. However this seems to have no effect, and the coordinates are converted as if there was no zoom.
In case it is unclear, the camera transformations using glTranslatef and glScalef are used at the beginning of the render function before the first glPushMatrix call. GUI elements simply discard camera transformations by using glLoadIdentity. Before drawing every element, glPushMatrix is called, and of course popped after drawing with glPopMatrix.