Picking and Selection
To provide visual object interaction in the editor environment, there needs to be some way to determine which object is located at a specific pixel coordinate in the actual CamView. The two most obvious approaches are Picking and (physics / collision driven) Raycasting. As the latter one requires a working collision detection system which is tightly connected to a lot of design descisions I haven’t made yet, I’ve implemented Picking for now.
There was a lot of experimentation. In my first approach, I followed this tutorial, trying to use OpenGLs GL_SELECT mode. I got a working prototype really quick, but when it came to an actual implementation, things seemed to fall apart. After hours of debugging and random, unexpected or simply wrong behaviour, I gave up. I think it might be possible that OpenGL Selection is simply too outdated to still be properly supported, but that’s just a guess. Might also have been a bug in my own code, but if you run into similar problems and waste hours – I warned you.
However, I’m now rendering a special “picking” version of the scene onto a RenderTarget. There is no Antialiazing, no Blending, only Alpha- and Depth-Testing and a special Shader, rendering every object flat and in one unique color. All this is simply done by a DrawTechnique override for incoming vertices in the Camera. The Picking RenderTarget is rendered once a frame, if requested (which is implicit by calling a picking method) and allows to track down each pixels object simply by reading out the objects unique color value and doing a lookup in a previously generated table.
For better performance when doing box-selection, the complete Picking Texture is downloaded from the video cards memory once after rendering it. All Picking queries are executed on buffered pixel data, instead of re-downloading it each query. Still, the algorithm is optimized for editor usage, not ingame usage. It might work out well, but I haven’t tested it yet and suppose it will seriously impact performance. Doing it each frame means rendering each scene twice and downloading huge chunks of data from video memory. Ingame mouse interaction should be done properly on a collision detection basis.