Help understanding how to get glutWarpPointer to work right.

Started by
6 comments, last by FXACE 12 years, 6 months ago
So originally I had in a function that handled rotation:

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;
}


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

glRotatef(rotx*0.01, 0.0f, 1.0f, 0.0f);
glRotatef(roty*0.01, 1.0f, 0.0f, 0.0f);


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

void isIdle() {
rotx = 0;
roty = 0;
}


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:

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


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.
Advertisement
This should work:



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



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



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;
}




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



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






Best wishes, FXACE.

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:

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


but, it makes the thing look jerky, and it doesn't rotate correctly.
This documentation states:

NAME
glutWarpPointer warps the pointer's location.

SYNTAX
void glutWarpPointer(int x, int y);

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

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

DESCRIPTION
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:

glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH) / 2, glutGet(GLUT_WINDOW_HEIGHT) / 2);
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.

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:

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


Any suggestions?

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:

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


Any suggestions?


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



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


Did you mean that?

Best wishes, FXACE.

This topic is closed to new replies.

Advertisement