tags:

views:

45

answers:

1

To zoom into the mouse position I was using:

glTranslatef(current.ScalePoint.x,current.ScalePoint.y,0);
    glScalef(current.ScaleFactor,current.ScaleFactor,current.ScaleFactor);
    glTranslatef(-current.ScalePoint.x,-current.ScalePoint.y,0);

so basically I translate to the new origin (the mouse position) then scale by the current scale factor, then translate back.

This kind of works generally well, but it can be a bit buggy. My issue is really that now I'v introduced a camera offset so I tried something like this:

glTranslatef(controls.MainGlFrame.GetCameraX(),
    controls.MainGlFrame.GetCameraY(),0);
glTranslatef(current.ScalePoint.x,current.ScalePoint.y,0);


glScalef(current.ScaleFactor,current.ScaleFactor,current.ScaleFactor);
glTranslatef(-current.ScalePoint.x,-current.ScalePoint.y,0);

But this did not work as I intended. How could I properly do this knowing that:

The matrix's origin is the top left corner (0,0)

1 unit == 1 pixel

My scale factor

My camera's position relative to how far it is from (0,0) (the origin) and

the mouse position (screen to client).

Thanks

A: 

It is more safe (and also for code reuse) to un-project the mouse coordinate point (from window coordinates to model coordinates) first even though you know how projection is done.

You can use the following function:

void unProject(int ix, int iy, int &ox, int &oy)
{
 // First, ensure that your OpenGL context is the selected one
 GLint viewport[4];
 GLdouble projection[16];
 GLdouble modelview[16];

 glGetIntegerv(GL_VIEWPORT, viewport);
 glGetDoublev(GL_PROJECTION_MATRIX, projection);
 glGetDoublev(GL_MODELVIEW_MATRIX, modelview);

 int xx = ix;
 int yy = viewport[3] - iy;
 GLdouble x, y, z;
 gluUnProject(xx, yy, 0 /*check*/, modelview, projection, viewport, &x, &y, &z);

 ox = (int) x;
 oy = (int) y;
}

The output then is the correct point on the model coordinates for your zooming

mmonem