Selection with ray from mouse


I just want to have a ray from the mouse …
I use this:


CVector_4D Ray;

GLint viewportCoords[4] = {0};
GLdouble modelMatrix[16] = {0};
GLdouble projMatrix[16] = {0};

//gluLookAt …

glGetIntegerv(GL_VIEWPORT, viewportCoords);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);

double vecx, vecy, vecz;
gluUnProject(xmouse, viewportCoords[3] - ymouse, 0.0, modelMatrix, projMatrix, viewportCoords, &vecx, &vecy, &vecz);

double vecx2, vecy2, vecz2;
gluUnProject(xmouse, viewportCoords[3] - ymouse, 1.0, modelMatrix, projMatrix, viewportCoords, &vecx2, &vecy2, &vecz2);

Ray.x = vecx2 - vecx;
Ray.y = vecy2 - vecy;
Ray.z = vecz2 - vecz;

But it doesn’t seams to work
Can you help me ? :wink:


Your code segment looks fine.
What is the return value of gluunproject?
What are the resultant values in your ray variables?

glUnProject return GL_TRUE two times …

And the values of ray seems to be good … but it work badly …
One time, it work and if i move or turn the camera, it does’nt work …

Maybe there’s another way to have this ray ?
Another method ?


You haven’t got your camera in the projection matrix, by any chance?