3D Picking

31st October 2005 @ 9:48 am
Processing, Processing-Hacks, Applets, Teaching and 3D
Back to our regular scheduled programming.

I've just been experimenting with a couple of implementations of 3D picking for simple scenes composed of triangles.

Picking with buffer is really simple, and can be pixel-accurate (but isn't in this case because I reduced the picking resolution for speed reasons). It requires you to draw your scene twice... once as you normally would, and once to an offscreen buffer using the triangle index as the fill colour. You then look at the colour of the object under mouseX/Y in the buffer to get the index of the picked object. It's a bit slow, but it works accurately even in this random-triangle scene.

Picking with projection projects all the triangles to screen space, depth sorts them, and checks if any of the projected triangles contains the mouse point. It's not 100% accurate, but for scenes without intersecting triangles it will be fine, and it's faster than the buffer-based method.

There are much faster methods if you're dealing with more geometry, but either of these methods should be fine for simple scenes. On the Processing board, cello points out this great tutorial for doing the same things in OpenGL. Marquee selection would be a simple extension of the pixel buffer method, and a ray-based tutorial is in the works (stay tuned).

It's worth noting that the back-buffer method documented there works for P3D (so no need to create another graphics object) but that in OpenGL the get(mouseX,mouseY) method doesn't work in the same way yet, so it requires different code (example here).

Apologies for delay with raycasting example. If anyone else wants to do it, please feel free and I'll link to it from here. Otherwise, don't hold your breath!