I’m using SDL and OpenGL for this project. My screen at program start up is 640 x 480 and so is my glOrtho setup. So the program starts out synchronized. But my program allows the user to create a selection with the mouse of a portion of the display to zoom into. For example, a user could select the area from 320,240 (_mouseStart) to 640,480 (_mouseEnd). This selection area is then passed into glOrtho so that it can center and zoom into the selected area. My problem is when I try to select an area of an already zoomed in area because SDL mouse coordinates are in the range from 0,0 to 640,480 instead of the new area selected which in this case was 320, 240 to 640,480. I need a way to calculate what the real mouse position is when trying to select and already zoomed in area.
I could not post pictures here but I have created a post in gamedev.net which you could refer to if you need more information. http://www.gamedev.net/topic/636257-opengl-2d-selection-scaling-multiple-times/
Here is where I calculate all the mouse position stuff after the user has selected an area (WHICH IS NOW CORRECT)
// modify mouse coordinates to fit scaled image
_mouseStart.x = ((_mouseStart.x / SCREEN_WIDTH) * (_portionEnd.x - _portionStart.x)) + _portionStart.x;
_mouseStart.y = ((_mouseStart.y / SCREEN_HEIGHT) * (_portionEnd.y - _portionStart.y)) + _portionStart.y;
_mouseEnd.x = ((_mouseEnd.x / SCREEN_WIDTH) * (_portionEnd.x - _portionStart.x)) + _portionStart.x;
_mouseEnd.y = ((_mouseEnd.y / SCREEN_HEIGHT) * (_portionEnd.y - _portionStart.y)) + _portionStart.y;
Here is how I reset glOrtho to center and display the selected area.
glLoadIdentity();
glOrtho(_portionStart.x, _portionEnd.x, _portionEnd.y, _portionStart.y, 1, -1);
I created an image which explains the process (i hope)
The red coordinates at the red rectangle are the coordinates you should get from your mouse position. You can use this coordinates to compute the values for your current zoomed viewport (the green coordinates). To get the final unzoomed coordinates (the blue values) you have to add the offset of the green rectangle (228,88).
Please note that my example isn’t the best because the offset of the green rectangle and the height of the green rectangle are identical (both values are 228)