# Mouse coordinate, gluUnproject() ect ..

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

## Recommended Posts

Hello guys ! I've got a small pb : i'm building a tiny app . in wich there is only ONE quad z = 0 ; -3 < x < -4 ; -3.5 < y < 3.5 ; for his coordinates . my camera is set up as : gluLookAt(0,0,10,0,0,0,0,1,0); She is not moving at all . My goal is simply to detect where ( in x,y ) is my mouse ON the quad . I tried a couple of things with gluUnprject() and gluProject() ... i cant get the result i want :/ Some strange things : gluUnProject(xmouse, ymouse, 0,matrixview,proj,vp,&xWorldMouse, &yWorldMouse, &zWorldMouse); -> gives me value for (x,y) around ( -1 / 1 ) ; in my screen , valu should be ranged in (-5 / 5 ) gluProject(3,3,0,matrixview,proj,vp,&Mx, &My, &Mz); -> gives me a z of 0.99091 ... dunno where it comes from . i'm kinda lost here ...

##### Share on other sites
Hi,
try this way :
1)when the mouse over draw the whole scene with solid color like your quad make it red and make sure you disable lighting , texture .... etc.
2)use glReadPixel() and use the mouse coord to get the pixel that the mouse on it .
3)check the color if it's red this mean you are on the quad.
i hope you get the way "check the red book for more information"
bye,

##### Share on other sites
yeah i read about that method ; but that will only tell me tahti am over the qaud ; not the (x,y) pos ON the quad :)
thx anyway

##### Share on other sites
try this
gluUnProject(xmouse, ymouse, 1,...

##### Share on other sites
Z = 1 give wierd results too .
anyway pb solved, i found an ugly way to this but at least its works :

int xmouse = cInputManager::GetInstance().mouseX ;     int ymouse = cInputManager::GetInstance().mouseY ;	 GLdouble matrixview[16] ;	 GLdouble proj[16] ;	 GLint vp[4]; 	 glGetIntegerv(GL_VIEWPORT, vp); 	 glGetDoublev ( GL_MODELVIEW_MATRIX,  matrixview );     glGetDoublev ( GL_PROJECTION_MATRIX, proj   );	 GLdouble xWorldMouse ;	 GLdouble yWorldMouse ;	 GLdouble zWorldMouse ;     gluUnProject(xmouse,768 -  ymouse, 0.990991,matrixview,proj,vp,&xWorldMouse, &yWorldMouse, &zWorldMouse);	 if ( xWorldMouse >= -3 && xWorldMouse <= 4 && yWorldMouse >= -3.5 && yWorldMouse <= 3.5 )	 {		 int xquad = 0 ;		 int yquad = 0 ;		 if ( xWorldMouse > 0 )		 {			 xquad = 512 * ( xWorldMouse / 7 )  + ( 3 * 512 / 7 ) ;		 }		 else xquad = 512 * ( xWorldMouse/ 7 ) + ( 3 * 512 / 7 )  ;	     if ( yWorldMouse > 0 )		 {			 yquad = 512 * ( yWorldMouse / 7 )  + (  512 / 2  ) ;		 }		 else yquad = 512 * ( yWorldMouse/ 7 ) + ( 512 / 2  )  ;		 	 }

I found the "0.990991" for z, while messing with gluproject() .
To found the coord in my quad i just do some easy math conversion ( 7 is the size in GL unit of my quad )

• 16
• 9
• 13
• 41
• 15