# The Mouse

I just started using the mouse in OpenGL, and there’s one thing that stumps me: How can one tell if the cursor is going off the screen (so I can stop it)? I’m using gluPerspective. Should I not be? I just never was able to decipher glFrustrum.

gluPerspective() essentially calls glFrustum() for you anyways, so it doesn’t matter which one you want to use. With glFrustum, you can have some non-symmetric frustums, but usually that is not needed. You don’t even need OpenGL for what you are saying. Just keep the mouse inside the boundries using procedures that the OS gives you. Say that the window starting point is (x,y) and the width is w and height is h. Then starting from the upper left and going counter-clockwise, the boundries are: (x,y), (x, y+h), (x+w, y+h), (x+h, y).

• Halcyon

That’s the problem; I don’t KNOW w and h. The cursor is a texture-mapped quad.

w and h are the screen dimensions (ie 640x480 or 800x600).
w = width
h = height

If your viewport takes up the whole window, then you have to know it. But if you are using teh quad like you set, then use glFrustum(…) to define your perspective projection. That way, you will know the boundaries of your view frustum. If you know the math, then you can apply it to find the width and the height. I think it is the farplane distance * tan(fovy) to get the width. Since the frustum is symmetric, you should have width = height.

This is where all the arguments are in the gluPerspective:

gluPerspective(fovy, aspect ratio, near plane distance, far plane distance)

again w = h = (far plane distance) * tan(fovy);

I’m not 100% sure about that formula though.

• Halcyon

• Halcyon

Well it does depend on the z-position of the quad… I’m using a quad instead of the just drawing it directly so I can have part of the cursor be transparent.

I figured it out. That formula was almost correct; you forgot to include the width of the far plane. I got it now.