Mouse scaling, factoring in camera translation? (Opengl)

Here is my problem, I have a scale that is an undesigned mouse position. I also have a "camera that basically converts all objects in X and Y. I want to do this to zoom in to the mouse position.

I tried this:

1. Find the mouse x and y coordinates 2. Translate by (x,y,0) to put the origin at those coordinates 3. Scale by your desired vector (i,j,k) 4. Translate by (-x,-y,0) to put the origin back at the top left 

But this does not affect the translation of the camera.

How can I do it right. Thanks

 glTranslatef(controls.MainGlFrame.GetCameraX(), controls.MainGlFrame.GetCameraY(),0); glTranslatef(current.ScalePoint.x,current.ScalePoint.y,0); glScalef(current.ScaleFactor,current.ScaleFactor,0); glTranslatef(-current.ScalePoint.x,-current.ScalePoint.y,0); 
+4
source share
1 answer

Instead of using glTranslate to move all objects, you should try glOrtho . As parameters, the required left coordinates, correct coordinates, lower coordinates, upper coordinates and minimum / maximum depth are required.

For example, if you call glOrtho (-5, 5, -2, 2, ...); all points whose coordinates are inside the rectangle going from (-5,2) to (5, -2) will be displayed on your screen. The advantage is that you can easily adjust the zoom level.

If you do not multiply by any kind of view / projection matrix (I assume that it is), the standard screen coordinates range from (-1.1) to (1, -1).

But in your project it can be very useful to control the camera. Call this before you draw any object instead of glTranslate:

 float left = cameraX - zoomLevel * 2; float right = cameraX + zoomLevel * 2; float top = cameraY + zoomLevel * 2; float bottom = cameraY - zoomLevel * 2; glOrtho(left, right, bottom, top, -1.f, 1.f); 

Note that cameraX and cameraY now represent the center of the screen.

Now, when you zoom in on a point, you just need to do something like this:

 cameraX += (cameraX - screenX) * 0.5f; cameraY += (cameraY - screenY) * 0.5f; zoomLevel += 0.5f; 
+3
source

Source: https://habr.com/ru/post/1316241/


All Articles