Skip to main content
GameDev.net gamedev.net
🔒 Locked

OpenGL 2D Selection Scaling Multiple Times

Started by Jevi Dec 23, 2012 at 11:58 PM 0 replies 2.1k views
Original Post
Jevi
Jevi

I am trying to scale into a base image multiple times. So I should be able to from a base image which has no scale factor (or scale factor of 1) scale into it and then from that scaled image generated from the base image I should be able to scale into it again and again and again. My problem is that I do not know how to calculate what the x and y coordinate of the mouse should be when trying to select a region from a scaled image. Here are some images that will describe my situation.

*Note that all the red coordinates are what the x and y coordinates should be but the mouse x and y coordinates are always from 0,0 to 640,480 ( the display dimensions )

This is my base image. Note the stating x and y coordinates.

[attachment=12896:original.png]

This is the selection of the portion that will be scaled from the original x and y coordinates

[attachment=12897:originalzooming.png]

This is the result of that first selection scaled

[attachment=12899:zoomedin.png]

I want to be able to do another selection scale from this image but as I stated I don't know how to calculate what the x and y coordinates should translate to.

This is an example of what I should be able to do

[attachment=12898:zommedinzooming.png]

Note that I am also translating the image to the center of the selection so that the scaled in image will be centered in the display.

This is what I am currently doing to scale from the base image to a scaled image. *note that this does not work when trying to scale and already scaled image as I said because I need to calculate what the actual x and y coordinates of the mouse should be. The first 4 lines is where I presume the calculations of the actual x and y coordinates should be.



				// modify mouse coordinates to fit scaled image
				_mouseStart.x = _mouseStart.x;
				_mouseStart.y = _mouseStart.y;
				_mouseEnd.x = _mouseEnd.x;
				_mouseEnd.y = _mouseEnd.y;

				// get width and height of selection
				float ratio = (float) SCREEN_WIDTH / (float) SCREEN_HEIGHT;
				float newFracWidth = fabs(_mouseEnd.x - _mouseStart.x);
				float newFracHeight = newFracWidth / ratio;

				if (newFracWidth > 0 && newFracHeight > 0)
				{
					Vertex starts;
					Vertex ends;

					if (_mouseStart.x > _mouseEnd.x)
					{
						starts.x = _mouseStart.x - newFracWidth;
						starts.y = _mouseStart.y - newFracHeight;

						ends.x = _mouseStart.x;
						ends.y = _mouseStart.y;

						_fracNormalCenter.x = _mouseStart.x - (newFracWidth / 2);
						_fracNormalCenter.y = _mouseStart.y - (newFracHeight / 2);
					}
					else
					{
						starts.x = _mouseStart.x;
						starts.y = _mouseStart.y;

						ends.x = _mouseStart.x + newFracWidth;
						ends.y = _mouseStart.y + newFracHeight;

						_fracNormalCenter.x = _mouseStart.x + (newFracWidth / 2);
						_fracNormalCenter.y = _mouseStart.y + (newFracHeight / 2);
					}
					
					_portionStart.x = starts.x;
					_portionStart.y = starts.y;
					_portionEnd.x = ends.x;
					_portionEnd.y = ends.y;

					cout << "glOrtho Dimension: : " << _portionStart.x << " , " << _portionStart.y << " -- " << _portionEnd.x << " , " << _portionEnd.y << endl;

					starts = NormalToComplex(starts);
					ends = NormalToComplex(ends);

					_scaleFactor = (SCREEN_WIDTH / newFracWidth);

					// set new complex display dimensions
					_fracStart.x = starts.x;
					_fracStart.y = starts.y;
					_fracEnd.x = ends.x;
					_fracEnd.y = ends.y;

					_pointOffSet.x = (SCREEN_WIDTH / 2) - _fracNormalCenter.x;
					_pointOffSet.y = (SCREEN_HEIGHT / 2) - _fracNormalCenter.y;

This is how I set the new scaled image


        glLoadIdentity();
	glOrtho(_portionStart.x, _portionEnd.x, _portionEnd.y, _portionStart.y, 1, -1);

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.