PDA

View Full Version : Picking and moving the camera position



Rapthor
01-22-2007, 03:09 PM
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?

Flavious
01-22-2007, 06:59 PM
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

Flavious
01-22-2007, 07:09 PM
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...

Rapthor
01-23-2007, 01:45 AM
Originally posted by Flavious:
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... 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();

Rapthor
01-24-2007, 09:32 AM
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.