PDA

View Full Version : Picking trouble



David Doria
11-04-2008, 05:31 PM
I am trying to select a face of a cube with the mouse.

here is my cube function:



GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};

void DrawCube()
{
glPushName(1);
glColor3f(1.0f, 0.0f, 0.0f); // Color Red
polygon(0,3,2,1);
glPopName();

glPushName(2);
glColor3f(0.0f,1.0f,0.0f); // Color Green
polygon(2,3,7,6);

glPopName();

glPushName(3);
glColor3f(0.0f, 0.0f, 1.0f); // Color Blue
polygon(0,4,7,3);
glPopName();

glPushName(4);
glColor3f(1.0f,1.0f,0.0f); // Color Yellow
polygon(1,2,6,5);
glPopName();


glPushName(5);
glColor3f(0.0f,1.0f,1.0f); // Color Cyan
polygon(4,5,6,7);
glPopName();

glPushName(6);
glColor3f(1.0f,0.0f,1.0f); // Color Magenta
polygon(0,1,5,4);
glPopName();
}

void polygon(int a, int b, int c , int d)
{
glBegin(GL_QUADS);
glVertex3fv(vertices[a]);
glVertex3fv(vertices[B]);
glVertex3fv(vertices[c]);
glVertex3fv(vertices[d]);
glEnd();
}


I call the picking function with:


double newy = glutGet(GLUT_WINDOW_HEIGHT) - 1 - y;
zprPick(x, newy, 3, 3);

because you have to invert the y coordinate returned by the window.

here is the picking function itself



void zprPick(GLdouble x, GLdouble y,GLdouble delX, GLdouble delY)
{
GLuint buffer[1024];
const int bufferSize = sizeof(buffer)/sizeof(GLuint);

GLint viewport[4];
GLdouble projection[16];

GLint hits;
GLint i,j,k;

GLint min = -1;
GLuint minZ = -1;

glSelectBuffer(bufferSize,buffer); /* Selection buffer for hit records */
glRenderMode(GL_SELECT); /* OpenGL selection mode */
glInitNames(); /* Clear OpenGL name stack */

glMatrixMode(GL_PROJECTION);
glPushMatrix(); /* Push current projection matrix */
glGetIntegerv(GL_VIEWPORT,viewport); /* Get the current viewport size */
glGetDoublev(GL_PROJECTION_MATRIX,projection); /* Get the projection matrix */
glLoadIdentity(); /* Reset the projection matrix */
gluPickMatrix(x,y,delX,delY,viewport); /* Set the picking matrix */
glMultMatrixd(projection); /* Apply projection matrix */

glMatrixMode(GL_MODELVIEW);

/* Draw the scene in selection mode */
display();

hits = glRenderMode(GL_RENDER); /* Return to normal rendering mode */

/* Determine the nearest hit */
if (hits)
{
for (i=0,j=0; i<hits; i++)
{
if (buffer[j+1]<minZ)
{
/* If name stack is empty, return -1 */
/* If name stack is not empty, return top-most name */

if (buffer[j]==0)
min = -1;
else
min = buffer[j+2+buffer[j]];

minZ = buffer[j+1];
}

j += buffer[j] + 3;
}
}

glMatrixMode(GL_PROJECTION);
glPopMatrix(); /* Restore projection matrix */
glMatrixMode(GL_MODELVIEW);

pick(min); /* Pass pick event back to application */
}


I took it directly out of ZPR (the picking works properly when I use all of zpr (ie. returns 1, 2, 3... 6 when i choose one of the cube faces)) but not when I use it on its own like this. Here it returns an integer from 1 to 6, but it doesn't even correspond to when I click the cube at all. Anywhere I click it seems to return 2 sometimes or other times 4, but seemingly with no respect to where I clicked.

Can anyone spot a problem? Or have a nice working picking function?

Thanks,

Dave