Mapping window position to openGL position

Hello!

I’m writing a simple space invaders game as a learning exercise using OpenGL in 3d. I can’t work out how to covert the screen X,Y coordinates of the mouse into the usual OpenGL coordinates (the arbitrary ones where centre is 0,0,0 and the unit cube goes -0.5 to +0.5 in all directions). I want player control by mouse, but this is holding me back. I found one example online but it was hugely complex with multiple functions.

Thanks,

Steve

So … converting a 2d point (screen) in a 3d point (opengl space) ? Sounds odd isn’t it ? The best you can get is a ray, not a point. search google for “opengl ray picking”.

why not just use a 1:1 mapping of coords ?

check kevglass’ space invaders:
http://www.cokeandcode.com/info/tut2d-4.html

[quote]the screen X,Y coordinates of the mouse into the usual OpenGL coordinates (the arbitrary ones where centre is 0,0,0 and the unit cube goes -0.5 to +0.5 in all directions).
[/quote]
Those are only arbitrary in the sense that they’re the defaults. You can have complete control over those via the projection matrix (although more likely you’ll set them indirectly via glOrtho or glPerspective).

For an easy “it just works” method, you can go from screen coords to world coords with gluUnProject (and back again with gluProject). But obviously that will give you a ray rather than a single point.

Why don’t you just get the deltas for the mouse, rather than the absolute coordinates?

[quote]the screen X,Y coordinates of the mouse into the usual OpenGL coordinates (the arbitrary ones where centre is 0,0,0 and the unit cube goes -0.5 to +0.5 in all directions).
[/quote]
Those are only arbitrary in the sense that they’re the defaults. You can have complete control over those via the projection matrix (although more likely you’ll set them indirectly via glOrtho or glPerspective).

For an easy “it just works” method, you can go from screen coords to world coords with gluUnProject (and back again with gluProject). But obviously that will give you a ray rather than a single point.

That’s why you need a depth coordinate to get the position on the ray.

Cas :slight_smile:

[quote]Why don’t you just get the deltas for the mouse, rather than the absolute coordinates?
[/quote]
That’s a good plan…I’d have to scale them down (divide by the width of the current view or something) but that would work. I could just poll that each frame.

Thanks!