Sign in to follow this  

Problems Converting Screen Coordinates With 2D Zoom

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 mode
glMatrixMode(GL_PROJECTION);
// save previous matrix which contains the
//settings for the perspective projection
glPushMatrix();
// reset matrix
glLoadIdentity();
// set a 2D orthographic projection
gluOrtho2D(0, w, 0, h);
// invert the y axis, down is positive
glScalef(1, -1, 1);
// mover the origin from the bottom left corner
// to the upper left corner
int 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 mode
glMatrixMode(GL_PROJECTION);
// save previous matrix which contains the
//settings for the perspective projection
glPushMatrix();
// reset matrix
glLoadIdentity();
// set a 2D orthographic projection
gluOrtho2D(50.0*zoom, w, 50.0*zoom, h);
// invert the y axis, down is positive
glScalef(1, -1, 1);
// mover the origin from the bottom left corner
// to the upper left corner
glTranslatef(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]

Share this post


Link to post
Share on other sites
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...)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by ajm113
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.
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.

Share this post


Link to post
Share on other sites
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 out
if(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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this