Skip to main content
GameDev.net gamedev.net
🔒 Locked

Picking / Selection problem.

Started by DarkDemon Dec 18, 2008 at 7:24 PM 0 replies 500+ views
Original Post
DarkDemon
DarkDemon
Hello, I've been trying to implement picking / selection into my project. I started using this tutorial to get the basics in http://www.lighthouse3d.com/opengl/picking/index.php?openglway The problem is it doesn't appear to work, I get rather strange results / inaccurate ones. I've tried changing the volume of the picking region but it hasn't changed any results. This is the current code I use in a class called viewer.cpp

void Viewer::ProcessSelection(int xPos, int yPos){

GLuint selectBuf[BUFSIZE];
GLint viewport[4];
int hits;

	glSelectBuffer(BUFSIZE,selectBuf);
	glRenderMode(GL_SELECT);

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

	glGetIntegerv(GL_VIEWPORT,viewport);
	gluPickMatrix(xPos,viewport[3]-yPos,5,5,viewport);
	gluPerspective(45.0f, fAspect(), 1.0, 425.0);
	glMatrixMode(GL_MODELVIEW);
	glInitNames();

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

	Display();
	
	// returning to normal rendering mode
	hits = glRenderMode(GL_RENDER);
	
	// if there are hits process them
	if(hits!=0){
			processHits(hits,selectBuf);

	}
	
}

void Viewer::processHits (GLint hits, GLuint buffer[])
{
   unsigned int i, j;
   GLuint names, *ptr, minZ,*ptrNames, numberOfNames;

   printf ("hits = %d\n", hits);
   ptr = (GLuint *) buffer;
   minZ = 0xffffffff;
   for (i = 0; i < hits; i++) {	
      names = *ptr;
	  ptr++;
	  if (*ptr < minZ) {
		  numberOfNames = names;
		  minZ = *ptr;
		  ptrNames = ptr+2;
	  }
	  
	  ptr += names+2;
	}
   printf ("Item id hit: "); 

/* got rid of the for loop from the tut only require the one name */
  ptr = ptrNames;

  printf ("%d ", *ptr);
  
  printf ("\n");
   
}


I assign names in another class called scene.cpp

void Scene::render(){

glInitNames();

  for(int i=0;i<nObjects;i++)
  {
      glPushMatrix();
      glPushName(i);
      myObject.draw();
      glPopName();
      glPopMatrix();
  }

}

The problem is if I click anywhere I get 0 displayed as if I've selected object 0, but I haven't. If I click anywhere I always get 0 returned even if I click an object I'll get 0. If I make the objects move around the screen however and I keep clicking in a random place it simply cycles through different IDs on the names stack. I think this maybe down to this tutorial code and the ways it's picking but I'm not totally sure hence why I'm posting here. Is there a simply and easy way just to poll which hit has been detected because for some reason the results I get are very inaccurate. Thanks in advance.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.