Thread: Question about WM_MOUSEMOVE

  1. #1
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200

    Question about WM_MOUSEMOVE

    I was trying to use wm_mousemove in an OpenGL project to create a custom mouse cursor. If i use:
    Code:
    glOrtho(0.0f, 800, 600, 0.0f, -10.0f, 10.0f);
    because glOrtho doesn't use depth so the mouse coordinate will be easy to determine where it is on the screen. But if i use this:
    Code:
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    It didn't work so well. Because of the depth, so when i put the coordinate of the mouse in glTranslatef(), it won't display on the screen.

    This is the mouse move code in glOrtho();
    Code:
    glLoadIdentity();
    mouseX = x_mouse;
    mouseY = y_mouse;
    glTranslatef(mouseX, mouseY, 0);
    glBegin(GL_QUADS);
         glVertex2f(0, 0);
         glVertex2f(20, 0);
         glVertex2f(20, 20);
         glVertex2f(0, 20);
    glEnd();
    ..........................
    //determine the coordinate of the mouse
    case WM_MOUSEMOVE:
    {
         x_mouse = LOWORD(lParam);	
         y_mouse = HIWORD(lParam);	
         return 0;	
    }
    So can anyone show me how to do it in gluPerspective() please. Thanks
    Hello, testing testing. Everthing is running perfectly...for now

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Don't run the mouse cursor vertexes through the perspective transform.

  3. #3
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Quote Originally Posted by Bubba
    Don't run the mouse cursor vertexes through the perspective transform.
    What do u mean?
    Hello, testing testing. Everthing is running perfectly...for now

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Code:
    glOrtho(window dimensions);
    DrawMouse();
    gluPerspective(field of vision);
    DrawWord();
    you use both perspectives each pass. There are some optimization techniques that can cut down the system hit you get by calling each every frame. What I do is set each perspective and then save the projection matrix of each with glGetDoublev(), and instead of calling the main perspective functions each frame, i call glLoadMatrix() with each of the saved matrices. If you want me to post my actual functions I can, but you should be able to set it up.

  5. #5
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Quote Originally Posted by Eber Kain
    Code:
    glOrtho(window dimensions);
    DrawMouse();
    gluPerspective(field of vision);
    DrawWord();
    you use both perspectives each pass. There are some optimization techniques that can cut down the system hit you get by calling each every frame. What I do is set each perspective and then save the projection matrix of each with glGetDoublev(), and instead of calling the main perspective functions each frame, i call glLoadMatrix() with each of the saved matrices. If you want me to post my actual functions I can, but you should be able to set it up.
    Ok, let me write down what i understood what u just said. You're saying that i would setup 2 perspectives, one is glOrtho(), and one is gluPerspective(). So the mouse will be appear in glOrtho(), instead of gluPerspective(). But if I have an object in the the gluPerspective() (like a button), how would i determine the coordinate of the mouse cursor and the object at a same coordinate on the screen?

    Yes, please show me your code and how u did it.
    Hello, testing testing. Everthing is running perfectly...for now

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    All GUI's are in 2D and in screen space. They are not or should not be in camera or view space. Don't run the vertices that make up the GUI objects through perspective transform since they are not 3D objects. They are simply quads that are textured to look like a cool GUI.

    When the mouse clicks on the screen the WM_MOUSEMOVE or WM_LBUTTONDOWN, etc, will still give you screen space coordinates since the mouse cursor is a global resource shared by all in Windows. But I would recommend firing up DirectInput and linking with it to make it easier to deal with input. Use OpenGL for graphics and use DirectInput for control. You only need to pass the handle of the window needing to use DirectInput. It does not require any IDirect3DDevice9 interface pointer and it should not conflict with OpenGL, and it will be faster than responding to Windows messages.

    You can create your own floating point rectangle structure or class similar to the one in Win32 called RECT.

    Code:
    class fRect
    {
      float left,top,right,bottom;
      public:
       fRect(void):left(0.0f),top(0.0f),right(0.0f),bottom(0.0f) {}
       fRect(const fRect &rect)
       {
         left=rect.left;
         top=rect.top;
         right=rect.right;
         bottom=rect.bottom;
       }
       fRect(float l,float t,float r,float b):left(l),top(t),right(r),bottom(b) {}
    
       float Width(void) {return right-left;};
       float Height(void) {return bottom-top;};
    
       void Normalize(void)
       {
         float temp=0.0f;
         //Check width
         if (left>right)
         {
           temp=right;
           right=left;
           left=right;
         }
         if (top>bottom)
         {
            temp=top;
            top=bottom;
            bottom=top;
          }
        }
    
        bool PointInRect(float x,float y)
        {
           if ((x>left && x<right) && (y>top && y<bottom))
           {
              return true;
           } else return false;
        }
    };
    Then in your Window class you might have this:

    Code:
    class CWindow
    {
      fRect PlacementInfo;
      ...
      ...
    };
    
    class CButton::public CWindow
    {
    };
    Button inherits from Window which means now the button has an fRect describing it's placement information.

    So button hit-testing is now as simple as calling:

    Code:
    Object->PlacementInfo.PointInRect(float mousex,float mousey)
    Where Object is either a CWindow or a class derived from CWindow.


    If it returns true, the user clicked inside this button, else the user clicked outside of this button.
    Last edited by VirtualAce; 11-14-2005 at 03:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM