Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: confusing problem !!!

  1. #1

    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

  2. #2

    Re: confusing problem !!!

    Here is an explanation draw :


  3. #3
    Senior Member OpenGL Pro
    Join Date
    May 2001
    Location
    Kristianstad,Skåne,Sweden
    Posts
    1,651

    Re: confusing problem !!!

    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

  4. #4

    Re: confusing problem !!!

    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,

  5. #5
    Intern Contributor
    Join Date
    Jun 2003
    Location
    Blacksburg, VA 24060, USA
    Posts
    61

    Re: confusing problem !!!

    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

  6. #6

    Re: confusing problem !!!

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

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: confusing problem !!!

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

  8. #8

    Re: confusing problem !!!

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

  9. #9
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: confusing problem !!!

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

  10. #10

    Re: confusing problem !!!

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •