Sign in to follow this  
arbuckle911

OpenGL mouse input for opengl in win32

Recommended Posts

I've been using win32 GET_X_MOUSE_POS macros to get the mouse position then I've been setting it back to the center of the screen so it doesn't get stuck on the edges, but I was wondering if there was a better way, because the code for re-centering is really ugly and some times the mouse seems to get stuck. I'd rather not use direct input for this job, since the rest of the game is in opengl. thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by arbuckle911
I've been using win32 GET_X_MOUSE_POS macros to get the mouse position then I've been setting it back to the center of the screen so it doesn't get stuck on the edges, but I was wondering if there was a better way, because the code for re-centering is really ugly and some times the mouse seems to get stuck.
Just look up the proper functions. GetCursorPos and SetCursorPos or something like that.
Quote:
I'd rather not use direct input for this job, since the rest of the game is in opengl. thanks.
Why? Direct Input and OpenGL mix just fine.

-edit-

Here you go a link to SetCursorPos(), links to GetCursorPos and other relevant function at the bottom of this page too.

Share this post


Link to post
Share on other sites
the SetCursorPos() doesn't work in my program..

when I run it, the screen freezes displaying the
window borders and the image of what was behind before it ran..
I usually get these kind of error when (unintentionally)
using combinations of double buffer and glFlush()
(instead of swapping buffers).

I don't think there's any calculation error, cause everything
works fine without the line "SetCursorPos(any x, any y);"
which is the key to eliminate cornering mouse movement..

MY CLUES: since it takes it to same place as rendering error, I'm starting
to think that SetCursorPos() affects the.. ok.. I don't have any clues :]
I don't see what I'm doing wrong..

can someone pls help me sort this out..
here is the mouse movement part CODE:
-------------------------------------------------------------
void passiveMotion(int x, int y)
{
aux_newX = x;
aux_newY = y;
accum_X = aux_newX - aux_oldX;
accum_Y = aux_newY - aux_oldY;

//these are the actual mouse x and y
mouse_X += accum_X;
mouse_Y += accum_Y;

//just for temporary monitoring
std::cout << "\n<" << mouse_X << "|" << mouse_Y << ">";

aux_oldX = x;
aux_oldY = y;

//and here is my eye pain
SetCursorPos(wWidth / 2, wHeight / 2);
}
-------------------------------------------------------------

ps: the SetCursorPos() is used from windows.h include..
I'm not sure if there's any other, but anyway.. that's that..

tnx!

[Edited by - experiment on May 31, 2007 12:09:50 PM]

Share this post


Link to post
Share on other sites
First, you don't need to take new - old. Windows moves the mouse for you, so I'm not sure why you are doing that. Just use GetCursorPos().

The better way would be to use messages WM_MOUSEMOVE etc. Especially if your window is not made at the top left of your screen, because GetCursorPos() gives you global mouse coordinates rather than from the top left of your window.

Share this post


Link to post
Share on other sites
I just ended up using directx. I wanted to avoid a dependency on such a huge library just for one small part of it, but it's so common anyway I guess it's no big deal. Anyway, I'm really impressed with it, and I'd recommend directx to anybody working on a big project.

Share this post


Link to post
Share on other sites
Quote:
Original post by dpadam450
First, you don't need to take new - old. Windows moves the mouse for you, so I'm not sure why you are doing that. Just use GetCursorPos().

The better way would be to use messages WM_MOUSEMOVE etc. Especially if your window is not made at the top left of your screen, because GetCursorPos() gives you global mouse coordinates rather than from the top left of your window.


GetCursorPos() solved my prob.. tnx for that!
instead of complicating with new-old, I went like this:
-------------------------------------------------------
//EXAMPLE SHOWING HOW TO ELIMINATE MOUSE CORNERING
//WHILE TRACKING ITS MOVEMENT

include<windows.h>
int winWidth; // current window width
int winHeight; // current window height
int mouse_X; // actual mouse X coordinate position
int mouse_Y; // actual mouse Y coordinate position

POINT pointCursorPos; // declare a point

// mouse position tracking
GetCursorPos(&pointCursorPos); // get current point position
mouse_X += (pointCursorPos.x - winWidth/2); // update actual X
mouse_Y += (pointCursorPos.y - winHeight/2); // update actual Y
SetCursorPos(winWidth/2, winHeight/2); // set mouse back to center of window
-------------------------------------------------------

notes:
-actual X and Y means the X and Y you want your program to use
-in openGL its best/necessary (in my opinion) to have winWidth and winHeight
updated in your reshape function [ex: glutReshapeFunc(func.) as winWidth = x; and winHeight = y;]
As for "mouse position tracking" part, I (personally) keep it in the display func. (just before glutSwapBuffers();) (There are other possibilities, probably better ones too)
-I searched the net for this kind of explanation, but didn't find any.. I hope you have some use of it.

PS: If you still can't make this out, I'll post a working example.. (just ask for it)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this