Reputation: 11006
I have set up openGL, so that the origin is at the top left corner. However, using gluUnProject to convert mouse coordinates to OpenGL coordinates, still gives me coordinates where the origin is at the bottom left corner and I am not sure why that is. So, OpenGL is setup as follows:
void setup_GL()
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// bottom and top flipped to change origin
glOrtho(0, width, height, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
The function to convert screen coordinates to OpenGL coordinates is as:
Point get_gl_from_screen(int x, int y)
{
set_up_view();
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat z;
GLdouble px, py, pz;
glGetDoublev(GL_MODELVIEW_MATRIX, modelview );
glGetDoublev(GL_PROJECTION_MATRIX, projection );
glGetIntegerv(GL_VIEWPORT, viewport);
gluUnProject(x, y, z, modelview, projection, viewport, &px, &py, &pz);
return Point(px, py);
}
This returns py still as if origin is bottom left. Not sure why. Note that the input coordinates are also with respect to the origin at upper left corner.
Upvotes: 1
Views: 335
Reputation: 45352
OpenGL's window space is always defined to have the origin at bottom left corner. Using some projection matrix will not flip this. It will only add a mirroring component to the transformation from eye to clip space (where later window coords are derived from). So if you feed gluUnporject
with a point at y=0, you are asking for the object space coordinates for something which appeared at the bottom row after rendering.
In your case, this just means that due to that ortho matrix (and with identity
as ModelView), you will get y=height
as the result when you ask for that.
Upvotes: 2