I've implemented an OpenGL based terrain unsing a tesselation-shader for dividing each 'terrain-cell' into the desired tiles. The heightmap is uploaded to the GPU and applied on the shader.

When it comes to picking, I don't really know what to do. Testing triangles on intersection with the pick ray is not possible because only the GPU 'knows' the mesh.

Is render to texture with an unique color the way to go? Or holding an additional mesh in RAM, only for picking/collision?


1 Answer 1


Ray-casting into the height map on the CPU will likely be much faster with proper optimizations and it wont unexpectedly fail (eg: due to some odd driver rounding issues on a rare-ish laptop chipset creating a few angry customers.)

Either of those solution would work but reading a texture or frame (or any data) from GPU RAM is extremely slow (relatively speaking).

You could also use a Transform Feedback to get the result from a geometry shader https://www.opengl.org/wiki/Transform_Feedback

The pipelining issue:

With either texture or transform feedback you'll need to implement pipelining to have the GPU do the picking without incurring a stall making sure you wait to get the result 1 to 4 frames later.

OpenCL might make it easier to get asynchronous results but that's potentially adding a lot of compatibility/timing/driver issues.

There can be multiple frames "in flight" being rendered/queued by the GPU which in most case needs to be finished before you get your data back so every time you ask the OpenGL driver for data it has to wait for everything to be done.

During that time no GPU render commands are queued so you're waiting (for example) for 2 frames for the GPU to finish its queue then waiting to get the data off the PCIe bus back into system RAM then the GPU sits idle for another frame while the CPU fills the render command queue back up, then it takes another frame for the GPU to finish drawing and display it so you've incurred a huge stall of 4-5 frames worth of rendering.

(This is an example, different GPUs and drivers will incur different penalties.)

All this just to get a coordinate on a height map.


You must log in to answer this question.