confusing problem !!!

Hi everybody …

I’m trying to dragg a rectangle in a condition that tha mouse pointer is inside that rectangle …

I drew the rectangle using
glRectf(px1,py1,px2,py2)…

the coordinates of the rectangle are (0,-65,20,-70);

The problem that when I click INSIDE the rectangle and get back the value of x & y using the glutMouse function , I found that the values of x & y CAN’T be inside the rectangle ( something like 209, 361 ) eventhough I was pressing inside that rectangle !!!

Any body can inlighten Me … !

Thanks in Advance

Here is an explanation draw :

Hi !

Mouse coordinates have nothing to do with the coordinates of the rectangle, on most platforms the mouse coordinates has their origin in the upper left corner and maps 1:1 to the device resolution.

The rectangle coordinates depends on your modeview matrix setup, if you do want to map them to mouse coordinates have a peek at the faq on this website (transformations section), there is an example on how to setup the modelview matrix to match the device resolution (with a flipped Y axis).

Mikael

Thanks Mikael for your quick responce …

I’m really not familiar on what you have said a bout mapping the modelview to match the device resolution eventhough I will look in the transformations section …

I wonder if there is other method to make sure that the mouse click is inside the rectangle … ??

Cheers,

Hello,

I think ur problem is that u r not converting the mouse coordinates from window co-ordinate system to rectangle coordinate system.

Based on the code u posted for the previous problem, I think ur doing lots of projection and modelview transformations, and then render the rectangle, so u have to take care of those for mouse co-oridnates also.

  • Chetan

Originally posted by virtualchetan:
[b]I think ur problem is that u r not converting the mouse coordinates from window co-ordinate system to rectangle coordinate system.

  • Chetan[/b]

Hello Chetan …

How can I do that … I.e, convert the mouse coordinate from window to rectangle coordinate system ???

I tried to apply the projection and modelview transformations I did to glut mouse function and I get no result I mean there is still big different between the two system coordinate …

Thank you for your explanation & support Chetan …

The mouse function returns the value in pixels, not openGL world units.

Lets say you have a openGL world of -10,-10 to 10,10, you viewport is 200 x 200 pixels.

This converts windows pixels to openGL world values.

Where x/y is mouse values and Win_x/y are hight and width of window in pixels.

// my mouse routine
void mouse(int button, int state, int x, int y)
{

Mouse_x = -10 + 20 * (x/Win_x);
Mouse_y = 10 - 20 * (y/Win_y);

// Mouse_x/y tells us where in openGL units the mouse was clicked.

}

// My reshape routine
void reshape_1 (int w, int h)
{
Win_x = w; // Get new value of windows witdh when window size change
Win_y = h; // Get new value of hight
glViewport (0, 0, (GLsizei) w, (GLsizei) h); // Tell openGL the new window size
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
}

[This message has been edited by nexusone (edited 12-02-2003).]

Hello nexusone,

I was trying your code all that time but I fave up … it didn’t work … now it gives me a fixed value for x & y !!!

Here is my mouse function :
void myGlutMouse(int button, int button_state, int x, int y )
{
switch(button)
{
case GLUT_RIGHT_BUTTON:
{
if ((button_state == GLUT_DOWN))
{
x = -80 + 160 *(x / Winx);
y = 80 + 160 *(x / Winy);
// if ((x>= (pX1+290)) && (x<=(pX1+pX2+361)) && (y>= 535) && (y<= 552) )
// {
dragging = true ;
last_x = x;
last_y = y;
// }
}
else
dragging = false ;
}
}
}

Here is my reshape function :
void myGlutReshape( int x, int y )
{
int tx, ty, tw, th;
Winx= tw;
Winy= th;

GLUI_Master.get_viewport_area( &tx, &ty, &tw, &th );
glViewport( tx, ty, tw, th );

// xy_aspect = (float)tw / (float)th;

glutPostRedisplay();
}

I beleived my world is between ( -80,-80, 80,80) and my viewport is 600 × 600 .

Thank you in advance

[This message has been edited by glcrazy (edited 12-04-2003).]

The problem is you did not copy my code correctly, there is an error in your reshape routine.

Also if you like I can e-mail you a example drawing program I wrote, just drop me a e-mail.

See corrections below

