mouse ray intersect

Started by
3 comments, last by cadmonkey39 11 years, 9 months ago
Hi,
I've got a terrain mesh(es) pre-calculated from a heightmap, and want to change it to generate the height on the fly from the heightmap in a vertex shader.

As its currently pre-calced, i can use standard mouse-ray picking to get my mouse world position, but once the shader is providing the height, how can I still get the mouse world position?

regards,
Nat.
Advertisement
You could read back the depth buffer value of the pixel under the cursor to get Z, and use that to unproject the point and get the world position. If you don't want to do that, you can write a function that calculates the height on the fly while doing the intersection test, in the same way as the shader, by keeping a copy of the heightmap in memory.

You could read back the depth buffer value of the pixel under the cursor to get Z, and use that to unproject the point and get the world position.

Though this is a valid way of doing it (I do it myself to pick objects), it bears the danger of flushing the command queue of the GPU which could result in bad performance. First off restrict the readback only to a very small section (1 pixel) and try to move the read back as far as possible away from the render calls, i.e. just before clearing or refilling the depth buffer again in the next frame.
I'm assuming that you mean that it's a predefined set of vertices with the heights already applied, and that you're going to be moving to a shader that offsets the vertices instead? Well, how are you planning to offset the vertices in the shader? With a texture and a vertex-texture-fetch in the vertex shader? Or are you going to offset by means of noise functions in the vertex shader?

If it's by texture, then I'd say you could read back that value from the texture rather than from the depth buffer. If it's by noise functions, then you'd probably have to do it via a sampling of the depth buffer.

OR, if using a texture for the heightmap, you could set up a post-processing pipeline and include an "overlay" texture which represents any user interaction. This way you could adjust the size of the "brush" that offsets the terrain. You'd do an RTT that combines the current heightmap with the overlay texture (which varies depending on if the user clicked, brush selection, etc.) to render to another heightmap. Then you ping pong back and forth between the heightmaps so that you can update the values seen by the vertex shader.
Thanks guys, i'll look into reading the depth buffer.

This topic is closed to new replies.

Advertisement