# Triangle picking

This topic is 4638 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I've managed to perform triangle picking with gluProject by projecting the vertices to the window area and then checing these with the mouse coordinates. A problem arises, when any of these points lies beyond the window region. In this scenario I'm not able to pick a triangle even though it is partially visible. So the only way to handle this situation afaik is to unproject the mouse point and then do a line-triangle intersection test. I've tried to use gluUnproject to unproject the mouse position and then make a line by adding the camera direction to this point, but then i realized it can't be done this way. The line will just extend nearly from the centre of the screen. I hope you understand what I'm trying to achieve here, any help is welcome.

##### Share on other sites
Quote:
 Original post by jmakitalo...I've tried to use gluUnproject to unproject the mouse position and then make a line by adding the camera direction to this point, but then i realized it can't be done this way. The line will just extend nearly from the centre of the screen....
That's close, what you need instead of a ray from the mouse position in the same direction as the camera is a ray from the camera that goes through the mouse position. You do something like this...
// (winx, winy) is the position of the mouse in window coordinates// this will get the position of the mouse at the near plane (winz=0)// it doesn't really matter what you use for winz since the ray will go through any (every) point gluUnproject(winx, winy, 0.0, model, proj, view, &mousePos.x, &mousePos.y, &mousePos.z);// Origin of the ray is just the camera positionVec3 rayOrigin = camPos;// Direction of the ray is the vector from the camera position to the mouse positionVec3 rayDirection = mousePos - camPos;
Now you can use that ray in your ray-<whatever> intersection test.

##### Share on other sites
Thanks, it did the trick.

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013597
×