| ARToolKit | Mailing List Archive |  
					  					
				 |  
 
			
| From: | Patrick AS Sinclair <pass99r@e ..............> | Received: | May 1, 2003 | 
| To | artoolkit@h .................. | ||
| Subject: | Picking with a mouse | ||
Hi, 
This might sound really crazy but I'd like to be able to point and click
using a mouse to select things with the artoolkit. 
I've been using some tutorials (Nehe Lesson 32) on the web on OpenGL mouse
picking and used some of this code but it isn't very accurate. Also, to
draw the objects I translate them into the scene (ie positive z
translation) but for the picking to work I translate them out of the
screen (so negative z translation). 
I think the reason that this is happening is that the gluProjection matrix
is loading the wrong values (as this code is from the tutorial): 
// Apply The Perspective Matrix 
gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.1f, 100.0f); 
Is this the problem? If so, what should these values be? Has anyone else
been able to use mouse clicking with the ARToolKit? 
Here's the code I've been using: 
Code: 
// clicking stuff - from nehe's lesson 32 
void selection(int mouse_x, int mouse_y) { 
   GLuint   buffer[512];       // Set Up A Selection Buffer 
   GLint   hits;         // The Number Of Objects That We Selected 
   // The Size Of The Viewport. [0] Is <x>, [1] Is <y>, [2] Is <length>, [3] Is <width> 
   GLint   viewport[4]; 
   // This Sets The Array <viewport> To The Size And Location Of The Screen Relative To The Window 
   glGetIntegerv(GL_VIEWPORT, viewport); 
   glSelectBuffer(512, buffer);   // Tell OpenGL To Use Our Array For Selection 
   // Puts OpenGL In Selection Mode. Nothing Will Be Drawn.  Object ID's and Extents Are Stored In The Buffer. 
   (void) glRenderMode(GL_SELECT); 
   glInitNames();   // Initializes The Name Stack 
   glPushName(0);   // Push 0 (At Least One Entry) Onto The Stack 
   glMatrixMode(GL_PROJECTION);    // Selects The Projection Matrix 
   glPushMatrix();         // Push The Projection Matrix 
   glLoadIdentity();      // Resets The Matrix 
   // This Creates A Matrix That Will Zoom Up To A Small Portion Of The Screen, Where The Mouse Is. 
   gluPickMatrix((GLdouble) mouse_x, (GLdouble) (viewport[3]-mouse_y), 1.0f, 1.0f, viewport); 
   // Apply The Perspective Matrix 
   gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.1f, 100.0f); 
   glMatrixMode(GL_MODELVIEW);   // Select The Modelview Matrix 
    
   glLoadIdentity();         // Reset The Modelview Matrix 
   glPushMatrix(); 
   glTranslatef(0.0f,0.0f,-100.0f);    // Move Into The Screen - in my drawing code this is translated (0,0,100) 
   DrawTargets(); 
   glPopMatrix(); 
    
   glMatrixMode(GL_PROJECTION);   // Select The Projection Matrix 
   glPopMatrix();         // Pop The Projection Matrix 
   glMatrixMode(GL_MODELVIEW);   // Select The Modelview Matrix 
   hits=glRenderMode(GL_RENDER);   // Switch To Render Mode, Find Out How Many 
               // Objects Were Drawn Where The Mouse Was 
   if (hits > 0) {            // If There Were More Than 0 Hits 
      int   choose = buffer[3];   // Make Our Selection The First Object 
      int depth = buffer[1];      // Store How Far Away It Is 
       
      printf("hits: %d %d\n", hits, choose); 
      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 (buffer[loop*4+1] < GLuint(depth)) 
         { 
            choose = buffer[loop*4+3];   // Select The Closer Object 
            depth = buffer[loop*4+1];   // Store How Far Away It Is 
         }        
      } 
      selected = choose; 
   } else { 
      selected = -1; 
      printf("no hits!\n"); 
   }    
} 
Thanks! 
Patrick Sinclair
				 | 
			|||
| From: | anderaus@s ........... | Received: | May 2, 2003 | 
| To | Patrick AS Sinclair <pass99r@e ..............> | ||
| Subject: | Re: Picking with a mouse | ||
Hi Patrick
As you point out yourself, you are using the wrong projection matrix. 
Try getting your current projection matrix with
  glGetFloatv(GL_PROJECTION_MATRIX, some_name_here);
