glut picking clashes with drawing

Moderator
Posts: 373
Joined: 2006.08
Post: #1
I'm using GLUT for my windowing code, and I've created a picking function using gluPickMatrix that works pretty well, most of the time. My problem is that if the user waves their mouse over the screen before it displays, the mouseMove function gets called, which then calls my picking function. This works fine, but then the screen gets drawn once or more times, and then when mouseMove (and therefore the picking function) gets called after that, the program dies. I've tracked the problem down to something within the picking function, but I'm thinking that there's probably something inherently wrong with the code I've written.
Here is the picking function:
Code:
int clientDLL::pickClosest(anim* theAnims, int numOfAnims)
{
assert(windowHeight != 0);
assert(windowWidth != 0);

//numOfAnims starts at 1
glSelectBuffer(512,selectBuffer);
glRenderMode(GL_SELECT);

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

glGetIntegerv(GL_VIEWPORT,viewport);
gluPickMatrix(mouseX,viewport[3]-mouseY,5,5,viewport);
gluPerspective(45.0f,(GLfloat)windowWidth/(GLfloat)windowHeight,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);    
    
glInitNames();                                // Initializes The Name Stack
glLoadIdentity();
glTranslatef(0.0f,0.0f,-6.0f);
   for(int i=0; i<numOfAnims; i++)
   {
   glPushName(i);
   theAnims[i].drawBounding();
   //theAnims[i]->drawBounding();
      if(i != numOfAnims-1)
      {
      glPopName();
      }
   }

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

//glMatrixMode(GL_MODELVIEW);                        // Select The Modelview Matrix
int hits=glRenderMode(GL_RENDER);                        // Switch To Render Mode, Find Out How Many
   if (hits != 0)                                // If There Were More Than 0 Hits
   {
   int    choose = selectBuffer[3];                    // Make Our Selection The First Object
   int    depth = selectBuffer[1];                    // Store How Far Away It Is
        
      for (int loop = 1; loop < hits; loop++)                // Loop Through All The Detected Hits
      {
      // If This Object Is Closer To Us Than The One We Have Selected
         if (selectBuffer[loop*4+1] < GLuint(depth))
         {
         choose = selectBuffer[loop*4+3];            // Select The Closer Object
         depth = selectBuffer[loop*4+1];            // Store How Far Away It Is
         }
      }
   cout << "clicked: " << choose << "\n";
   return choose;
   }
   else
   {
   cout << "clicked nothing \n";
   return -1;
   }
}
where anims is an array and drawBounding() just draws a rectangular prism.

Here is my drawing code for glut:
Code:
void display(void)
{
     cout << "\nentering display\n\n";

#if defined(__APPLE_CC__)
elapsedTime = elapsedTime + (AbsoluteToDuration(UpTime()) - AbsoluteToDuration(theTime));
theTime = UpTime();
#else
elapsedTime += (GetTickCount() - theTime);
theTime = GetTickCount();
#endif

numOfFrames++;

   if(elapsedTime >= 1000)
   {
   cout << "FPS: " << numOfFrames << "\n";
   numOfFrames = 0;
   elapsedTime = 0;
   }
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
    
game->draw();

glutSwapBuffers();
}

I can post more code if needed.
Any ideas? Smile
Thank you,
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
"dies" is not helpful. What exactly happens?

Don't call glFlush, it's just a performance penalty for no benefit.

You might like glutGet(GLUT_ELAPSED_TIME) for cross-platform timing, too.
Quote this message in a reply
Moderator
Posts: 373
Joined: 2006.08
Post: #3
hmmm....looks like it doesn't happen on the Mac, just Windows Annoyed
By 'dies' I mean it just crashes. I've tried to debug it, but the debugger is being incredibly unhelpful and giving me the error message: 'SIGSEGV, Segmentation fault. 0x693f730c in atioglxx!atiPPHSN () from C:\Windows\system32\atioglxx.dll
I'm no Windows wizard, but I'm guessing that its dying in opengl somewhere? Lost as to to where I should go from here, though Sad

Anyway, sorry about the post since it's not really Mac related. I think I'll leave it alone for now and try to fix it eventually.

-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  How do I do Color Picking? dockode 2 5,163 Sep 24, 2011 08:02 PM
Last Post: dockode
  Issues with picking FFaqui 0 2,292 Nov 21, 2009 04:37 PM
Last Post: FFaqui
  Opengl picking problem (zip file) papillon68 1 4,515 Mar 1, 2009 08:49 PM
Last Post: chronus
  Picking and selection... chalamov33 2 3,478 Mar 31, 2008 02:08 PM
Last Post: wyrmmage
  picking glOrtho vs gluPerspective rhiannon 0 3,731 Jun 6, 2005 02:20 PM
Last Post: rhiannon