View Full Version : View coords to world coords problem

02-19-2002, 02:33 AM

I have a mesh (with irregular triangles) and I try to click mouse over a triangle and report interpolated height. I have implemented a selection-pick. It works fine, I know which triangle was clicked. But I have problems finding interpolated height.

I create a ray (calling gluUnproject two times), and I find intersection point between ray and plane, but intersection point wasn't expected world-coords. Someone can help me? Any idea?

I'm moving center's world to origin and I'm using a gluPerspective.

The code:

CTriangle *pTri = m_pD->m_aTIN.GetAt(selectedObject);

GLdouble projectmatrix[16];
GLdouble modelmatrix[16];
GLdouble objx, objy, objz;

// Offset to move center world to origin
double xdif = m_pGuiView->m_volume.m_dXMin + ((m_pGuiView->m_volume.m_dXMax-m_pGuiView->m_volume.m_dXMin) / 2.0);
double ydif = m_pGuiView->m_volume.m_dZMin;
double zdif = m_pGuiView->m_volume.m_dYMin + ((m_pGuiView->m_volume.m_dYMax-m_pGuiView->m_volume.m_dYMin) / 2.0);

glGetIntegerv(GL_VIEWPORT, viewportCoords); // Get the current view port coordinates
glGetDoublev(GL_MODELVIEW_MATRIX, modelmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projectmatrix);

GLint realy = viewportCoords[3] - (GLint) y;
ASSERT(gluUnProject((GLdouble) x, (GLdouble) realy, 0.0, modelmatrix, projectmatrix, viewportCoords, &objx, &objy, &objz) == TRUE);
vNear = CVector(objx , objy, objz);

ASSERT(gluUnProject((GLdouble) x, (GLdouble) realy, 1.0, modelmatrix, projectmatrix, viewportCoords, &objx, &objy, &objz) == TRUE);
vFar = CVector(objx, objy, objz);

CVector vIntersection;

// Apply model transformation and projection to triangle
CVector v1, v2, v3;
ProjectTriangle(pTri, projectmatrix, modelmatrix, v1, v2, v3);

// Find Intersection
if(IntersectedPolygon(v1, v2, v3, vNear, vFar, vIntersection) == true)
TRACE3("\tIntersection coords: (%g, %g, %g)\n",

If I defined a traingle (1,1,100)(5,1,100)(2.5,6,100) and I click in the middle (aprox), I expect to receive (2.5, 3.0, 100) or an aproximation, but I receive (2.29363, 2.81264, 1.40916)

Thanks in advance!

[This message has been edited by NoAntz (edited 02-19-2002).]

[This message has been edited by NoAntz (edited 02-19-2002).]

[This message has been edited by NoAntz (edited 02-19-2002).]