View Full Version : picking or selecting a triangle of a mesh?

11-02-2004, 07:24 PM
I'm not sure which method the best is. I have a mesh and I want to select a vertex with the mouse and give him another color. this all should be working in realtime.
There's also a method from the OpenGl utility library. you can use the gluUnProject to transform window coordinates into object coordinates.
So but I don't know what to use.
thanks for help Lydia

11-02-2004, 10:50 PM
I use gluUnProject>> you only have to read the depth of the pixel, pass it along with the window position and matrices to gluUnproject and you get it's originall position in the 3d space...
Then all you have to do it to check which of your vertices it the closest one. It works fine for triangles too, unless they are very narrow. But this can be solved by more complex checking whether the point belongs to the triangle(there are some collision tutorials out there). The whole solution doesn't affect the frame rate, at least in my case.
As far as I remember when you do picking, you have to ~redraw at least part of your scene. And it works on per primitive basis...

11-02-2004, 11:05 PM
You can use this tutorial to calculate a ray from the mouse position to the 3D world:


It is Direct3D based, but it is just the same with OpenGL.. (I just have replaced "/ aspect" by "* aspect").

Next, you have to calculate Ray/Triangle intersection between the ray and all triangles of your mesh.

(you can find a ray/triangle intersect routine here: http://www.realtimerendering.com/#isect)

When you know the impact position, you just have to select the one of the 3 triangle-vertices which is the nearest of the impact position.

For a very large mesh, you could use an octree to discard some triangle tests.

11-03-2004, 12:15 AM
You can render each triangles with a different color with GL_FLAT in the backbuffer or a pbuffer. The color is your ID of the triangle. Its works very well with a 32bit colorbuffer. After the rendering you read it back with glReadPixel(only the part you are interessted in). You can also use glScissor to save fullrate. With this methode you can also pick many triangles. It's described in the book real time rendering. Maybe you purchase it. You can also use the the stencil buffer if your triangles not exceed 255. I know that the z buffer is also a possibility but have never used it.

11-03-2004, 01:14 AM
The color buffer trick is only tested by me on some nvidia cards. You can be get problems on other cards. Maybe I will use the backbuffer and a simple shader. This should be more robust.

11-03-2004, 01:03 PM
Use Bumpers suggestion (raycasting). It will give you the best flexibility and you will learn some very useful techniques (for example it will give you some of the ground work required for collision detection).