Sign in to follow this  
thyrgle

Help understanding how to get glutWarpPointer to work right.

Recommended Posts

So originally I had in a function that handled rotation:

[code]void rotateCamera(int x, int y) {
mouseDeltaX = mouseDeltaX - x; //Get the change.
mouseDeltaY = mouseDeltaY - y;
rotx = (float)mouseDeltaX; //Assign it to a global variable value that would actually rotate everything.
roty = (float)mouseDeltaY;
mouseDeltaX = x; //Change mouseDeltaX and mouseDeltaY to the current x
mouseDeltaY = y;
}[/code]

It was passed to the glutPassiveMotionFunc(rotateCamera). My game is being constantly updated and near the top of the display function I would have:

[code] glRotatef(rotx*0.01, 0.0f, 1.0f, 0.0f);
glRotatef(roty*0.01, 1.0f, 0.0f, 0.0f);[/code]

And to make sure everything wasn't being constant rotated I would use a function I passed to glutIdleFunc:

[code]void isIdle() {
rotx = 0;
roty = 0;
}[/code]

This would cause the thing to stop when I was done. I got it to work, but the only problem is I need to keep the mouse in the center of the window or else it would hit the corners of the screen and prevent further rotation.

I tried making a function:

[code]void handleCamera(int x) {;
glutWarpPointer(glutGet(GLUT_WINDOW_X)+glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_Y)+glutGet(GLUT_WINDOW_HEIGHT));
glutTimerFunc(0.5, handleCamera, 0);
}[/code]

Which is passed to a glutTimerFunc initially. First off: no movement of the scene occurs except for the very begging when the mouse is warped. Second: The mouse for some reason isn't in the center of the window.

Will you please help me with these problems?

Thanks in advanced.

Note: Oops, wrong place, this should probably in "For Beginners" sorry. Can someone switch it? I can't seem to find out how.

Share this post


Link to post
Share on other sites
This should work:

[code]

glutWarpPointer(glutGet(GLUT_WINDOW_X)+glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_Y)+glutGet(GLUT_WINDOW_HEIGHT)/2)

[/code]

Basically, glutWarpPointer sets mouse's cursor position in "display screen-space".

Share this post


Link to post
Share on other sites
[code]

void rotateCamera(int x, int y) {


mouseDeltaX = mouseDeltaX - x; //Get the change.
mouseDeltaY = mouseDeltaY - y;
rotx = (float)mouseDeltaX; //Assign it to a global variable value that would actually rotate everything.
roty = (float)mouseDeltaY;
mouseDeltaX = x; //Change mouseDeltaX and mouseDeltaY to the current x
mouseDeltaY = y;
}


[/code]

If you want to create FPS camera with glutWarpPointer you need to that values "rotx" and "roty" must be calculated by this way:

[code]

rotx = (float)(x - glutGet(GLUT_WINDOW_WIDTH)/2); 

roty = (float)(y - glutGet(GLUT_WINDOW_HEIGHT)/2);

// and then warp cursor pointer into center of your glut window as it shows above

[/code]




Best wishes, FXACE.

Share this post


Link to post
Share on other sites
Hi,

I have tried your suggestions, but your glutWarpPointer parameters look identical to mine (with different spacing) and it still isn't working. (Oops, nvm. I forgot to divide by 2, but it still isn't in the center for some odd reason using your code).

Also, I have tried changing my rotateCamera function to:

[code]void rotateCamera(int x, int y) {
rotx = (float)(x - glutGet(GLUT_WINDOW_WIDTH)/2);
roty = (float)(y - glutGet(GLUT_WINDOW_HEIGHT)/2);
glutWarpPointer(glutGet(GLUT_WINDOW_X)+glutGet(GLUT_WINDOW_WIDTH)/2, glutGet(GLUT_WINDOW_Y)+glutGet(GLUT_WINDOW_HEIGHT)/2);
}[/code]

but, it makes the thing look jerky, and it doesn't rotate correctly.

Share this post


Link to post
Share on other sites
[url="http://www-etud.iro.umontreal.ca/~clavetsi/api/glut/glutWarpPointer.html"]This documentation[/url] states:
[quote]
[b]NAME[/b]
glutWarpPointer warps the pointer's location.

[b]SYNTAX[/b]
void glutWarpPointer(int x, int y);

[b]ARGUMENTS[/b]
x X offset relative to the current window's origin
(upper left).

y Y offset relative to the current window's origin
(upper left).

[b]DESCRIPTION[/b]
glutWarpPointer warps the window system's pointer to a new
location relative to the origin of the current window.
The new location will be offset x pixels on the X axis and
y pixels on the Y axis. These parameters may be negative.
The warp is done immediately.

If the pointer would be warped outside the screen's frame
buffer region, the location will be clamped to the nearest
screen edge. The window system is allowed to further con­
strain the pointer's location in window system dependent
ways.

The following is good advice that applies to glutWarp­
Pointer: ``There is seldom any reason for calling this
function. The pointer should normally be left to the
user.'' (from Xlib's XWarpPointer man page.)
[/quote]
Have you tried:
[code]
glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH) / 2, glutGet(GLUT_WINDOW_HEIGHT) / 2);
[/code]

Share this post


Link to post
Share on other sites
Sorry, I forgot that glutWarpPointer sets cursor in glut window's area. (for example, on windows, it calls SetCursorPos(glut_window_pos_x+glut_warp_pointer_x, glut_window_pos_y+glut_warp_pointer_y))

So, rip-off's code is solution of your problem.

P.S. I forgot some details of functions of GLUT.

Best wishes, FXACE.

Share this post


Link to post
Share on other sites
Thanks for the help it is in the center now, but the thing doesn't move correct still: It is really choppy unlike before when I didn't warp the pointer. I have changed my rotateCamera function to this, it doesn't update when I add the glutWarpPointer:

[code]void rotateCamera(int x, int y) {

rotx = (float)(x - glutGet(GLUT_WINDOW_WIDTH)/2);
roty = (float)(y - glutGet(GLUT_WINDOW_HEIGHT)/2);
glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH) / 2, glutGet(GLUT_WINDOW_HEIGHT) / 2);
}[/code]

Any suggestions?

Share this post


Link to post
Share on other sites
[quote name='thyrgle' timestamp='1318641284' post='4872724']
Thanks for the help it is in the center now, but the thing doesn't move correct still: It is really choppy unlike before when I didn't warp the pointer. I have changed my rotateCamera function to this, it doesn't update when I add the glutWarpPointer:

[code]void rotateCamera(int x, int y) {
   
    rotx = (float)(x - glutGet(GLUT_WINDOW_WIDTH)/2);
    roty = (float)(y - glutGet(GLUT_WINDOW_HEIGHT)/2);
    glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH) / 2, glutGet(GLUT_WINDOW_HEIGHT) / 2);
}[/code]

Any suggestions?
[/quote]

If "rotx" and "roty" are your relative rotations (of objects, view,...) use this:

[code]

float rotx=0.0f, roty=0.0f;// initialization

void rotateCamera(int x, int y) {
   
     rotx += (float)(x - glutGet(GLUT_WINDOW_WIDTH)/2); // do an additive operation because with "=" your object view, etc... would always set to default
     roty += (float)(y - glutGet(GLUT_WINDOW_HEIGHT)/2);
    glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH) / 2, glutGet(GLUT_WINDOW_HEIGHT) / 2);
}[/code]

Did you mean that?

Best wishes, FXACE.

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