Here is my reshape function :
void myGlutReshape( int x, int y )
{
int tx, ty, tw, th; //
Winx= tw; <----error you set Winx to zero every call here…
Winy= th;
// Correct code
Winx = x;
Winy = y;

// tx, ty, tw, th; all have zero until this is called
GLUI_Master.get_viewport_area( &tx, &ty, &tw, &th );
glViewport( tx, ty, tw, th );

// xy_aspect = (float)tw / (float)th;

glutPostRedisplay();
}

I beleived my world is between ( -80,-80, 80,80) and my viewport is 600 × 600 .

Thank you in advance

[This message has been edited by nexusone (edited 12-04-2003).]

Hi nexusone

the problem still there even I changed what you tell me to change …I beleive it give me a zero somewhere in the second part of the equation here … x = -80 + 160 * ( x/ Winx) … because the value of x remains -80 always the same happened to y…

I checked the value of intial x … it’s not zero , Winx also not zero … but the value of x after the equation is always -80 !!!

I don’t know where I’m wrong exactly …

Thanks for any help …

Hello,
I suggest that you look at the pratical
gluUnProject function which given

  • the model view matrix
  • the projection matrix
  • the viewport

gives you the coordinates of the mouse pointer in the model coordinates system

Hello Ido77

Eventhough I didn’t ever used gluUnProject function , I’ll try to look for it and know how it works …

Thank you Ido77

Hello again Ido77

I looked a round for gluUnProject function , it seems interesting but difficult to me especially I’m running out of time now … !!

Thank you for your help

Originally posted by nexusone:

Mouse_x = -10 + 20 * (x/Win_x);
Mouse_y = 10 - 20 * (y/Win_y);

ahem, I would say that if both x and Win_x are C int, you will have bad rounding errors…
try to force conversion to float or double :

Mouse_x = -10 + 20 * ((1.0x)/Win_x);
Mouse_y = 10 - 20 * ((1.0
y)/Win_y);

This will probably make it work.

It has to be an error still in your code, because the routine works fine in my program.

Repost your full code as you have it now, and I will look at it or e-mail it to me.

Hi ZbuffeR

I did as you mentioned ( … *0.1). It changed the values of x&y but not in the right way … !! I mean the code still not work Correctly …

Thank you

Originally posted by nexusone:
[b]It has to be an error still in your code, because the routine works fine in my program.

Repost your full code as you have it now, and I will look at it or e-mail it to me.

[/b]

I already sent the code for you nexusone…

Thank you very much for your help.

Originally posted by glcrazy:
I did as you mentioned ( … *0.1). It changed the values of x&y but not in the right way … !!

Well, I told you to use 1.0*x to force the C compiler to do the division in double precision. You could use ((double)x) too… NOT *0.1 .
Of course, 0.1 will change your values in a wrong way… Maybe a typo ?

Here is a code example for using gluUnproject
Given the gMouseAreaIni var and the gMouseAreaFin var (respectively first and second mouse points)

GLdouble lFirstPoint[3];
GLdouble lSecondPoint[3];
GLdouble lModelViewMatrix[16];
GLdouble lProjectionMatrix[16];
GLdouble lDepthRange[2];
GLint lViewport[4];
GLboolean lValidFirstPoint;
GLboolean lValidSecondPoint;

glGetDouble(GL_MODELVIEW_MATRIX,lModelViewMatrix);
glGetDoublev
(GL_PROJECTION_MATRIX, lProjectionMatrix);
glGetDoublev
(GL_DEPTH_RANGE, lDepthRange);
glGetIntegerv(GL_VIEWPORT, lViewport);

lValidFirstPoint = gluUnProject( (GLdouble)gMouseAreaIni[0], (GLdouble)(lViewport[3] - gMouseAreaIni[1]), lDepthRange[0], lModelViewMatrix, lProjectionMatrix, lViewport,&(lFirstPoint[0]), &(lFirstPoint[1]), &(lFirstPoint[2]));

lValidSecondPoint = gluUnProject((GLdouble)gMouseAreaFin[0], (GLdouble)(lViewport[3] - gMouseAreaFin[1]), lDepthRange[0], lModelViewMatrix, lProjectionMatrix, lViewport,&lSecondPoint[0], &lSecondPoint[1], &lSecondPoint[2]);

Hi Zbuffer

OK … I did what you said …

I have results now … but also not a correct ones …

This will drive me crazy

My equations now becomes like that:

Mousex = -80 + (float)(160*(x/Winx));
Mousey = 80 - (float)(160*(y/Winy));

Not work as desired or I’m missing something in my code !!!

Thank you