Picking and moving the camera position

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?

For one thing I think you need to put the pick matrix on the projection matrix stack, and before you multiply by perspective. LookAt is usually multiplied on the modelview stack.

Actually what you have looks good to me, but maybe I missed something (it’s been a while since I used picking). Does the lighthouse demo not work?

Cheers

FYI, all the pick matrix does is zoom the pick rectangle to cover the entire viewport, so all that gets rendered is what falls within the new, constrained frustum.

Hope that helps…

Originally posted by Flavious:
[b] FYI, all the pick matrix does is zoom the pick rectangle to cover the entire viewport, so all that gets rendered is what falls within the new, constrained frustum.

Hope that helps… [/b]
Hi,

What I do between the start and stop method is the actual rendering of the scene. I’m using a gluLookAt() there already. May be I am handling the matrices in a wrong way.
I’m fairly new to that topic, so could you please tell me where I have to put which commands?

I’d appreciate your help:

            startPicking();
            Gl.glLoadIdentity();                 // Reset The Current Modelview Matrix         

            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);

            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer

            Gl.glPolygonMode(Gl.GL_FRONT, Gl.GL_FILL);
            drawAllTheStuff();
            stopPicking();
            Gl.glFlush();

Ok I had some odd lines of code that generated my IDs. Now it works. You have to be aware of the names on the name stack and their corresponding objects.