Creating a pickray with mouse?

I was wondering if anyone could help me with a little problem I’m having. I’m trying to create a pickray for 3D object selection?

I’ve got the mouse cursor being drawn in ortho (screen) mode, but I’m not sure how to take this point in screen space and extend it out into the game world…

How can I create the ray from a point in screen space to a point in the world? I have been trying gluUnProject but I’m not sure how to create the ray from the screen (x,y,z=0) to a world point (x,y,z)?

Can anyone help?


gluUnproject maps a 2D point (mouse cursor for example) in to the 3D coordinate space, you just give it the X and Y cursor (remeber to swap Y) and a Z coordinate (0.0-1.0) and it will transform this into a point in the 3D world at that point, the problem here is of course that you need to give it a Z coordinate also, this mean that even if you click on something in the 3D world, you may end up with a 3D coordinate that is at that specific position but the distance may be incorrect.

When you use selection mode you get depth values from the objects you hit, and that can of course be used to find the Z value.

But then again, you might just use selection mode to get the hit, is there any problem doing that ?


Yeah, I was thinking about using OpenGL’s selection or feedback mode, but regarding selection mode: Because of the re-rendering the scene in selection mode, would that be bad for an RTS game? (like be too slow?) I just figured from what I’ve been reading (3D Game Engine Design, Eberly) that it seems to be the “professional” way to do object selection with pickrays?

If GL’s selection mode is fast enough, since I already know how to use it, I’ll use it, but is it good for games?

OpenGL’s selection is pretty fast.
I’m sure you won’t run into any trouble using it for your RTS.

…give it a try.

Indeed. I’ll give it a go. Thanks.

Hmm, actually I’m not sure if I should use GL’s selection constructs… They would be good for picking for the game’s objects, but what about for walking/moving around (as in click on a location and the units move there?). That would mean I would have to have all the units in the name stack (which would be OK, I guess) but also all the world/map data in the name stack so it could be clicked on!

Maybe a combination of pickrays AND the selection buffer may be needed? Perhaps I could put all units in the name stack after a sweep select, then use a pickray for selecting where they should move? But, if I can get a functioning pickray, what would be the point of using the name stack at all?

That would mean I would have to have all the units in the name stack
You only need to draw what’s in the selection rectangle. There’s no need to have all units on the name stack, unless they just happen to be in one prodigious pile on the ground. You want to restrict drawing as mmuch as possible, just the stuff that can intersect the selection frustum.

Yeah, but all the units still have to be pushed onto the name stack even if their not immediately being selected, because if they don’t have a name then they can’t be found with the pick matrix? Every unit must have a name and every tile/cell must have a name or else how would I pass thier ID’s to the pick matrix? Wouldn’t it be a bad idea to have a large amount of vertex ID’s floating around? (Please correct me if I’m wrong :stuck_out_tongue: )

Hmmm, you seem to have a fundamental misunderstanding about the way the selection works. The basic idea is to push a name onto the stack before you draw an object, draw the object, then pop the name off the stack. If during the rendering of the object it is determined that a selection hit has been made, then its corresponding record will be stored in the selection buffer. Thus, at most you will have a stack depth no greater than the name depth complexity of an individual object.

I suggest you check out the redbook and read the section on selection carefully. h14.html

Also, line tracing is a viable method. If you know how to trace each object in your world, then your work is already done. It’s unlikely that selection will be able to compete with some fast line-traces, in terms of sheer speed. Is this a top-down view game? If so, you should be able to make some geometric assumptions that could simplfy things quite a bit.