   Problems Converting Screen Coordinates With 2D Zoom
  Hello I'm having a problem with getting the correct cursor position is located when my camera zooms in the 2D scene. I've spent days google searching and playing with the variables, but I can't get it too work right and zoom is going too be a very important feature in my 2D image program. I'm currently using OpenGL for the 2D. I'm wondering if it's the way I could have Ortho set up, but I'm not sure since I'm using two Ortho projections.Example when I try too determine the mouse position with the zoom:int ImageHalfWidth = RenderImageWidth / 2;int ImageHalfHeight = RenderImageHeight / 2;int halfw = (windowWidth / 2) - ImageHalfWidth;int halfh = (windowHeight / 2) - ImageHalfHeight;Vector2D vec = Vector2D(PreviewZoom*(MouseCurPos.m_X-ViewPos.m_X-halfw), PreviewZoom*(MouseCurPos.m_Y-ViewPos.m_Y-halfh));Vector2D vec2 = Vector2D(PreviewZoom*(MouseLastPos.m_X-ViewPos.m_X-halfw), PreviewZoom*(MouseLastPos.m_Y-ViewPos.m_Y-halfh));ViewPos is the camera position over the image, and RenderImageWidth and RenderImageHeight is the image being displayed in the center of the screen the user draws on. Lets say both those values are 512.Here is how my 2D Ortho is set up:Set2DDrawImage(windowWidth*PreviewZoom, windowHeight*PreviewZoom, RenderImageWidth, RenderImageHeight);//Draw workspace here! Layers, the image, etc..Set2DDraw(windowWidth, windowHeight, 0);//Normal 2D drawing of tool bars, buttons, etc with OpenGL.void Set2DDrawImage(int w, int h, int ImageWidth, int ImageHeight){// switch to projection modeglMatrixMode(GL_PROJECTION);// save previous matrix which contains the //settings for the perspective projectionglPushMatrix();// reset matrixglLoadIdentity();// set a 2D orthographic projectiongluOrtho2D(0, w, 0, h);// invert the y axis, down is positiveglScalef(1, -1, 1);// mover the origin from the bottom left corner// to the upper left cornerint ImageHalfWidth = ImageWidth / 2;int ImageHalfHeight = ImageHeight / 2;int halfw = (w / 2) - ImageHalfWidth;int halfh = (h / 2) + ImageHalfHeight;glTranslatef(halfw, -halfh, 0);glMatrixMode(GL_MODELVIEW);}void Set2DDraw(int w, int h, float zoom){// switch to projection modeglMatrixMode(GL_PROJECTION);// save previous matrix which contains the //settings for the perspective projectionglPushMatrix();// reset matrixglLoadIdentity();// set a 2D orthographic projectiongluOrtho2D(50.0*zoom, w, 50.0*zoom, h);// invert the y axis, down is positiveglScalef(1, -1, 1);// mover the origin from the bottom left corner// to the upper left cornerglTranslatef(0, -h, 0);glMatrixMode(GL_MODELVIEW);}Thanks for reading, I'll appreciate any feedback, thanks![Edited by - ajm113 on September 1, 2010 11:32:40 PM]
  I didn't look at the code carefully, but I'll just offer the comment that if a) you handle zoom by adjusting the parameters of the orthographic projection rather than via scaling, and b) you use traditional picking methods based on 'point unprojection' to yield your pick ray, then you should have no problem correctly computing the pick point. It looks to me like you're currently trying to compute the world-space pick point manually, which usually isn't necessary.(Apologies if none of that is relevant to the question...)   ;  Are you sure? Because I'm doing nothing in 3D if that's what your thinking about, it's all 2D coordinates... I'm only just zooming on a 2D image, I was wondering how to get the position of the mouse after the camera zoomed on the 2D Image.   ;Quote:Original post by ajm113Are you sure? Because I'm doing nothing in 3D if that's what your thinking about, it's all 2D coordinates... I'm only just zooming on a 2D image, I was wondering how to get the position of the mouse after the camera zoomed on the 2D Image.Yeah, the usual method for picking will still work, even if the projection is orthographic and the camera is axis-aligned. It's still 3D - it's just a special case.But yes, you can certainly do it 'manually' if you want by mapping the mouse coordinate from screen space to whatever space you're interested in using simple arithmetic. I'm not completely clear on what exact problem you're trying to solve here though, so without digging into your code in detail I couldn't really tell you what the specific solution would be here.   ;  I'm guesting you want me too look into glUnproject, but I kinda feel more comfortable not using it quit yet.Alright well I'll add a bit more detail then:Well basically PreviewZoom is incremented like so://dir is the mouse wheel direction.if (dir > 0)    { //Zoom in... if(PreviewZoom > 0.14999984)   PreviewZoom -= 0.05;    }    else    {   //Zoom outif(PreviewZoom < 4.8500004)  PreviewZoom += 0.05;    }The problem is that when I try too zoom in or out that means the coordinates of where I try too click on the screen will be off. Is their a certain why I have to multiply PreviewZoom to go along with the "halfw" or "halfh" so that the position of the mouse cursor is always with the zoom? I don't know what else to give you, you pretty much have the hole zoom source code now and how the workspace Ortho code looks like where the user draws.   ;Quote:I don't know what else to give you, you pretty much have the hole zoom source code now and how the workspace Ortho code looks like where the user draws.Yeah, I know, but like I said, I'd probably have to dig into the code to find the problem (whatever the issue is, it's not immediately obvious to me from glancing over the code).Maybe someone else will take that on, but in the meantime, I'd suggest trying to break the problem down into sub-problems to try to narrow things down a bit. I don't know how your whole system works exactly, but it sounds like maybe picking works in some cases (such as when no zoom is applied), but not in others. One debugging method would be to set up a case where you know what the results should be, and then step through the code in the debugger to see where the actual results deviate from the expected results. In this way, you might be able to narrow things down somewhat.   ;  Well I pretty much already concluded that the Set2DDrawImage was messing the coordinates up, such as the mouse pos and the screen pos of the Set2DDrawImage is smaller then the "Set2DDraw" which would cause the mouse always to be off in the Set2DDrawImage Ortho. So I'm just going to do this:m_Draw.Set2DDraw(windowWidth*PreviewZoom, windowHeight*PreviewZoom, 0);glPushMatrix();glLoadIdentity();//Then to figure out the position with the zoom I just do this:Vector2D vec = Vector2D(MouseCurPos.m_X*PreviewZoom, MouseCurPos.m_Y*PreviewZoom);Although now the camera won't zoom in the center of the screen so that causes a new problem, but I may just go around it by making the zoom go with the position of the mouse by panning the camera when it zooms.   