Skip to main content
gamedev.net
🔒 Locked

Mouse detection/Picking

Started by Grog 4 replies 650+ views
Original Post
Grog
Grog
Hi, I need to do some mouse detection in opengl. Basically I have an array of points in 3d and want to tell which one is closest to the mouse. Or another way of looking at it is if you are looking down on a chessboard and you want to select the squares. What''s the best way? With picking it looks like I would have to go to object mode and render every square and give every square a object id. Is there an easier way to get the equasion of the ray using the pixel coordinates and the viewing matrix and figuring it out myself? Which way is faster?
Sander
Sander
I have used picking and selection on 3D characters and 3D terrain at the same time. It's not too slow for me. It checks every 3D character every frame, and checks the exact 3D coordinates on 3D terrain whenever you click the terrain. In my demo the terrain is fairly large and takes about 0.1 seconds to process on my PIII 600 Mhz. You don't even notice the time it takes to check all the characters. It's very fast. The slowest part of rendering OpenGL stuff is the actual on-screen drawing. The trick to speed selection up is render only to the selection buffer with all rendering options (lighting, fog, textures, whatever else slows it down) turned OFF.

If you do not have to click constantly on a lot of different objects every frame, I would definately recommend picking and selection. To see my implementation, download the Raid3D demo from my site (see my signature).

Sander Maréchal
[Lone Wolves Production][Articles][GD Emporium][Webdesign][E-mail]

[edited by - smarechal on January 26, 2003 4:26:05 PM]
beaton_john
beaton_john
You could also use gluUnProject/gluProject , passing the coordinates of your model (xyz) and checking the xy coordinates returned against the mouse
Grog
Grog
Wow, thanks. I need to do the same thing as Sander in that I need the coordinates on the terrain clicked. I downloaded the program, but didn''t read the code yet. It''s missing a header for the music so won''t compile, but the exe is there. It looks good, although I would brighten up the lights a bit.

.1 seconds seems excruciatingly slow though. In this program, this picking feature will be hooked to the mouse passivly, because the area where the mouse is needs to be highlighted as the user moves it around, so I need to do this check every frame. If it took .1 seconds this would max out my fps at 10, which is too slow. I think I will just use the gluUnProject, I was wondering if there was a function to do it for you, so thanks for that. I just looked it up in the vc++ help and it has you give three coordinates for the window position. You should only need x and y right? Does anyone know why the z is there?
Grog
Grog
Also on the window coordinates, where is (0,0), ie the upper left corner, the lower left corner, the middle of the screen, does anyone know?
Sander
Sander
To compile my demo you will need the directX 8 or 9 SDK. Then it''ll work fine (I really should put that on the site...)

If your terrain is not too big and the highlighted area isn''t too small you can still get away with using picking. My Raid3D problem is that I have huge terrain and a small selection area. Therefor I need to make several passes with picking to increase the picking resolution becasue you can only use 65 objects in a picking array.

Sander Maréchal
[Lone Wolves Production][Articles][GD Emporium][Webdesign][E-mail]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.