View Full Version : confusing problem !!!

glcrazy

12-01-2003, 12:52 AM

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

glcrazy

12-01-2003, 01:09 AM

Here is an explanation draw :

http://nomatterwhat.jeeran.com/rectangle-problem.gif

mikael_aronsson

12-01-2003, 01:10 AM

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

glcrazy

12-01-2003, 01:25 AM

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,

virtualchetan

12-01-2003, 04:30 AM

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

glcrazy

12-02-2003, 05:32 AM

Originally posted by virtualchetan:

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

- Chetan

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 ..

nexusone

12-02-2003, 05:48 AM

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).]

glcrazy

12-04-2003, 08:06 AM

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).]

nexusone

12-04-2003, 08:41 AM

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).]

glcrazy

12-04-2003, 09:37 PM

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 ..

ido77

12-04-2003, 11:11 PM

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

glcrazy

12-05-2003, 12:05 AM

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

glcrazy

12-05-2003, 03:15 AM

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 http://www.opengl.org/discussion_boards/ubb/frown.gif .. !!

Thank you for your help

ZbuffeR

12-05-2003, 03:38 AM

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.0*x)/Win_x);

Mouse_y = 10 - 20 * ((1.0*y)/Win_y);

This will probably make it work.

nexusone

12-05-2003, 04:48 AM

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.

glcrazy

12-07-2003, 12:00 AM

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

glcrazy

12-07-2003, 12:01 AM

Originally posted by nexusone:

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.

I already sent the code for you nexusone..

Thank you very much for your help.

ZbuffeR

12-07-2003, 09:53 AM

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 http://www.opengl.org/discussion_boards/ubb/tongue.gif.

Of course, 0.1 will change your values in a wrong way... Maybe a typo ?

ido77

12-07-2003, 10:56 PM

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]);

glcrazy

12-09-2003, 04:14 AM

Hi Zbuffer

OK http://www.opengl.org/discussion_boards/ubb/smile.gif .. 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

ZbuffeR

12-09-2003, 09:44 AM

You have to force conversion to float before you do the divide, else it will be integer divide (eg. 3/7=2).

ex :

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

Mousey = 80 - (160*(((float)y)/Winy));

or :

Mousex = -80 + (160.0*x)/Winx;

Mousey = 80 - (160.0*y)/Winy;

glcrazy

12-10-2003, 08:41 AM

Originally posted by ido77:

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 ido77

Seems also difficult to me http://www.opengl.org/discussion_boards/ubb/smile.gif ..

I found other easier solution to solve my problem .

Thanks alot ido77 , I apprciate your help.

glcrazy

12-10-2003, 08:43 AM

Originally posted by ZbuffeR:

You have to force conversion to float before you do the divide, else it will be integer divide (eg. 3/7=2).

or :

Mousex = -80 + (160.0*x)/Winx;

Mousey = 80 - (160.0*y)/Winy;

IT WORKS http://www.opengl.org/discussion_boards/ubb/smile.gif

Thanks millions http://www.opengl.org/discussion_boards/ubb/smile.gif ..

ZbuffeR

12-10-2003, 08:47 AM

Glad to see I helped you solve your problem. Try to find some doc/tutorial on the nasty C traps, especially with format conversion, pointers and arrays, etc. You may need it.

ido77

12-10-2003, 10:55 PM

I'am happy to see you have succeeded in solving your problem this way.

A firm grasp on C/C++ would help a ton here, I think. Everyone wants to make cool 3D programs, but no one wants to learn HOW cool 3D programs are made. Just throw me some code, that's all beginner programmers are asking these days.

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.