How does zooming, panning, and rotation work?

Using OpenGL I'm trying to draw a primitive map of my campus.

Can someone explain to me how panning, zooming and rotating are usually done?

For example, with panning and zooming, am I just adjusting my viewport? So I draw and draw all my lines that make up my map, and then when the user clicks and drags it, changes the viewport?

For panning, shifts the x / y values ​​of my viewport, and for scaling, does it increase or decrease my viewport by some amount? What about rotation?

For rotation, do I need to do affine transformations for each polyline that represents my campus map? Wouldn't it cost a lot on the fly on a decent sized map?

Or, does the viewport remain the same, but does pan / zoom / rotate in a different order?


For example, if you go to this link , you will see that it describes the panning and zooming of exactly what I have above by changing the viewing area.

It is not right?

+6
rotation viewport zoom opengl pan
source share
3 answers

They are achieved by applying a series of glTranslate, glRotate commands (which represent camera position and orientation) before drawing the scene. (technically, you rotate the whole scene!)

There are utility functions such as gluLookAt that sort some details about this.

To simplify things, suppose you have two vectors representing your camera: position and direction.

gluLookAt takes position, destination and vector up.

If you are implementing a vector class, destinaion = position + direction should indicate the destination.

Again, to make things simple, you can assume that the up vector is always (0,1,0)

Then, before rendering anything in your scene, load the identification matrix and call gluLookAt

 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt( source.x, source.y, source.z, destination.x, destination.y, destination.z, 0, 1, 0 ); 

Then start drawing objects

You can let the user skip by slightly changing the position to the right or left. Rotating is a bit trickier as you need to rotate the direction vector. Assuming you're spinning, this is a camera, not some object in the scene.

One of the problems is that you only have a forward direction vector, how do you move it? where are the right and left?

My approach in this case is to simply take the cross product of the “direction” and (0,1,0).

Now you can move the camera left and right using something like:

 position = position + right * amount; //amount < 0 moves to the left 

You can move forward using the "direction vector", but IMO is better to limit the movement to a horizontal plane, so get the vector forward just like we got the correct vector:

 forward = cross( up, right ) 

Honestly, this is a somewhat hacky approach.

The right approach is to use a more “complex” data structure to represent the “orientation” of the camera, not just the front direction. However, since you're just getting started, it's good to do things one at a time.

+7
source share

All these "actions" can be achieved using the transformation functions of the model matrix. You should read about glTranslatef (panning), glScalef (scaling), glRotatef (rotation). You also need to read the basic OpenGL tutorial, you can find this link .

+4
source share

Typically, there are three steps that apply whenever you refer to any point in three-dimensional space inside opengl.

For local point

  • Local → World Transform
  • World → Camera Conversion
  • Camera → Screen Transformation (usually the projection depends on whether you use perspective or orthogonally)

Each of these transformations takes your three-dimensional point and is multiplied by a matrix.

When you rotate the camera, it usually changes the world → camera transformation, multiplying the transformation matrix by the affine rotation / pan / zoom transformation. Since all your points are redrawn every frame, a new matrix is ​​applied to your points and gives a rotation look.

+1
source share

All Articles