OpenGL Zoom to cursor issue

This topic is 1992 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello,

I have a 2D application which is an image viewer.
I have the ability to pan around the image in the window and I have a basic zoom.
Now I'd like to improve the zoom so it zoom to the point under the mouse.
So far I've read everything possible on google and i've come close to something working,
but still can't find something perfect, here is what I got:

glMatrixMode (GL_PROJECTION);

// w and h are respectivly the width  and height of the window
// newzoom_pos is the point in openGL coordinates  where the user requested the zoom
// zoomFactor is between [0.1,10.0]  , 0.1 -> 1.0 means downscale, 1.0 -> 10.0 means upscale
// transX and transY are used to pan around the image
float left = (0.f+transX -newzoom_pos.x())/zoomFactor  +newzoom_pos.x();
float right = (w+transX -newzoom_pos.x())/zoomFactor +newzoom_pos.x();
float bottom = (h-transY-newzoom_pos.y())/zoomFactor +newzoom_pos.y();
float top = (0.f-transY -newzoom_pos.y())/zoomFactor +newzoom_pos.y();

glOrtho(left, right, top, bottom, -1, 1);

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();

it works well when the texture is filling all the window : i.e when the user zoom in a lot.
But when the image is small and does not fit yet in the viewer entirely ( there's black around), well it is messy and does not work...

btw, here is the code when the user zoom-in, in the mouse wheel event handler:

zoomFactor+=0.1;
QPoint oglpos = openGLpos(new_zoomPoint.x(), new_zoomPoint.y());
this->newzoom_pos = oglpos;

the openGLpos function is the following:

QPoint ViewerGL::openGLpos(int x,int y){
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX=0, winY=0, winZ=0;
GLdouble posX=0, posY=0, posZ=0;
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
winX = (float)x;
winY = viewport[3]- y;
if(winY == 0) winY =1.f;
glReadPixels( x, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
GLint b=gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
if(b==GLU_FALSE) cout << "failed unprojection" << endl;
return QPoint(posX,posY);
}



Could anybody help me through this?

Alex

Share on other sites

help, i'm still stuck on this please !

Share on other sites

If by "messy" you mean that there are pieces of the image hanging around outside the area where the actual image belongs, you're probably forgetting a call to glClear(). If that's not it, then I'm afraid that I can't do much without some more information about what it's doing wrong.

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
gaxio
10

• 14
• 30
• 13
• 11
• 11
• Forum Statistics

• Total Topics
631778
• Total Posts
3002309
×