picking or selecting a triangle of a mesh?

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

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…

You can use this tutorial to calculate a ray from the mouse position to the 3D world:

http://www.mvps.org/directx/articles/rayproj.htm

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.

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.

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.

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).