Hello,
it is my first time I use picking in my OpenGL application. I found a tutorial of Lighthouse 3D and implemented it. It works if I don’t move the camera position. However the looking direction can be changed and picking still works.
But when I change gluLookAt(…) in render mode and then want to pick again, the picking does not work anymore. It seems as if the position of the camera is not calculated to get the picked objects.
Here’s the code:
private void startPicking()
{
selectBuf = new uint[1024];
int[] viewport = new int[4];
float ratio;
Gl.glSelectBuffer(1024, selectBuf);
Gl.glGetIntegerv(Gl.GL_VIEWPORT, viewport);
Gl.glRenderMode(Gl.GL_SELECT);
Gl.glInitNames();
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glPushMatrix();
Gl.glLoadIdentity();
// I tried this but it does not work so I commented it out
/*Glu.gluLookAt(camPosition.X, camPosition.Y, camPosition.Z,
camPosition.X + camDirection.X, camPosition.Y + camDirection.Y, camPosition.Z + camDirection.Z,
camUp.X, camUp.Y, camUp.Z);*/
Glu.gluPickMatrix(currentMousePos.X, viewport[3] - currentMousePos.Y, 5, 5, viewport);
ratio = (float)viewport[2] / (float) viewport[3];
Glu.gluPerspective(fovY, ratio, zNear, zFar);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
}
private void stopPicking()
{
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glPopMatrix();
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glFlush();
hits = Gl.glRenderMode(Gl.GL_RENDER);
uint pickedID = 0;
if (hits != 0)
{
pickedID = getPickedID(hits, selectBuf);
}
selectionMode = false;
if (hits != 0)
{
if (landscapeChart[pickedID].GetType().Equals(typeof(HillTerrainTile)))
processPick(pickedID);
}
}
Can anyone help me with this?