views:

346

answers:

2

Hey, I'm working on a map editor for my game, and I'm trying to convert the mouse position to a position in the game world, the view is set up using gluPerspective

+5  A: 

A good place to start would be the function gluUnProject, which takes mouse coordinates and calculates object space coordinates. Take a look at http://nehe.gamedev.net/data/articles/article.asp?article=13 for a basic tutorial.

UPDATE:

You must enable depth buffering for the code sample I provided to work. The Z value for mouse coordinates is determined based on the value in the depth buffer at that point.

In your initialization code, make sure you do the following:

glEnable(GL_DEPTH);
robinjam
I'm drawing a Quad 1 unit long in 0, 0, 0, then when I put my mouse over it, it gives me a ridic position as 17, -344, -177.Anything I can do about it?The camera Y is 10.
Tamir
Do you have depth buffering enabled?
robinjam
I'm not too sure how to enable it, I'm pretty sure it is since I'm calling glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
Tamir
+2  A: 

A point on the screen represents an entire line (an infinite set of points) in 3D space.

Most people with questions similar to yours are really trying to select an object by clicking on it. If that's what you're after, OpenGL offers a selection mode that's generally more effective than trying to convert the screen coordinate into real-world coordinates.

Using selection mode is (usually) pretty simple: you start with gluPickMatrix, which you use to specify a small box around the click point. You then draw your scene in selection mode. When you're done, instead of actually drawing anything, it gives you back records of what would have been drawn in the box you specified. If memory serves, those are arranged in Z order, so the first one in the list is what would have displayed front-most (i.e., the one you usually want).

Jerry Coffin
I also was about to point out selection mode. One problem I see with selection mode in this use case is that it seems the program should follow the mouse cursor in real time. For that, selection mode can become too laggy.
ypnos
@ypnos: It's certainly not the one and only possible answer -- but unless there's a really good reason to do otherwise, it's what I'd start with, and only do something else when/if it became apparent that performance was going to be a real problem.
Jerry Coffin