Jump to content
  • Advertisement
Sign in to follow this  
cadmonkey39

mouse ray intersect

This topic is 2345 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!