Jump to content
  • Advertisement
Sign in to follow this  
dbrayford

OpenGL picking problems

This topic is 3494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to pick in OpenGL using code that was working a week ago. I changed the drawing function and now picking doesn't work and returns invalid enum error from glSelectBuffer(BUFSIZE, selectBuf) and RenderMode(GL_RENDER). I've been unable to determine where I've gone wrong and any help would be most welcome.
// picking code
void StartPicking() 
{	
	std::cout << "Entering StartPicking" << std::endl;

	int viewport[4];
	float ratio;

	glSelectBuffer(BUFSIZE, selectBuf);

	error = glGetError();
	if(error)
		std::cout << "Error code after glSelectBuffer(BUFSIZE, selectBuf) is : " << error << std::endl;

	glGetIntegerv(GL_VIEWPORT, viewport);

	error = glGetError();
	if(error)
		std::cout << "Error code after glGetIntegerv(GL_VIEWPORT, viewport) is : " << error << std::endl;

	glRenderMode(GL_SELECT);

	error = glGetError();
	if(error)
		std::cout << "Error code after glRenderMode(GL_SELECT) is : " << error << std::endl;

	glInitNames();

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

	std::cout << "picking x cursor value : " << startDragX << std::endl;
	std::cout << "picking y cursor value : " << startDragY << std::endl;

	gluPickMatrix(startDragX, viewport[3]-startDragY, 5, 5, viewport);
	ratio = ( (float)viewport[2] + 0.0f ) / (float)viewport[3];
	gluPerspective(45, ratio, 0.1f, 1000);
	glMatrixMode(GL_MODELVIEW);
}

int StopPicking() 
{
	std::cout << "Entering StopPicking" << std::endl;
	int uid;
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glFlush();
	hits = glRenderMode(GL_RENDER);
	
	error = glGetError();
	if(error)
		std::cout << "Error code after RenderMode(GL_RENDER) is : " << error << std::endl;

	std::cout << "hits : " << hits << std::endl;
	if (hits != 0)
	{
		uid = GetUIDOfHit(hits, selectBuf, 0);
		return uid;
	}
	return -1;
	drawMode = RENDER;
}

int GetUIDOfHit (int hits, unsigned int buffer[], int sw)
{
	std::cout << "Entering GetUIDOfHit" << std::endl;

   int i, numberOfNames;
   unsigned int names, *ptr, minZ,*ptrNames;

   ptr = (GLuint *) buffer;
   minZ = 0xffffffff;
   for (i = 0; i < hits; i++) 
   {	
      names = *ptr;
	  ptr++;
	  if (*ptr < minZ) 
	  {		  	
		  std::cout << "the branch (*ptr < minZ) is true" << std::endl;
		  numberOfNames = names;
		  std::cout << "numberOfNames : " << numberOfNames << std::endl;
		  minZ = *ptr;
		  ptrNames = ptr+2;
	  }
	  
	  ptr += names+2;
	}

   if (numberOfNames > 0) 
   {
	   	std::string str_message = "uid is : ";
		std::string str_num = IntToString(*ptrNames);
		str_num += "\n";
		str_message += str_num;
		std::cout << str_message << std::endl;
	   return *ptrNames;
   }
   else
   {
	   return -1;
   }
   return 1;
}


// drawing code
void PickTest()
{
		glPushMatrix();
		glTranslatef(0.0f, 0.0f, 0.0f);
		
		glDisable(GL_LIGHTING);
		glDisable(GL_LIGHT0);
		
		glPushMatrix();
		glPushName(1);
		glTranslatef(0.7f, 0.2f, 15.0f);						
			glBegin(GL_QUADS);									
				glColor4f(0.0f, 1.0f, 0.0f, 0.4f);
				glVertex3f(-1.0f, 1.0f, 0.0f);					
				glColor4f(1.0f, 0.0f, 0.0f, 0.4f);
				glVertex3f( 1.0f, 1.0f, 0.0f);					
				glColor4f(0.0f, 0.0f, 1.0f, 0.4f);
				glVertex3f( 1.0f,-1.0f, 0.0f);					
				glColor4f(1.0f, 1.0f, 0.0f, 0.4f);
				glVertex3f(-1.0f,-1.0f, 0.0f);					
			glEnd();
		glPopName();
		glPopMatrix();
		
		glPushMatrix();
		glPushName(3);
		glTranslatef(2.0f, 2.5f, 15.0f);						
			glBegin(GL_QUADS);									
				glColor4f(0.0f, 1.0f, 0.0f, 0.4f);
				glVertex3f(-1.0f, 1.0f, 0.0f);					
				glColor4f(1.0f, 0.0f, 0.0f, 0.4f);
				glVertex3f( 1.0f, 1.0f, 0.0f);					
				glColor4f(0.0f, 0.0f, 1.0f, 0.4f);
				glVertex3f( 1.0f,-1.0f, 0.0f);					
				glColor4f(1.0f, 1.0f, 0.0f, 0.4f);
				glVertex3f(-1.0f,-1.0f, 0.0f);					
			glEnd();		
		glPopName();
		glPopMatrix();
		
		glPopMatrix();
		
		glEnable(GL_LIGHTING);
		glEnable(GL_LIGHT0);	

}


[edit: source tags are your friend -SiCrane]

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!