I'm sure there is a solution to this somewhere, but I can't find it--and I'm tired of looking for hours and hours, so here goes.
I'm trying to convert window to graph coordinates. Yes, I've seen the tutorial, and the tons of posts on this same issue. But I think my problem is rooted in the fact that the values returned in the call to
glGetIntegerv(GL_VIEWPORT, viewport);
are inconsistent (they mostly switch back and forth between a few different sets of values), and I have yet to see anyone post about this problem. My code is very short, and I'm doing 2D so that should make it simpler.
If it makes any difference, this is in a QT opengl widget. And if anyone knows of a post that addresses this issue, feel free to just point me in that direction instead of explaining everything again.
resize function:
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, Width, 0, Height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
On click function (straight out of NeHe tutorial):
glLoadIdentity();
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
winX = (float)event->pos().x();
winY = (float)viewport[3] - (float)event->pos().y();
glReadPixels(event->pos().x(), int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
My data looks like this (first 4 values is viewport array, last 3 are posX, posY, and posZ):
0 0 239 191 1.83224 -0.355086 -6.54942
0 0 240 191 1.72944 -0.337781 -6.23023
0 0 1250 28 -0.45097 -0.0961251 -0.0345631
0 0 239 191 1.50305 -0.291288 -5.37269
0 0 240 191 1.82233 -0.355923 -6.56485
0 0 240 191 1.78512 -0.348657 -6.43084
Thanks