views:

45

answers:

2

When the program I wrote starts up, I have some vertices in a window. The viewport is set to take up the entire window. On a mouse click, the distance from the mouse position to each of the vertices is calculated. If any of them are within a certain threshold (let's say 5 units), the vertex is selected. This part works with no problem.

After a window resize, however, I restrict the viewport of the new window in order to maintain aspect ratio. This also works.

However, after resizing, trying to click on vertices produces weird behavior, since the mouse position doesn't seem to align to world coordinates anymore. How can I convert the mouse position to the world coordinate system that the x/y values of these vertices is stored?

I've seen this question asked before, but in a 3D context. The fix for that seems to be to use gluUnproject. I'm not sure if that's applicable to a 2D program. Any help would be appreciated!

Here is the code to my resize function:

//resize function; used to maintain aspect ratio
void resize(int w, int h)
{
    screenHeight = h;
    screenWidth = w;

    int width, height;

    //taller
    if ((float)(w/h) < R)
    {
        width = w;
        height = w/R;
    }
    //wider
    else
    {
        width = h*R;
        height = h;
    }

    widthBound = width;
    heightBound = height;

    glViewport(0, 0, width, height);
} //end resize

I've tried resetting the projection matrix inside the code, by doing:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(left, right, bottom, top);

Where left, right, bottom, and top are the limits of my new viewport. I've tried doing those both before and after setting up the new viewport. Doesn't seem to fix the issue.

A: 

From the comments it appears your problem is recalculating your projection when your window is resized.

Just call glOrtho() after the resize (making sure to set the matrix mode to GL_PROJECTION) and you should be good to go.

Ron Warholic
A: 

Check this:

screenHeight = h;
screenWidth = w;

// ...

widthBound = width;
heightBound = height;

Do you use screen*** or ***Bound to transform mouse coordinates? this may be an issue since they hold different sizes with aspect-ratio enforcing and are equal otherwise.

ybungalobill