title: Convert SDL window position to OpenGL world position
body: <p>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.</p>
  
  <p>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. <a href="http://www.gamedev.net/topic/636257-opengl-2d-selection-scaling-multiple-times/" rel="nofollow">http://www.gamedev.net/topic/636257-opengl-2d-selection-scaling-multiple-times/</a></p>
  
  <p>Here is where I calculate all the mouse position stuff after the user has selected an area (WHICH IS NOW CORRECT)</p>
  
  <pre><code>// 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;
  </code></pre>
  
  <p>Here is how I reset glOrtho to center and display the selected area.</p>
  
  <pre><code>glLoadIdentity();
  glOrtho(_portionStart.x, _portionEnd.x, _portionEnd.y, _portionStart.y, 1, -1);
  </code></pre>