Hello everyone, I’ve been having some probablems lately with my object picking code in which there are no hits detected if I rotate my camera. Things work fine (most of the time) if I just translate the camera but as soon as I start flipping the camera upside down or rotate it a certain way things go crazy. Here is the code that I’m using for picking, can anyone tell me whats wrong with what I have??
void Mouse(int button, int state, int x, int y)
{
Camera *pPQ = camera; // pointer to the PQ field of the camera...
VRp3 rp;
VRq rq;
float matrix[16]; // matrix to apply the camera transformation
unsigned int selectbuf[512];
int hits;
GLint viewport[4];
int mod = glutGetModifiers();
switch(button)
{
case GLUT_LEFT_BUTTON:
if(state == GLUT_DOWN && mod == GLUT_ACTIVE_ALT)
{
glClear(GL_DEPTH_BUFFER_BIT);
glGetIntegerv(GL_VIEWPORT, viewport); // store viewport information in an array
glSelectBuffer(512, selectbuf);
glViewport(0, 0, 570, 355);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glInitNames(); // clear the name stack
glLoadIdentity();
glRenderMode(GL_SELECT); // switch to select mode (nothing is drawn)
gluPickMatrix(x, viewport[3]-y, 1, 1, viewport); // create an area around the mouse
gluPerspective(pPQ->fieldofview*m_180pi, pPQ->aspect, pPQ->nearclip, pPQ->farclip);
tomatrix(-pPQ->position, ~pPQ->orientation, matrix);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(matrix);
root->Draw(VRp3(), VRq(), GL_SELECT);
hits = glRenderMode(GL_RENDER); // return the amout of selected objects
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
// loop through all the hits and find the one closest to the one we selected
if(hits > 0)
{
currNode = (SceneNode*)(selectbuf[3]);
unsigned int depth = selectbuf[1];
if(currNode->selected == false)
{
currNode->selected = true;
}
else
{
currNode->selected = false;
}
for(int n = 1; n < hits; n++)
{
if(selectbuf[n*4+1] < depth)
{
currNode = (SceneNode*)(selectbuf[n*4+3]);
depth = selectbuf[n*4+1];
if(currNode->selected == false)
{
currNode->selected = true;
}
else
{
currNode->selected = false;
}
}
}
}
}
break;
default:
break;
}
glutPostRedisplay();
}