and multiply it with your defined picking region like this:
  gluPickMatrix((GLdouble) mouse_x, (GLdouble) (viewport[3]-mouse_y), 
1.0f, 1.0f, viewport); 
 
  glMultMatrixf(some_name_here);
Hope this helps.
Good luck!
Anders Austad
NTNU Norway
> 
> Hi, 
> 
> This might sound really crazy but I'd like to be able to point and
> click
> using a mouse to select things with the artoolkit. 
> 
> I've been using some tutorials (Nehe Lesson 32) on the web on OpenGL
> mouse
> picking and used some of this code but it isn't very accurate. Also,
> to
> draw the objects I translate them into the scene (ie positive z
> translation) but for the picking to work I translate them out of the
> screen (so negative z translation). 
> 
> I think the reason that this is happening is that the gluProjection
> matrix
> is loading the wrong values (as this code is from the tutorial): 
> 
> // Apply The Perspective Matrix 
> gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat)
> (viewport[3]-viewport[1]), 0.1f, 100.0f); 
> 
> Is this the problem? If so, what should these values be? Has anyone
> else
> been able to use mouse clicking with the ARToolKit? 
> 
> 
> Here's the code I've been using: 
> 
> Code: 
> 
> // clicking stuff - from nehe's lesson 32 
> void selection(int mouse_x, int mouse_y) { 
>    GLuint   buffer[512];       // Set Up A Selection Buffer 
>    GLint   hits;         // The Number Of Objects That We Selected 
> 
>    // The Size Of The Viewport. [0] Is <x>, [1] Is <y>, [2] Is
> <length>, [3] Is <width> 
>    GLint   viewport[4]; 
> 
>    // This Sets The Array <viewport> To The Size And Location Of The
> Screen Relative To The Window 
>    glGetIntegerv(GL_VIEWPORT, viewport); 
>    glSelectBuffer(512, buffer);   // Tell OpenGL To Use Our Array For
> Selection 
> 
>    // Puts OpenGL In Selection Mode. Nothing Will Be Drawn.  Object
> ID's and Extents Are Stored In The Buffer. 
>    (void) glRenderMode(GL_SELECT); 
> 
>    glInitNames();   // Initializes The Name Stack 
>    glPushName(0);   // Push 0 (At Least One Entry) Onto The Stack 
> 
>    glMatrixMode(GL_PROJECTION);    // Selects The Projection Matrix 
>    glPushMatrix();         // Push The Projection Matrix 
>    glLoadIdentity();      // Resets The Matrix 
> 
>    // This Creates A Matrix That Will Zoom Up To A Small Portion Of The
> Screen, Where The Mouse Is. 
>    gluPickMatrix((GLdouble) mouse_x, (GLdouble) (viewport[3]-mouse_y),
> 1.0f, 1.0f, viewport); 
> 
>    // Apply The Perspective Matrix 
>    gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat)
> (viewport[3]-viewport[1]), 0.1f, 100.0f); 
> 
>    glMatrixMode(GL_MODELVIEW);   // Select The Modelview Matrix 
>     
>    glLoadIdentity();         // Reset The Modelview Matrix 
>    glPushMatrix(); 
>    glTranslatef(0.0f,0.0f,-100.0f);    // Move Into The Screen - in my
> drawing code this is translated (0,0,100) 
>    DrawTargets(); 
>    glPopMatrix(); 
>     
>    glMatrixMode(GL_PROJECTION);   // Select The Projection Matrix 
>    glPopMatrix();         // Pop The Projection Matrix 
>    glMatrixMode(GL_MODELVIEW);   // Select The Modelview Matrix 
>    hits=glRenderMode(GL_RENDER);   // Switch To Render Mode, Find Out
> How Many 
>                // Objects Were Drawn Where The Mouse Was 
>    if (hits > 0) {            // If There Were More Than 0 Hits 
>       int   choose = buffer[3];   // Make Our Selection The First
> Object 
>       int depth = buffer[1];      // Store How Far Away It Is 
>        
>       printf("hits: %d %d\n", hits, choose); 
> 
>       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 (buffer[loop*4+1] < GLuint(depth)) 
>          { 
>             choose = buffer[loop*4+3];   // Select The Closer Object 
>             depth = buffer[loop*4+1];   // Store How Far Away It Is 
>          }        
>       } 
>       selected = choose; 
>    } else { 
>       selected = -1; 
>       printf("no hits!\n"); 
>    }    
> } 
> 
> 
> Thanks! 
> 
> Patrick Sinclair
> 
				 | 
			|||