PDA

View Full Version : Picking and points



gbiv
10-24-2006, 01:41 AM
Hi all. I've been going at this all nite. I thought the selection stuff would be straight forward. not so much...here is the code I basically copied from this tutorial:
http://www.lighthouse3d.com/opengl/picking/index.php?openglway2


void PointMesh::Selection()const
{
GLint viewport[4];
GLuint selectionBuffer[512];
int hits;

glSelectBuffer(512,selectionBuffer);
glRenderMode(GL_SELECT);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glGetIntegerv(GL_VIEWPORT,viewport);
gluPickMatrix(viewport[2]*_mouse[0],viewport[3]*_mouse[1],
150.0,150.0,viewport);
gluPerspective(45,(GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]),0.1,1000);
glMatrixMode(GL_MODELVIEW);
glInitNames();

_drawUVPoints();

// restoring the original projection matrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glFlush();

// returning to normal rendering mode
hits = glRenderMode(GL_RENDER);
GLenum glerr = glGetError();
while(glerr != GL_NO_ERROR) {
std::cout<<"glGetError:"<<gluErrorString(glerr)<<std::endl;
glerr = glGetError();
}
if (hits > 0)
{
std::cout<<"Hit a ctrl point"<<std::endl;
/*int choose = selectionBuffer[3];
int depth = selectionBuffer[1];

for (int loop = 1; loop < hits; loop++)
{
// If This Object Is Closer To Us Than The One We Have Selected
if (selectionBuffer[loop*4+1] < GLuint(depth))
{
choose = selectionBuffer[loop*4+3];
depth = selectionBuffer[loop*4+1];
}
}*/
}
} and here is the referenced _drawUVPoints() function:


void PointMesh::_drawUVPoints()const
{
//u iso-curves
if(!_selection)
{
glEnable(GL_POINT_SMOOTH);

}
glPointSize(5.0);


for(unsigned int v = 0; v < _numVControlPoints; v++)
{
for(unsigned int u = 0; u < _numUControlPoints; u++)
{
glLoadName(v*_numUControlPoints + u);
glBegin(GL_POINTS);
glVertex3f(_controlPoints->at(v*_numUControlPoints + u).X(),
_controlPoints->at(v*_numUControlPoints + u).Y(),
_controlPoints->at(v*_numUControlPoints + u).Z());
glEnd();
}
}


if(!_selection)
{
glDisable(GL_POINT_SMOOTH);
}
} When I try selecting with the mouse, I get no hits and I also see a duplicate (un-correctly positioned) set of points. Clicking the mouse in different areas of the window moves the duplicate set of points around in the window... Not sure what's going on...does anyone see anything blatently wrong with my code? :confused:
Thanks in advance
biv
Oh, I have the mouse coords already "transformed" to gl window coords by the time I reach this function but my window is not full screen. I also have an obscene size for my selection window but I was trying to debug.

yalmar
10-24-2006, 06:31 AM
Should check/save the current matrix states (GL_MODELVIEW, GL_PROJECTION), I see that you modify the matrix states for picking


glGetIntegerv(GL_VIEWPORT,viewport); gluPickMatrix(viewport[2]*_mouse[0],viewport[3]*_mouse[1],
150.0,150.0,viewport);
gluPerspective(45,(GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]),0.1,1000);
glMatrixMode(GL_MODELVIEW); After the picking process, before of renderize anything, you should come back to original matrix states.

gbiv
10-24-2006, 07:02 AM
Thanks for the response. I added a push/pop for my modelview matrix but I'm still seeing the same results. I also tried commenting out the call to

glRenderMode(GL_SELECT)and the app behaved the same way...Is there something that I'm doing wrong to enable selection?

Y-tension
10-31-2006, 05:54 AM
OK, here's my guess:I've implemented picking in openGL on windows and there are some caveats:
First, check that your mouse coordinates are in the range 0-1(Your code requires that it is so).
Have in mind that WM_LBUTTONDOWN(I think) that intercepts mouse events in windows provides a different mapping to window coordinates than openGL,counting from the top of the window(I think)instead of the lower left,so an inversion is necessary..

gbiv
11-07-2006, 06:15 AM
I figured out my problem...The problem is that I was in advertently trying to do selection (i.e. glRenderMode calls) within a display list...whoops... Thanks for the advise